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

Applying Metaclass to int-derived classes gives 'Inconsistent metaclass structure' error. #3563

Closed
isundaylee opened this issue Jun 16, 2017 · 5 comments

Comments

@isundaylee
Copy link

On both 0.510-dev and 0.520-dev-94e3f9cfc2510e651a0147de2626e084a7433f6d, I have been running into the following error:

error: Inconsistent metaclass structure for 'BitMask'

when trying to apply a metaclass to a class derived from int (or float). Repro:

class BitMaskMeta(type):
    pass

class BitMask(int, metaclass=BitMaskMeta):
    pass

When I change BitMask to derive from object the error goes away. Is this the expected behavior? Or a potential BUG?

Thank you! :)

@ilevkivskyi
Copy link
Member

Thanks for reporting!

The problem that int has SupportsAbs, SupportsInt, etc. in bases in typeshed, this creates a spurious metaclass conflict (since the latter are instances of ABCMeta). This will be super-simple to fix after protocols land (also in typeshed) by just removing those bases.

In the meantime, maybe @elazarg knows some quick workaround?

@elazarg
Copy link
Contributor

elazarg commented Jun 19, 2017

I think inheriting from ABCMeta is a working hack

from abc import ABCMeta
class BitMaskMeta(ABCMeta):
    pass

class BitMask(int, metaclass=BitMaskMeta):
    pass

Alternatively, you can use # type: ignore:

class BitMask(int, metaclass=BitMaskMeta): # type: ignore
    pass

@elazarg
Copy link
Contributor

elazarg commented Sep 15, 2017

python/typeshed#1595

@viniciusd
Copy link

I think inheriting from ABCMeta is a working hack

from abc import ABCMeta
class BitMaskMeta(ABCMeta):
    pass

class BitMask(int, metaclass=BitMaskMeta):
    pass

Tried that in a similar case, but it didn't work. Going with type ignore for now.

@ilevkivskyi
Copy link
Member

ilevkivskyi commented Sep 21, 2019

This is essentially a duplicate of the typeshed issue (or rather the other way around, but the typeshed one has more discussion). I think it doesn't make sense to keep both open.

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

4 participants