FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

bpo-46475: Add typing.Never and typing.assert_never by JelleZijlstra · Pull Request #30842 · python/cpython · GitHub

/ cpython Public

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

Merged
gvanrossum merged 13 commits into
python:mainfrom
JelleZijlstra:never
Feb 8, 2022
Merged

bpo-46475: Add typing.Never and typing.assert_never#30842
gvanrossum merged 13 commits into
python:mainfrom
JelleZijlstra:never

Conversation

JelleZijlstra commented Jan 23, 2022
edited by bedevere-bot
Loading

Copy link
Copy Markdown
Member

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

sobolevn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Thank you! Awesome features!

Comment thread Doc/library/typing.rst
Comment thread Doc/library/typing.rst Outdated
Comment thread Lib/test/test_typing.py Outdated
Comment thread Lib/typing.py
Comment thread Doc/library/typing.rst Outdated
Comment thread Doc/library/typing.rst
Comment thread Doc/library/typing.rst Outdated
Comment thread Lib/typing.py Outdated
Comment thread Doc/library/typing.rst Outdated

gvanrossum left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

Comment thread Lib/test/test_typing.py Outdated

Copy link
Copy Markdown
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.

Copy link
Copy Markdown
Member

Should I wait for the tests or are they hanging?

Copy link
Copy Markdown
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

gvanrossum left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Sorry, I've got more...

Comment thread Doc/library/typing.rst Outdated
Comment thread Doc/library/typing.rst Outdated
Comment thread Doc/library/typing.rst
Comment thread Doc/library/typing.rst Outdated
Comment thread Lib/typing.py Outdated
Comment thread Lib/typing.py Outdated
Comment thread Lib/typing.py
Comment thread Lib/typing.py Outdated
Comment thread Lib/typing.py Outdated
Comment thread Lib/typing.py
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
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

This example is much better than the examples in the main doc. :-) And so is the sentence following the example.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

Copy link
Copy Markdown
Member Author

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

Comment thread Doc/library/typing.rst Outdated
Comment thread Lib/typing.py Outdated
Comment thread Doc/library/typing.rst

AlexWaygood left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

LGTM — thanks!

Comment thread Lib/typing.py

gvanrossum left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Great! Thanks for being patient with me.

Copy link
Copy Markdown
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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


Back | FazBrowse Home | New Git URL