Skip to content

Metaclasses should inherit from type. #2818

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
ilevkivskyi opened this issue Feb 7, 2017 · 3 comments
Closed

Metaclasses should inherit from type. #2818

ilevkivskyi opened this issue Feb 7, 2017 · 3 comments

Comments

@ilevkivskyi
Copy link
Member

There is something missing in #2475: this code:

class M:
    ...
class A(metaclass=M):
    ...

Passes without errors, but fails at runtime.


Btw, I have found two open issues that were fixed by #2475, I left comments referencing the #2475 in that issues.

@elazarg
Copy link
Contributor

elazarg commented Feb 7, 2017

I am for fixing it. The reason I have allowed this behavior is the documentation.

Quote:

The appropriate metaclass for a class definition is determined as follows:

  • if no bases and no explicit metaclass are given, then type() is used
  • if an explicit metaclass is given and it is not an instance of type(), then it is used directly as the metaclass
  • if an instance of type() is given as the explicit metaclass, or bases are defined, then the most derived metaclass is used

End quote.

What do you think?

@ilevkivskyi
Copy link
Member Author

@elazarg I think the use case where metaclass is not an instance of type is quite rare, and (more importantly) this case could be difficult to typecheck. I think mypy could give an error message like: Metaclasses not inheriting from "type" are not yet supported.

@elazarg
Copy link
Contributor

elazarg commented Feb 7, 2017

I have a branch ready , but it requires multiple changes to typeshed before it can be merged.

JukkaL pushed a commit that referenced this issue Feb 7, 2017
Fix #2818.

ABCMeta requires special treatment.

Related: #2475
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