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

bpo-46475: Add typing.Never and typing.assert_never #30842

Merged
merged 13 commits into from
Feb 8, 2022

Conversation

JelleZijlstra
Copy link
Member

@JelleZijlstra JelleZijlstra commented Jan 23, 2022

Another pending proposal from typing-sig.

  • Add a Never primitive that is semantically identical to NoReturn, but documented explicitly as a bottom type. I chose to make it a separate object instead of a pure alias for NoReturn so that dynamic checkers can distinguish between the two and so that we can document them separately.
  • Add an 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

Copy link
Member

@sobolevn sobolevn left a 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!

Doc/library/typing.rst Show resolved Hide resolved
Doc/library/typing.rst Outdated Show resolved Hide resolved
Lib/test/test_typing.py Outdated Show resolved Hide resolved
Lib/typing.py Show resolved Hide resolved
Doc/library/typing.rst Show resolved Hide resolved
Doc/library/typing.rst Outdated Show resolved Hide resolved
Lib/typing.py Outdated Show resolved Hide resolved
Doc/library/typing.rst Outdated Show resolved Hide resolved
Copy link
Member

@gvanrossum gvanrossum left a 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. :-( )

Lib/test/test_typing.py Outdated Show resolved Hide resolved
@JelleZijlstra
Copy link
Member Author

Thanks @gvanrossum! I think I addressed all previous comments, so from my perspective this is ready.

https://mail.python.org/archives/list/typing-sig@python.org/thread/CGIYKGMF6XLDX3F2JNRCCE7HC6K2XLZ2/ is the typing-sig thread. Nobody opposed it.

@gvanrossum
Copy link
Member

Should I wait for the tests or are they hanging?

@JelleZijlstra
Copy link
Member Author

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:

  • We wait for the tests on Self to pass and merge it
  • I fix the conflict here and wait for tests to pass again
  • Then we merge this one

Copy link
Member

@gvanrossum gvanrossum left a 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...

Doc/library/typing.rst Outdated Show resolved Hide resolved
Doc/library/typing.rst Outdated Show resolved Hide resolved
Doc/library/typing.rst Show resolved Hide resolved
Doc/library/typing.rst Outdated Show resolved Hide resolved
Lib/typing.py Outdated Show resolved Hide resolved
Lib/typing.py Outdated Show resolved Hide resolved
Lib/typing.py Show resolved Hide resolved
Lib/typing.py Outdated Show resolved Hide resolved
Lib/typing.py Outdated Show resolved Hide resolved
Comment on lines +1984 to +1994
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.
Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member

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 :-).

@JelleZijlstra
Copy link
Member Author

Thanks for your feedback! I pushed a round of docs changes.

Doc/library/typing.rst Outdated Show resolved Hide resolved
Lib/typing.py Outdated Show resolved Hide resolved
Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

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

LGTM — thanks!

Copy link
Member

@gvanrossum gvanrossum left a 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.

@JelleZijlstra
Copy link
Member Author

Thank you for the feedback!

JelleZijlstra added a commit to JelleZijlstra/typing that referenced this pull request Feb 9, 2022
JelleZijlstra added a commit to python/typing that referenced this pull request Feb 11, 2022
JelleZijlstra added a commit to python/typing_extensions that referenced this pull request May 19, 2022
JelleZijlstra added a commit to python/typing_extensions that referenced this pull request May 19, 2022
JelleZijlstra added a commit to python/typing_extensions that referenced this pull request May 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants