8000 bpo-41559: Change PEP 612 implementation to pure Python by Fidget-Spinner · Pull Request #25449 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-41559: Change PEP 612 implementation to pure Python #25449

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

Merged
merged 9 commits into from
Apr 28, 2021

Conversation

Fidget-Spinner
Copy link
Member
@Fidget-Spinner Fidget-Spinner commented Apr 17, 2021
  • Reverted changes in genericaliasobject.c to commit 463c7d3 (right before PEP 612) via a clean git checkout 463c7d3d149283814d879a9bb8411af64e656c8e -- genericaliasobject.c.
  • Implemented PEP 612 behavior only in collections.abc.Callable in pure Python.

PR 1/2, another update to typing.py is coming later, but that's not urgent.

The intention of this change is to conform more strictly to PEP 612. Specifically, this snippet:

As before, parameters_expressions by themselves are not acceptable in places where a type is expected

https://www.python.org/dev/peps/pep-0612/#valid-use-locations

The current implementation leaks extra behavior into all builtin GenericAlias types just for the sake of collections.abc.Callable, which is out of the scope of the PEP. Since there is no need to support ParamSpec in other places, it's valid to not have them in __parameters__ of all builtin generics. except for collections.abc.Callable.

This will also speed up builtin GenericAlias as it has to do less checks, and reduce the amount of confusing code in GenericAlias (though it defers that to collections.abc.Callable.)

https://bugs.python.org/issue41559

@Fidget-Spinner
Copy link
Member Author

@rhettinger sorry to ping you; would you like me to remove your request for review for future typing-only changes to _collections_abc or would you prefer otherwise?

@gvanrossum gvanrossum self-requested a review April 17, 2021 04:20
@Fidget-Spinner
Copy link
Member Author
Fidget-Spinner commented Apr 27, 2021

@gvanrossum Guido, I have the feeling that you're really busy with many review requests right now. So I apologise for the ping and I don't mean to rush. I just have a few pointers to help make your reviewing easier:

  • You can probably skim the C changes, they were created directly by git checkout 463c7d3d149283814d879a9bb8411af64e656c8e -- genericaliasobject.c (ie revert to older code), so unless you're unhappy with the old code, there shouldn't be much work there.
  • Most of the substitution code is a direct copy from typing.py 's _GenericAlias., or a direct re-implementation of the C code in Python with tweaks for ParamSpec.

BTW, if you feel you're way too overwhelmed right now, maybe we could ping someone else (eg. Jelle?) to get this reviewed if they have more leeway?

Thanks for your time. I hope you enjoy your well-deserved break after beta lands.
Off-topic: "Beta freeze" seems like a contradiction -- Nobody's feeling cold and everyone's feeling the heat now 😅

@gvanrossum
Copy link
Member
gvanrossum commented Apr 27, 2021 via email

@Fidget-Spinner
Copy link
Member Author

No worries Guido. @JelleZijlstra, sorry may I summon you for a review please?

@JelleZijlstra
Copy link
Member

I'll try to take a look by end of today.

@Fidget-Spinner
Copy link
Member Author

I'll try to take a look by end of today.

Thank you so much!

Copy link
Member
@JelleZijlstra JelleZijlstra left a comment

Choose a reason for hiding this comment

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

Mostly looks good. I have one suggestion for improving the code and I'd like to see some more tests.

It took me a while to realize this, but it looks like the observable effect of the change is that while on current master this works:

>>> P = ParamSpec("P")
>>> T = TypeVar("T")
>>> list[T][int]
list[int]
>>> list[P][int]
list[int]

This PR instead makes it so that P is not recognized as a TypeVar-like by the implementation of list[int], so the last line will instead throw TypeError: There are no type variables left in list[~P].

@Fidget-Spinner
Copy link
Member Author
Fidget-Spinner commented Apr 28, 2021

This PR instead makes it so that P is not recognized as a TypeVar-like by the implementation of list[int], so the last line will instead throw TypeError: There are no type variables left in list[~P].

Jelle, thank you for reviewing this on such short notice.

Yes that is indeed what I intended. Perhaps I wasn't clear enough in the news item and I should mention this is a breaking change from earlier versions of 3.10 alpha.

list[P] is invalid according to PEP 612. ParamSpec is only valid in Concatenate, Callable and Generic. IMO there isn't a need to support behavior explictly stated as invalid in the PEP.

As before, parameters_expressions by themselves are not acceptable in places where a type is expected

https://www.python.org/dev/peps/pep-0612/#valid-use-locations

Re: the tests, I agree, I'll add them in soon.

@Fidget-Spinner
Copy link
Member Author
Fidget-Spinner commented Apr 28, 2021

Yay, the tests are now all green. @gvanrossum, Jelle has reviewed and approved this PR. Would you like for me to request for any additional reviewers?

Edit: Oops sorry, I misspelled Jelle's name.

@gvanrossum gvanrossum merged commit 859577c into python:master Apr 28, 2021
@bedevere-bot
Copy link

@gvanrossum: Please replace # with GH- in the commit message next time. Thanks!

@gvanrossum
Copy link
Member

Thanks Ken Jin for the PR, and thanks Jelle for the review!!

@Fidget-Spinner
Copy link
Member Author
Fidget-Spinner commented Apr 28, 2021

My pleasure. A huge thanks to Jelle for the review (I don't think this could've landed without him) and thanks Guido for the merge!

@Fidget-Spinner Fidget-Spinner deleted the pep612-python branch May 1, 2021 15:25
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
0