Skip to content

classmethod and selftype: incompatible with supertype #2511

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
thomascellerier opened this issue Nov 30, 2016 · 6 comments · Fixed by #7474
Closed

classmethod and selftype: incompatible with supertype #2511

thomascellerier opened this issue Nov 30, 2016 · 6 comments · Fixed by #7474
Assignees
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-0-high

Comments

@thomascellerier
Copy link

I am defining some classes capable of returning an (optional) instance of themselves based on a configuration (a dictionary).

In the base class I use the new SelfType capability to define the generic classmethod. I wanted to use a metaclass with abc.abstractclassmethod but mypy doesn't seem to support that.

In the subclass I now use the narrower type for cls and the return value but mypy gives me an error:

$ ./virtualenv/bin/python3 -m mypy --version
mypy 0.4.6
$ ./virtualenv/bin/python3 -m mypy repro.py 
repro.py: note: In class "B":
repro.py:15: error: Return type of "from_config" incompatible with supertype "A"

repro.py:

from typing import TypeVar, Type, Optional

AT = TypeVar('AT', bound='A')

class A:
    @classmethod 
    def from_config(cls: Type[AT], config: dict) -> Optional[AT]:
        return None

class B(A):
    @classmethod
    def from_config(cls: Type[B], config: dict) -> Optional[B]:
        return B()

I could only make it work by defining B like this which looks quite ugly and requires passing around the type variable:

from typing import cast

class B(A):
    @classmethod
    def from_config(cls: Type[AT], config: dict) -> Optional[AT]:
        return cast(AT, B())

Is that the intended result?

@JukkaL
Copy link
Collaborator

JukkaL commented Nov 30, 2016

This is a known issue with self types: #2353

@JukkaL
Copy link
Collaborator

JukkaL commented Nov 30, 2016

Closed as duplicate.

@ilevkivskyi
Copy link
Member

I think this is not a duplicate of #2353. In that issue mypy allows unsafe override, while here mypy prohibits a safe override. Also this example is specific to @classmethod, with normal self type it works. Then it is probably easier to fix.

@ilevkivskyi ilevkivskyi reopened this Nov 7, 2018
@ilevkivskyi ilevkivskyi added bug mypy got something wrong priority-1-normal false-positive mypy gave an error on correct code labels Nov 7, 2018
@dsully
Copy link

dsully commented Mar 29, 2019

Still seeing this with mypy 0.670

@JukkaL
Copy link
Collaborator

JukkaL commented Aug 29, 2019

#7401 has a slightly different example (no annotation for cls in the method override).

@ilevkivskyi
Copy link
Member

#7466 looks like yet another duplicate. Raising priority to high, since this started to come up more often and a fix might be not hard.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-0-high
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants