Skip to content

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

@JelleZijlstraJelleZijlstra commented Jan 24, 2022

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 threadDoc/library/typing.rst
@JelleZijlstra

Copy link
Copy Markdown
MemberAuthor

@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 threadDoc/library/typing.rst Outdated
Comment threadLib/test/test_typing.py Outdated
Comment threadLib/typing.py Outdated
@davidfstr

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)

@JelleZijlstra

Copy link
Copy Markdown
MemberAuthor

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

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

I'll plan to make a post tomorrow.

@JelleZijlstra

Copy link
Copy Markdown
MemberAuthor

@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
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!

@AlexWaygoodAlexWaygood 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.

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

Comment threadDoc/library/typing.rst Outdated
Comment threadDoc/library/typing.rst Outdated
Comment threadDoc/library/typing.rst Outdated
Comment threadDoc/library/typing.rst
JelleZijlstraand 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
Copy Markdown
MemberAuthor

Thanks for the review!

@AlexWaygoodAlexWaygood 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.

LGTM!

@davidfstrdavidfstr 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.

Just 1 tiny nit:

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

Copy link
Copy Markdown
MemberAuthor

Planning to merge this tomorrow unless I get more feedback.

Comment threadDoc/library/typing.rst Outdated
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
@JelleZijlstraJelleZijlstra self-assigned this Mar 12, 2022
@JelleZijlstra
JelleZijlstra merged commit 96568e9 into python:mainMar 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 freeto 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

@JelleZijlstra@davidfstr@AlexWaygood@sobolevn@bedevere-bot