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-43224: Implement PEP 646 changes to typing.py by mrahtz · Pull Request #31021 · python/cpython

Conversation

@mrahtz

@mrahtz mrahtz commented Jan 30, 2022 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

These are the parts of the old PR (#30398) relevant to typing.py.

Note that we haven't yet merged the grammar PR (#31018), so for the time being all the tests just use Unpack. We can add tests using the actual star operator in a future PR.

https://bugs.python.org/issue43224

Sorry, something went wrong.

Uh oh!

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

All reactions
@the-knights-who-say-ni the-knights-who-say-ni added the CLA signed label Jan 30, 2022
@bedevere-bot bedevere-bot added the awaiting review label Jan 30, 2022
JelleZijlstra
JelleZijlstra requested changes Jan 30, 2022
View reviewed changes
Comment thread Lib/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.

Comment thread Lib/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.

Comment thread Lib/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.

@bedevere-bot bedevere-bot added awaiting core review and removed awaiting review labels Jan 30, 2022
@mrahtz
PEP 646: Implement support in typing.py
0a91264
@mrahtz
mrahtz force-pushed the pep646-typing branch from 83e7476 to 0a91264 Compare January 30, 2022 16:53
@mrahtz
mrahtz marked this pull request as ready for review January 30, 2022 17:00
@mrahtz
mrahtz requested review from Fidget-Spinner and gvanrossum as code owners January 30, 2022 17:00
mrahtz
mrahtz commented Jan 30, 2022
View reviewed changes
Comment thread Lib/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.

mrahtz and others added 5 commits January 30, 2022 20:58
@mrahtz @JelleZijlstra
Fix typo
33dc1bf
Co-authored-by: Jelle Zijlstra <[email protected]>
@mrahtz @JelleZijlstra
Remove unnecessary whitespace
51ea1af
Co-authored-by: Jelle Zijlstra <[email protected]>
@mrahtz
Remove some old debug prints
fa133dd
@mrahtz
Re-implement Unpack using _SpecialForm
6d1126e
@blurb-it
📜🤖 Added by blurb_it.
51870e2
JelleZijlstra
JelleZijlstra reviewed Jan 30, 2022
View reviewed changes
Comment thread Lib/typing.py Resolved

Uh oh!

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

pradeep90
pradeep90 reviewed Feb 1, 2022
View reviewed changes

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

(Reviewed three-fourths of it and got tired after a couple of hours. Will review the rest after the changes.)

Overall, I think we'd want to try other things beyond Unpack[Ts]: Unpack[tuple[int, ...]], Unpack[tuple[int, str]], and Unpack[tuple[int, Unpack[Ts], str]]. I've tried to point to cases inline.

I'd assumed all these comments had been posted weeks ago - I'd missed the part where you actually have to click the 'Submit code review' button to post them! Uff.

Ah, that clears up a mystery :) I was wondering why many comments were unaddressed.


Do we need to validate *args: <foo> somewhere? For example, *args: Ts vs *args: Unpack[Ts].

What if someone tries Union[*Ts] or Union[Ts]? Same for other special forms that call _type_check:

  • ClassVar
  • Optional
  • Final (i.e., x: Final[Ts])
  • TypeGuard
  • What about Concatenate[Ts, P] and Concatenate[*Ts, P]?

n00b question: how do I run the Python tests in my local clone of this repository? Wanted to experiment.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread Lib/test/test_typing.py 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/test/test_typing.py 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/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.

Comment thread Lib/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.

Comment thread Lib/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

JelleZijlstra commented Feb 1, 2022

Copy link
Copy Markdown
Member

n00b question: how do I run the Python tests in my local clone of this repository? Wanted to experiment.

First compile Python, as explained in "Quick reference" at https://devguide.python.org/. To run just the typing tests, the easiest way is just ./python.exe Lib/test/test_typing.py or similar depending on your OS and CWD.

pradeep90 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.

@pradeep90

pradeep90 commented Feb 1, 2022

Copy link
Copy Markdown
Contributor

n00b question: how do I run the Python tests in my local clone of this repository? Wanted to experiment.

First compile Python, as explained in "Quick reference" at https://devguide.python.org/. To run just the typing tests, the easiest way is just ./python.exe Lib/test/test_typing.py or similar depending on your OS and CWD.

Thanks, Jelle. Should have RTFM'ed but was being lazy :|

