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

bpo-46480: add typing.assert_type by JelleZijlstra · Pull Request #30843 · python/cpython · GitHub

/ cpython Public

bpo-46480: add typing.assert_type - #30843

Merged
JelleZijlstra merged 9 commits into
python:mainfrom
JelleZijlstra:asserttype
Mar 17, 2022
Merged

bpo-46480: add typing.assert_type#30843
JelleZijlstra merged 9 commits into
python:mainfrom
JelleZijlstra:asserttype

Conversation

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

Copy link
Copy Markdown
Member

Another pending typing-sig proposal. This function serves as a static assertion to the type checker about the type of a value.

https://bugs.python.org/issue46480

Comment thread Doc/library/typing.rst

Copy link
Copy Markdown
Member Author

@gvanrossum this one is also ready I believe. Feedback on this function in https://mail.python.org/archives/list/typing-sig@python.org/thread/MITFQ6Z45RRMXY3HNM66IC3XXS3TA3JN/#MITFQ6Z45RRMXY3HNM66IC3XXS3TA3JN was positive. (I also proposed assert_error() there, but dropped it because of negative feedback.)

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

Copy link
Copy Markdown
Contributor

It occurs to me that users of "assert_type()" may be surprised that it doesn't actually perform an assertion at runtime.

Consider code like:

from typing import assert_type

def parse_int(value) -> int:
    # Hmm. A casual observer would likely read this incorrectly as `assert isinstance(value, str)`...
    assert_type(value, str)
    return int(value)

I wonder if we might consider a word other than "assert", or maybe add the word "static" somewhere. Suggestions:

  • prove_type(value, str)
  • static_assert_type(value, str)

Copy link
Copy Markdown
Member Author

That's a reasonable concern, but it's probably better discussed on the typing-sig thread. I'm happy to change the PR if there's consensus for another name. I'll note though that cast() has the same potential problem, and I haven't heard of confusion because of that.

Copy link
Copy Markdown
Contributor

it's probably better discussed on the typing-sig thread

I'll plan to make a post tomorrow.

Copy link
Copy Markdown
Member Author

@gvanrossum I think this should be ready to merge soon, since people on the typing-sig thread now seem on board with the assert_type name. I'd appreciate some more reviews for the documentation though, maybe @Fidget-Spinner or @AlexWaygood.

AlexWaygood self-requested a review March 10, 2022 06:41

Copy link
Copy Markdown
Member

@gvanrossum I think this should be ready to merge soon, since people on the typing-sig thread now seem on board with the assert_type name. I'd appreciate some more reviews for the documentation though, maybe @Fidget-Spinner or @AlexWaygood.

I'll try to take a look soon!

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

Thanks! Looks pretty good, just a few thoughts :)

Comment thread Doc/library/typing.rst Outdated
Comment thread Doc/library/typing.rst Outdated
Comment thread Doc/library/typing.rst Outdated
Comment thread Doc/library/typing.rst
JelleZijlstra and others added 3 commits March 11, 2022 19:17
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>

Copy link
Copy Markdown
Member Author

Thanks for the review!

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!

davidfstr left a comment

Copy link
Copy Markdown
Contributor

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

Just 1 tiny nit:

Comment thread Lib/typing.py Outdated
Co-authored-by: David Foster <david@dafoster.net>

Copy link
Copy Markdown
Member Author

Planning to merge this tomorrow unless I get more feedback.

Comment thread Doc/library/typing.rst Outdated
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
JelleZijlstra self-assigned this Mar 12, 2022
JelleZijlstra merged commit 96568e9 into python:main Mar 17, 2022
JelleZijlstra deleted the asserttype branch March 17, 2022 03:02
JelleZijlstra added a commit to JelleZijlstra/typing that referenced this pull request Mar 17, 2022
JelleZijlstra added a commit to JelleZijlstra/mypy that referenced this pull request Apr 14, 2022
See python/cpython#30843.

The implementation mostly follows that of cast(). It relies on
`mypy.sametypes.is_same_type()`.
JelleZijlstra added a commit to python/mypy that referenced this pull request Apr 15, 2022
See python/cpython#30843.

The implementation mostly follows that of cast(). It relies on
`mypy.sametypes.is_same_type()`.
JukkaL pushed a commit to python/mypy that referenced this pull request Apr 20, 2022
See python/cpython#30843.

The implementation mostly follows that of cast(). It relies on
`mypy.sametypes.is_same_type()`.
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.

5 participants


Back | FazBrowse Home | New Git URL