Skip to content

__class_getitem__ is not assumed to be a classmethod #7319

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
keysmashes opened this issue Aug 11, 2019 · 5 comments · Fixed by #7647
Closed

__class_getitem__ is not assumed to be a classmethod #7319

keysmashes opened this issue Aug 11, 2019 · 5 comments · Fixed by #7647
Labels

Comments

@keysmashes
Copy link

mypy (version 0.720) gives confusing errors like this:

error: The erased type of self "Type[_Type]" is not a supertype of its class "_Type"

for the following method:

_TypeT = TypeVar("_TypeT", bound="_Type")
class _Type:
    def __class_getitem__(cls: Type[_TypeT], item: Any) -> _TypeT:
        raise NotImplementedError

even though this is apparently the correct way to type-hint factory functions.

Adding the @classmethod decorator makes the error go away, but according to PEP 560, __class_getitem__ is automatically a class method, so the decorator shouln't be required.

@ilevkivskyi ilevkivskyi added bug mypy got something wrong priority-2-low labels Aug 12, 2019
@ilevkivskyi
Copy link
Member

Yes, we should recognise it as a class method automatically, this is however quite low priority IMO, because it is a rarely used method.

@ilevkivskyi
Copy link
Member

On the other hand it should be easy to fix, so please go ahead if you would like to make a PR.

@keysmashes
Copy link
Author

I think we have different definitions of "easy" :)

I pushed a few commits to my fork, but I get some errors in the tests I added, which I have no idea how to fix, since I basically just copied bits of what is special-cased for __init_subclass__ (which is also automatically a classmethod), ignoring the parts about setting the return type to None; I probably don't have the brainspace to work out how to fix this in the near future, sorry.

@ilevkivskyi
Copy link
Member

I think we have different definitions of "easy" :)

I have only one scale, it is much easier than any other mypy issue.

Btw a quick fix for your issue will be to tweak shared_parse.special_function_elide_names() so that it will preserve cls argument name for __class_getitem__. Or maybe a better solution would be to always keep the name of first argument.

@tavianator
Copy link
Contributor

I think something similar to #7355 would work for this.

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

Successfully merging a pull request may close this issue.

3 participants