@mrahtz Some tests fail locally (and on CI, apparently).

All reactions

Sorry, something went wrong.

Uh oh!

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

@mrahtz @JelleZijlstra
Fix call to is_unpacked_typevartuple
bc12b49
Co-authored-by: Jelle Zijlstra <[email protected]>
@mrahtz

mrahtz commented Feb 2, 2022

Copy link
Copy Markdown
Contributor Author

@gvanrossum @JelleZijlstra @pradeep90 There's a high-level design point I've just realised we should discuss.

In the PEP itself, we stated that Unpack would primarily be for backwards compatibility. But in the current iteration of this PR, we're actually using it as the wrapper for unpacked types in general. For example, TypeVarTuple.__iter__ returns Unpack[self].

(@gvanrossum, in case you haven't followed the chain of comments on the PR so far, there were two main reasons to try doing it this way: 1. It means we can avoid the need for a middleman classes UnpackedTypeVarTuple and StarredTuple like we had before; and 2. It means we can also easily support unpacking of other types at runtime, which might be useful for people wanting to experiment with typing features - e.g. Jelle mentioned he'd like to experiment with Unpack[TypedDict type].)

This raises two questions:

  1. How much does it matter if the way that Unpack is used in typing.py doesn't match the spirit in which the PEP introduced it? (It feels a bit ugly to me, but I wonder whether I'm being too perfectionist. An alternative might be to have two separate things, Unpack as the backwards-compatible version of *, then a separate wrapper Unpacked for eliminating the middleman classes.)
  2. What should we do about unpacking a native tuple? In bpo-43224: Implement PEP 646 changes to genericaliasobject.c #31019, this essentially returns a special native 'starred tuple' type. I was about to change it so it instead returned typing.Unpack[self] - but I realise this is against the precedent set by the new union operator, where Foo | Bar also returns a special native type types.UnionType rather than typing.Union[Foo, Bar]. @gvanrossum in particular, do you know whether this was a deliberate decision? Is this a precedent we should respect?
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

Thanks for bringing this up! Here are my takes:

  1. I agree there's some tension here, but I'd rather keep the number of classes lower. Having a separate Unpacked and Unpack doing virtually the same thing would be confusing.
  2. types.UnionType was created specifically because we didn't want built-in syntax to create an instance of a Python-defined class. We should do the same for the PEP 646 syntax.
All reactions

Sorry, something went wrong.

Uh oh!

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

@gvanrossum

gvanrossum commented Feb 2, 2022

Copy link
Copy Markdown
Member

Exactly what Jelle says.

All reactions

Sorry, something went wrong.

Uh oh!

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

JelleZijlstra
JelleZijlstra reviewed Feb 2, 2022
View reviewed changes
Comment thread Lib/test/test_typing.py 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.

@mrahtz
Update docstring for Unpack to reflect that it isn't just for backwar…
4a647cf
…ds compatibility now
@mrahtz

mrahtz commented Feb 6, 2022

Copy link
Copy Markdown
Contributor Author

@JelleZijlstra @gvanrossum Thanks for confirming re Unpack design. In that case, we'll stick with the plan where unpacking things from typing.py (that is, Tuple and TypeVarTuple) results in an Unpack[], and unpacking a native tuple results in a special version of the native tuple type that's starred.

All reactions

Sorry, something went wrong.

Uh oh!

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

mrahtz added 3 commits February 6, 2022 19:47
@mrahtz
Fix _is_unpacked_typevartuple for argument of Unpack[tuple[...]]
e4e68a6
@mrahtz
Add more tests for repr of variadic classes
7fb4043
@mrahtz
Add more tests to test_variadic_class_with_2_typevars_accepts_2_or_mo…
4b1b7f6
…re_args
@JelleZijlstra
Merge branch 'main' into pep646-typing
ad5b090
@JelleZijlstra

JelleZijlstra commented Mar 4, 2022

Copy link
Copy Markdown
Member

(Fixed a merge conflict)

All reactions

Sorry, something went wrong.

Uh oh!

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

mrahtz added 5 commits March 5, 2022 14:32
@mrahtz
Remove accidental cosmetic change
2604094
@mrahtz
Add some type annotations
75cd9d0
@mrahtz
Fix various things in Generic.__class_getitem__
23b393b
@mrahtz
Split up _determine_typevar_substitution
f094905
@mrahtz
Cut out variadic type substitution logic
b9b1c80
@mrahtz

