Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[typing] Make arguments to __getitem__/__setitem__/ etc. positional only. #125390

Open
randolf-scholz opened this issue May 2, 2024 · 1 comment
Labels
module: typing Related to mypy type annotations triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Comments

@randolf-scholz
Copy link
Contributor

randolf-scholz commented May 2, 2024

馃殌 The feature, motivation and pitch

A strict type checker will complain when overwriting __getitem__ with a different argument:

from torch.utils.data import Dataset

class MyDatasets(Dataset):
    def __getitem__(self, key, /) -> int:
        return 0

Gives: Parameter 2 mismatch: base parameter "index" is keyword parameter, override parameter is position-only when checking with pyright.

Note: most type-checkers support the white lie that if the parent class defines __getitem__(self, index), then a subclass could do __getitem__(self, key), despite this not being type save. This could change in the future as more libraries adopt positional only parameters.

Additional context

Such changes are backward-compatibility braking if a caller tries to execute something like A.__getitem__(self=self, index=index).

cc @ezyang @malfet @xuzhao9 @gramster

@randolf-scholz randolf-scholz changed the title [typing] Make arguments to __getitem__/__setitem__ arguments positional only. [typing] Make arguments to __getitem__/__setitem__ positional only. May 2, 2024
@randolf-scholz randolf-scholz changed the title [typing] Make arguments to __getitem__/__setitem__ positional only. [typing] Make arguments to __getitem__/__setitem__/ etc. positional only. May 2, 2024
@drisspg drisspg added module: typing Related to mypy type annotations triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module labels May 3, 2024
@ezyang
Copy link
Contributor

ezyang commented May 3, 2024

Sgtm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module: typing Related to mypy type annotations triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module
Projects
None yet
Development

No branches or pull requests

3 participants