-
-
Notifications
You must be signed in to change notification settings - Fork 30.4k
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
bpo-46475: Add typing.Never and typing.assert_never #30842
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you! Awesome features!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should iterate this until @Fidget-Spinner approves it and then he can merge it. I think the idea is good, and IIRC typing-sig is in agreement. (Though are there folks at Pyre or Google who don't read it but want issues instead? Sigh. :-( )
Thanks @gvanrossum! I think I addressed all previous comments, so from my perspective this is ready. https://mail.python.org/archives/list/[email protected]/thread/CGIYKGMF6XLDX3F2JNRCCE7HC6K2XLZ2/ is the typing-sig thread. Nobody opposed it. |
Should I wait for the tests or are they hanging? |
They just started, I had to fix a merge conflict. It's going to conflict with the Self change too, which is why I pinged you on that one first. I think this is the easiest way forward:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I've got more...
def int_or_str(arg: int | str) -> None: | ||
match arg: | ||
case int(): | ||
print("It's an int") | ||
case str(): | ||
print("It's a str") | ||
case _: | ||
assert_never(arg) | ||
|
||
If a type checker finds that a call to assert_never() is | ||
reachable, it will emit an error. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This example is much better than the examples in the main doc. :-) And so is the sentence following the example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That sentence is also in the docs for assert_never, but not Never. I'm pushing some changes to improve the Never docs according to your feedback.
Honestly I'm a bit confused about the relationship between the docstring and the RST docs. I end up copying from one to the other.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Usually the docstring is shorter, since it is often displayed by tools that have limited output space (it's really annoying to get a scroll bar in a tooltip in VS Code :-).
Thanks for your feedback! I pushed a round of docs changes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM — thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great! Thanks for being patient with me.
Thank you for the feedback! |
Backport of python/cpython#30842, with additional tests from @sobolevn's python/cpython#31222.
Backport of python/cpython#30842, with additional tests from @sobolevn's python/cpython#31222.
Backport of python/cpython#30842, with additional tests from @sobolevn's python/cpython#31222.
Backport of python/cpython#30842, with additional tests from @sobolevn's python/cpython#31222.
Backport of python/cpython#30842, with additional tests from @sobolevn's python/cpython#31222.
Another pending proposal from typing-sig.
Never
primitive that is semantically identical toNoReturn
, but documented explicitly as a bottom type. I chose to make it a separate object instead of a pure alias forNoReturn
so that dynamic checkers can distinguish between the two and so that we can document them separately.assert_never()
function to statically assert that code is unreachable. Based on @davidfstr's feedback, the error raised by the function does not include the argument.https://bugs.python.org/issue46475