Skip to content

Bug: __class_getitem__ not recognised #14111

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

Closed
samsja opened this issue Nov 16, 2022 · 6 comments
Closed

Bug: __class_getitem__ not recognised #14111

samsja opened this issue Nov 16, 2022 · 6 comments
Labels
bug mypy got something wrong

Comments

@samsja
Copy link

samsja commented Nov 16, 2022

Bug Report

I am experienced a very weird behavior (which I am now pretty sure is a bug) with __class_getitem__.

To Reproduce

from abc import abstractmethod
from typing import Iterable


class A(Iterable):
    @abstractmethod
    def __class_getitem__(cls, item):
        ...


class B(A):
    def f(self, field: str):

        self.__class__[str]

>>> attribute.py:14: error: Value of type "Type[B]" is not indexable  [index]

Here I expect that mypy know that __class__ is bounded by the class B and therefore implement __class_getitem__.

The weirdest part is that removing the parameter in f remove the problem

from abc import abstractmethod
from typing import Iterable


class A(Iterable):
    @abstractmethod
    def __class_getitem__(cls, item):
        ...


class B(A):
    def f(self):

        self.__class__[str]

>>> Success: no issues found in 1 source file

Expected Behavior

mypy recognize that class can be indexed

Actual Behavior

mypy said it is no indexable even though B implement __class_getitem__

Your Environment

  • Mypy version used: 0.990

  • Mypy command-line flags: None

  • Mypy configuration options from mypy.ini (and other config files): plugins = "pydantic.mypy"

  • Python version used: 3.9

FYI: a more advance setup in this PR docarray/docarray#803

@samsja samsja added the bug mypy got something wrong label Nov 16, 2022
@tmke8
Copy link
Contributor

tmke8 commented Nov 17, 2022

Duplicate of #11501 I believe.

@AlexWaygood
Copy link
Member

Duplicate of #11501 I believe.

Yup. Thanks!

@AlexWaygood AlexWaygood closed this as not planned Won't fix, can't repro, duplicate, stale Nov 17, 2022
@samsja
Copy link
Author

samsja commented Nov 17, 2022

I am not sure it is totally a duplicate. Did you see that if I don't have a parameters in my function mypy does not complain anymore ?
This work

    def f(self):
        self.__class__[str]

This does not

    def f(self, field: str):
        self.__class__[str]

Why is the behavior changing ?

@AlexWaygood
Copy link
Member

The fundamental issue — that __class_getitem__ is an implementation detail of the typing system at runtime, which mypy has no special recognition of at present — is the same.

@AlexWaygood
Copy link
Member

AlexWaygood commented Nov 17, 2022

Did you see that if I don't have a parameters in my function mypy does not complain anymore ?

Oh, that's because your "function without parameters" is unannotated, meaning mypy just ignores it. It's a common footgun :)

If you want mypy to check that function, add -> None to the signature. Once you've done that, mypy emits an error for the version without parameters as well.

@samsja
Copy link
Author

samsja commented Nov 17, 2022

Oh I see ! Then yes it is a duplicate. Thanks for the clarification

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

3 participants