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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inheriting type hints from abstract parents #8492

Closed
MaxHalford opened this issue Mar 4, 2020 · 1 comment
Closed

Inheriting type hints from abstract parents #8492

MaxHalford opened this issue Mar 4, 2020 · 1 comment

Comments

@MaxHalford
Copy link

MaxHalford commented Mar 4, 2020

I have a usecase where I have a few interfaces which are classes that each inherit from abc.ABC. Each interface defines a set of abstract methods to implement in order to satisfy said interface. I then have several hundred classes that inherit from one of these interfaces. I like this because the interfaces give a clear view of the API. I would really like it if the type hints that I define in my interfaces apply to the classes that inherit from them.

Basically, I would like mypy to raise an error for the following script:

import abc


class Foo(abc.ABC):

    @abc.abstractmethod
    def speak(self, msg: str) -> None:
        pass


class Bar(Foo):

    def speak(self, msg):
        print(msg)


b = Bar()
b.speak(1)  # not a str!

I'm aware that I can copy/paste the type hint from Foo.speak into Bar.speak, but I find that inelegant, error-prone, and less efficient from a code maintenance perspective.

Sorry if this is a duplicate; I tried to search for a similar issue but didn't find one. Feel free to point me to a relevant issue and to close this issue if so.

@ilevkivskyi
Copy link
Member

This is essentially a duplicate of #3903

Note there was an earlier attempt to fix it in #7548

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

No branches or pull requests

2 participants