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-46480: add typing.assert_type #30843

Merged
merged 9 commits into from
Mar 17, 2022
Merged

Conversation

JelleZijlstra
Copy link
Member

@JelleZijlstra JelleZijlstra commented Jan 24, 2022

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

@JelleZijlstra
Copy link
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.)

Doc/library/typing.rst Outdated Show resolved Hide resolved
Lib/test/test_typing.py Outdated Show resolved Hide resolved
Lib/typing.py Outdated Show resolved Hide resolved
@davidfstr
Copy link
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)

@JelleZijlstra
Copy link
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.

@davidfstr
Copy link
Contributor

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

I'll plan to make a post tomorrow.

@JelleZijlstra
Copy link
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 AlexWaygood self-requested a review March 10, 2022 06:41
@AlexWaygood
Copy link
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!

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.

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

Doc/library/typing.rst Outdated Show resolved Hide resolved
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
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>
@JelleZijlstra
Copy link
Member Author

Thanks for the review!

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!

Copy link
Contributor

@davidfstr davidfstr left a comment

Choose a reason for hiding this comment

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

Just 1 tiny nit:

Lib/typing.py Outdated Show resolved Hide resolved
Co-authored-by: David Foster <david@dafoster.net>
@JelleZijlstra
Copy link
Member Author

Planning to merge this tomorrow unless I get more feedback.

Doc/library/typing.rst Outdated Show resolved Hide resolved
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
@JelleZijlstra JelleZijlstra self-assigned this Mar 12, 2022
@JelleZijlstra JelleZijlstra merged commit 96568e9 into python:main Mar 17, 2022
@JelleZijlstra 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 python/typeshed 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()`.
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