Skip to content

Fix __class_getitem__ to be positional-only #3410

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 1 commit into from
Apr 26, 2025
Merged

Conversation

ankith26
Copy link
Member

As noted by @aatle in #3398, __class_getitem__ is positional only, and stubtest on python 3.13 complains about this issue. This PR fixes that.

@ankith26 ankith26 requested a review from a team as a code owner April 21, 2025 10:01
@gresm
Copy link
Contributor

gresm commented Apr 22, 2025

I've looked around and failed to find information about key parameter being required a positional-only:
PEP-560: original spec
Python documentation
Possible alternative that it's Generic stuff instead - also negative

Edit:
The cause is __class_getitem__ = classmethod(types.GenericAlias) line. What I said above is true, but when instantiated, GenericAliasexpects a positional-only argument. An alternative is to just subclass AbstractClass from a generic in sprite.py like stub pretends (I wonder why there's no warning about that...)

Copy link
Contributor

@aatle aatle left a comment

Choose a reason for hiding this comment

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

If you would like, you can rename generic to item to be consistent with typeshed.

LGTM;

@ankith26 ankith26 force-pushed the ankith26-fix-class-getitem branch from ac8c795 to e76176b Compare April 24, 2025 06:18
@ankith26
Copy link
Member Author

I updated the code to use item instead of generic as per the review.

I also want to keep code changes out of this stubs PR so I am not changing the parent classes in the implementation, though someone could take it up in the future if interested.

@Starbuck5 Starbuck5 added the type hints Type hint checking related tasks label Apr 26, 2025
@Starbuck5
Copy link
Member

I played around a bit with list's implementation, it being positional only seems to be standard.

>>> list.__class_getitem__(key=int)
Traceback (most recent call last):
  File "<python-input-9>", line 1, in <module>
    list.__class_getitem__(key=int)
    ~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^
TypeError: list.__class_getitem__() takes no keyword arguments

Also ofc class getitem is meant to be used by type hints, and trying to use a keyword there is a syntax error.

>>> list[key=int]
  File "<python-input-5>", line 1
    list[key=int]
         ^^^^^^^
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?

@Starbuck5 Starbuck5 added this to the 2.5.4 milestone Apr 26, 2025
Copy link
Member

@damusss damusss left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@damusss damusss merged commit be3d05a into main Apr 26, 2025
26 checks passed
@ankith26 ankith26 deleted the ankith26-fix-class-getitem branch April 26, 2025 10:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type hints Type hint checking related tasks
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants