Skip to content

Metaclasses should inherit from type. #2818

Closed

Description

@ilevkivskyi

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.

Activity

  1. elazarg commented on Feb 7, 2017

    @elazarg
    Contributor

    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?

  2. ilevkivskyi commented on Feb 7, 2017

    @ilevkivskyi
    MemberAuthor

    @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.

  3. elazarg commented on Feb 7, 2017

    @elazarg
    Contributor

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

  4. added a commit that references this issue on Feb 7, 2017
    452a64f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions