Skip to content

Impossible to specify a metaclass for a Generic class #3720

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
rowillia opened this issue Jul 14, 2017 · 2 comments
Closed

Impossible to specify a metaclass for a Generic class #3720

rowillia opened this issue Jul 14, 2017 · 2 comments

Comments

@rowillia
Copy link
Contributor

rowillia commented Jul 14, 2017

Not sure if this issue should go here or in https://github.com/python/typing

Below is a contrived example. In the real world I might not actually control the metaclass for Foo or I may be inheriting from some other class that also has a metaclass.

from typing import Generic, TypeVar

class FooMeta(type):
    pass

_T = TypeVar('_T')

class Foo(Generic[_T], metaclass=FooMeta):
    pass

This passes MyPy just fine, but fails at runtime with:

$ python3.6 test_generics.py
Traceback (most recent call last):
  File "test_generics.py", line 8, in <module>
    class Foo(Generic[_T], metaclass=FooMeta):
TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases
@ilevkivskyi
Copy link
Member

I think this should be in typing rather than here. (Also this is unfortunately not an easy problem.)

@rowillia
Copy link
Contributor Author

Thanks @ilevkivskyi , moving over there.

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

No branches or pull requests

2 participants