mrahtz commented Mar 5, 2022

Copy link
Copy Markdown
Contributor Author

On the subject of what to do about type substitution, Pradeep had the excellent idea of moving discussion to a future PR, given how long even this PR has taken. For now I've excised all the extra logic required, instead raising NotImplementedError in the cases it means we currently don't support (re-purposing the corresponding tests so it's clear exactly which things we currently can't deal with).

For what it's worth, though, given that both Pradeep and Jelle lean towards thinking simple is better, for the follow-up PR, I'll try drafting a prototype of Jelle's suggestion: having the subscription operator just return a new GenericAlias.

All reactions

Sorry, something went wrong.

Uh oh!

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

@mrahtz

mrahtz commented Mar 5, 2022

Copy link
Copy Markdown
Contributor Author

P.S. bedevere-bot: I have made the requested changes; please review again.

All reactions

Sorry, something went wrong.

Uh oh!

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

@bedevere-bot

bedevere-bot commented Mar 5, 2022

Copy link
Copy Markdown

Thanks for making the requested changes!

@JelleZijlstra: please review the changes made to this pull request.

All reactions

Sorry, something went wrong.

Uh oh!

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

@bedevere-bot
bedevere-bot requested a review from JelleZijlstra March 5, 2022 16:11
JelleZijlstra
JelleZijlstra approved these changes Mar 5, 2022
View reviewed changes

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

Let's do it.

I'm planning to merge this PR once 3.11.0a6 is out (cc @gvanrossum @Fidget-Spinner).

Sorry, something went wrong.

Uh oh!

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

All reactions
@bedevere-bot bedevere-bot added awaiting merge and removed awaiting change review labels Mar 5, 2022
@JelleZijlstra
JelleZijlstra merged commit 7a793a3 into python:main Mar 8, 2022
@bedevere-bot bedevere-bot removed the awaiting merge label Mar 8, 2022
@JelleZijlstra

JelleZijlstra commented Mar 8, 2022

Copy link
Copy Markdown
Member

Congratulations!

All reactions

Sorry, something went wrong.

Uh oh!

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

@gvanrossum

gvanrossum commented Mar 8, 2022

Copy link
Copy Markdown
Member

Thanks @mrahtz for the code, and thanks @JelleZijlstra for the review. Thanks @pradeep90 for your help. This is a monumental change!

All reactions

Sorry, something went wrong.

Uh oh!

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

@mrahtz

mrahtz commented Mar 9, 2022

Copy link
Copy Markdown
Contributor Author

Fantastic! Thank you @JelleZijlstra and @pradeep90 for the review, and @gvanrossum for your continuing support :)

All reactions

Sorry, something went wrong.

Uh oh!

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

serhiy-storchaka
serhiy-storchaka reviewed Mar 10, 2022
View reviewed changes
Comment thread Lib/typing.py Resolved

Uh oh!

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

This was referenced Mar 10, 2022
bpo-43224: Work around substitution of unpacked TypeVarTuple #31804
Closed
bpo-43224: Implement substitution of unpacked TypeVarTuple #31800
Merged
@mrahtz
mrahtz deleted the pep646-typing branch March 13, 2022 10:14
@mrahtz mrahtz mentioned this pull request Mar 13, 2022
bpo-43224: Add tests for TypeVarTuple substitution in Annotated #31846
Merged
@JelleZijlstra JelleZijlstra mentioned this pull request Mar 17, 2022
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.
@mrahtz mrahtz mannequin mentioned this pull request Apr 11, 2022
Add support for PEP 646 #87390
Closed
@sobolevn sobolevn mentioned this pull request Sep 13, 2022
Tuple[...] is allowed since 3.11+ (gh-31021) #96792
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@serhiy-storchaka serhiy-storchaka serhiy-storchaka left review comments
@AlexWaygood AlexWaygood AlexWaygood left review comments
@JelleZijlstra JelleZijlstra JelleZijlstra approved these changes
@Fidget-Spinner Fidget-Spinner Awaiting requested review from Fidget-Spinner
@gvanrossum gvanrossum Awaiting requested review from gvanrossum
+1 more reviewer
@pradeep90 pradeep90 pradeep90 requested changes
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.

8 participants

@mrahtz @JelleZijlstra @pradeep90 @gvanrossum @bedevere-bot @serhiy-storchaka @AlexWaygood @the-knights-who-say-ni
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.