You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Use positional only arguments for dunder methods in collections.abc #14071
Currently, some dunder methods like Sequence.__getitem__ do not use positional-only arguments, which causes errors when trying to subclass with positional-only arguments. Therefore, dunder methods, especially operators like __getitem__, that are typically not called using keyword arguments, should probably be annotated using positional only arguments in the stubs.
While I do agree that these should be positional only, this should be done in CPython first. If the Python maintainers agree that this is a good idea, we can implement it in typeshed – and I'd be okay to do this for all Python versions in typeshed.
This is different with mypy. In mypy, dunder methods are implicitly positional-only, and your example type-checks without errors as is. I'm tempted to say that mypy is correct here, and this is a pyright problem, but I don't feel very strongly about it.
However, I do want to point out that the current stubs can actually lead to some false negatives. For instance, list does not allow calling __getitem__ with index as a keyword. Code sample in pyright playground
Activity
srittau commentedon May 15, 2025
While I do agree that these should be positional only, this should be done in CPython first. If the Python maintainers agree that this is a good idea, we can implement it in typeshed – and I'd be okay to do this for all Python versions in typeshed.
Akuli commentedon May 15, 2025
This is different with mypy. In mypy, dunder methods are implicitly positional-only, and your example type-checks without errors as is. I'm tempted to say that mypy is correct here, and this is a pyright problem, but I don't feel very strongly about it.
randolf-scholz commentedon Jun 9, 2025
I opened python/cpython#135312.
However, I do want to point out that the current stubs can actually lead to some false negatives. For instance,
listdoes not allow calling__getitem__withindexas a keyword. Code sample in pyright playgroundAlso, I noticed that the stubs already use positional-only sometimes when CPython doesn't, for instance with
Container.__contains__.