Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle type error when navigating #3699

Merged

Conversation

marcuslimdw
Copy link
Contributor

In the event that target does not satisfy the type hint for to, the user will get an UnboundLocalError for path, which is confusing. This PR explicitly handles the case where to gets an input of an unexpected type.

@rodja
Copy link
Member

rodja commented Sep 9, 2024

Thanks for the PR, @marcuslimdw. This is only one special case. If we implement this, we should do it for the entire public API. And that is quite a lot of code which needs to be written, tested and maintained. Maybe it's better to rely on static type checkers like mypy to catch such errors.

@marcuslimdw
Copy link
Contributor Author

@rodja I agree that, in theory, all projects should incorporate static type checkers, because what can be checked before runtime should be checked before runtime.

From a user experience perspective, IMO, it is likely that sooner or later, a user will run into this (which is basically a pattern matching failure) and be confused by the UnboundLocalError, so the question is - is the maintenance burden worth the easier debugging?

Lastly, it's apposite to note that even in the stdlib, there is a function provided for this exact case: assert_never, which might be better than a custom error.

@falkoschindler falkoschindler added this to the 2.2 milestone Sep 26, 2024
@falkoschindler falkoschindler added the enhancement New feature or request label Sep 26, 2024
Copy link
Contributor

@falkoschindler falkoschindler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the pull request, @marcuslimdw!

We came to the conclusion that raising a TypeError in an else block is a good idea. While it improves the error message for the user, it also improves code readability, since it is a clear indication that path will be defined after the if-elif-else block. I just shortened the error message a little to avoid the risk of it getting outdated in case we change the set of supported types in the future.

There are lots of other places where wrong input types lead to weird exceptions. But we won't be able (and willing) to address all of them. This issue is a great example for the value of static type checkers and linters like mypy and pylint.

@falkoschindler falkoschindler merged commit 680b100 into zauberzeug:main Sep 26, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants