Skip to content

Fix __class_getitem__ to be positional-only#3410

Merged
damusss merged 1 commit into
mainfrom
ankith26-fix-class-getitem
Apr 26, 2025
Merged

Fix __class_getitem__ to be positional-only#3410
damusss merged 1 commit into
mainfrom
ankith26-fix-class-getitem

Conversation

@ankith26
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Member

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