GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — bpo-46480: add typing.assert_type by JelleZijlstra · Pull Request #30843 · python/cpython

Conversation

@JelleZijlstra

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

Uh oh!

There was an error while loading. Please reload this page.

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@JelleZijlstra
bpo-46480: add typing.assert_type
4cdf6d3
@JelleZijlstra
JelleZijlstra requested review from Fidget-Spinner and gvanrossum as code owners January 24, 2022 00:37
@JelleZijlstra JelleZijlstra added awaiting review CLA signed DO-NOT-MERGE labels Jan 24, 2022
sobolevn
sobolevn reviewed Jan 24, 2022
View reviewed changes
Comment thread Doc/library/typing.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

@JelleZijlstra
Merge branch 'main' into asserttype
629f180
@JelleZijlstra JelleZijlstra removed the DO-NOT-MERGE label Feb 2, 2022
@JelleZijlstra

JelleZijlstra commented Feb 2, 2022

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/[email protected]/thread/MITFQ6Z45RRMXY3HNM66IC3XXS3TA3JN/#MITFQ6Z45RRMXY3HNM66IC3XXS3TA3JN was positive. (I also proposed assert_error() there, but dropped it because of negative feedback.)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

davidfstr
davidfstr reviewed Feb 2, 2022
View reviewed changes
Comment thread Doc/library/typing.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/test/test_typing.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/typing.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@JelleZijlstra
feedback from David Foster
3b09f1b
@davidfstr

davidfstr commented Feb 2, 2022

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)
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@JelleZijlstra

JelleZijlstra commented Feb 2, 2022

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@davidfstr

davidfstr commented Feb 2, 2022

Copy link
Copy Markdown
Contributor

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

I'll plan to make a post tomorrow.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@JelleZijlstra
Merge branch 'main' into asserttype
29176e2
@JelleZijlstra

JelleZijlstra commented Mar 10, 2022

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

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

AlexWaygood commented Mar 10, 2022

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!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

AlexWaygood
AlexWaygood reviewed Mar 11, 2022
View reviewed changes

@AlexWaygood AlexWaygood left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this 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 :)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Doc/library/typing.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Doc/library/typing.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Doc/library/typing.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Doc/library/typing.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

JelleZijlstra and others added 3 commits March 11, 2022 19:17
@JelleZijlstra
Merge remote-tracking branch 'upstream/main' into asserttype
e035815
@JelleZijlstra @AlexWaygood
Apply suggestions from code review
ba35012
Co-authored-by: Alex Waygood <[email protected]>
@JelleZijlstra @AlexWaygood
Apply suggestions from code review
49765d3
Co-authored-by: Alex Waygood <[email protected]>
@JelleZijlstra

JelleZijlstra commented Mar 12, 2022

Copy link
Copy Markdown
Member Author

Thanks for the review!

AlexWaygood reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

AlexWaygood
AlexWaygood approved these changes Mar 12, 2022
View reviewed changes

@AlexWaygood AlexWaygood left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

LGTM!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bedevere-bot bedevere-bot added awaiting core review and removed awaiting review labels Mar 12, 2022
davidfstr
davidfstr reviewed Mar 12, 2022
View reviewed changes

@davidfstr davidfstr left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this 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:

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Lib/typing.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@JelleZijlstra @davidfstr
Update Lib/typing.py
b2c4cc5
Co-authored-by: David Foster <[email protected]>
@JelleZijlstra

JelleZijlstra commented Mar 12, 2022

Copy link
Copy Markdown
Member Author

Planning to merge this tomorrow unless I get more feedback.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

AlexWaygood
AlexWaygood reviewed Mar 12, 2022
View reviewed changes
Comment thread Doc/library/typing.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

@JelleZijlstra @AlexWaygood
Update Doc/library/typing.rst
e96c988
Co-authored-by: Alex Waygood <[email protected]>
@JelleZijlstra JelleZijlstra self-assigned this Mar 12, 2022
@JelleZijlstra
JelleZijlstra merged commit 96568e9 into python:main Mar 17, 2022
@bedevere-bot bedevere-bot removed the awaiting core review label 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
Add assert_type
9ffa1e1
See python/cpython#30843
This was referenced Mar 17, 2022
Add assert_type python/typing#1103
Merged
Add typing.TypeVarTuple, Unpack, assert_type python/typeshed#7501
Merged
JelleZijlstra added a commit to python/typeshed that referenced this pull request Mar 17, 2022
@JelleZijlstra
Add typing.TypeVarTuple, Unpack, assert_type
80b5f3a
python/cpython#31021 and python/cpython#30843.
AlexWaygood pushed a commit to python/typeshed that referenced this pull request Mar 18, 2022
@JelleZijlstra
Add typing.TypeVarTuple, Unpack, assert_type (#7501)
4d23919
python/cpython#31021 and python/cpython#30843.
@erictraut erictraut mentioned this pull request Mar 21, 2022
Add support for new typing.assert_type and its typing_extension counterpart microsoft/pyright#3233
Closed
@JelleZijlstra JelleZijlstra mentioned this pull request Apr 14, 2022
Add support for assert_type python/mypy#12584
Merged
JelleZijlstra added a commit to JelleZijlstra/mypy that referenced this pull request Apr 14, 2022
@JelleZijlstra
Add support for assert_type
22a8e4d
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
@JelleZijlstra
Add support for assert_type (#12584)
8757747
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
@JelleZijlstra @JukkaL
Add support for assert_type (#12584)
851e89a
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

Reviewers

@sobolevn sobolevn sobolevn left review comments
@AlexWaygood AlexWaygood AlexWaygood approved these changes
@Fidget-Spinner Fidget-Spinner Awaiting requested review from Fidget-Spinner
@gvanrossum gvanrossum Awaiting requested review from gvanrossum
+1 more reviewer
@davidfstr davidfstr davidfstr left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@JelleZijlstra JelleZijlstra

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@JelleZijlstra @davidfstr @AlexWaygood @sobolevn @bedevere-bot
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.