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

Virtual subclasses of abstract base classes #1459

Closed
jtatum opened this issue May 1, 2016 · 5 comments
Closed

Virtual subclasses of abstract base classes #1459

jtatum opened this issue May 1, 2016 · 5 comments
Labels
bug mypy got something wrong needs discussion

Comments

@jtatum
Copy link

jtatum commented May 1, 2016

Seeing a couple of different errors with virtual subclasses:

from abc import ABCMeta
class M(metaclass=ABCMeta):
    pass
class A:
    pass
M.register(A)  # error: "M" has no attribute "register"
def test(obj: M):
    pass
obj = A()
test(obj)  # error: Argument 1 to "test" has incompatible type "A"; expected "M"
@gvanrossum
Copy link
Member

ABC.register() is not supported and it's unclear to me how it could be supported without a relatively big change to mypy. It probably would require an extra pass over the code looking for register() calls everywhere.

The only thing that's easy to do is to add register() to the stubs in typeshed -- can you send a PR for that?

@jtatum
Copy link
Author

jtatum commented May 1, 2016

Hmm, there might be another issue with metaclasses that prevents me from adding register to typeshed:

class M(type):
    def test(cls):
        pass
class C(metaclass=M):
    pass
C.test()  # error: "C" has no attribute "test"

@gvanrossum
Copy link
Member

Ohh... Yes, that looks like we have a problem with metaclasses.

@jsoref
Copy link
Contributor

jsoref commented Nov 20, 2019

FWIW, this is a pretty common/frustrating bug in the Python ecosystem, here's the equivalent IntelliJ/PyCharm issue.

@AlexWaygood
Copy link
Member

AlexWaygood commented Mar 31, 2022

For ABCMeta.register() support, see #2922 (newer issue but has more discussion than this one).

For generalised metaclass support, this example no longer reproduces on mypy 0.941.

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

No branches or pull requests

4 participants