Skip to content

Inconsistent "Invalid Metaclass" error for classes defined within functions #15027

Open
@nathanjmcdougall

Description

@nathanjmcdougall

Bug Report

When a class:

  • is defined inside a function, and
  • uses a metaclass

then it mypy will inconsistently give an Invalid metaclass error based on whether the metaclass is specified via an alias name, or the original name from when the class is defined. This behaviour occurs regardless of whether the metaclass is defined inside or outside of the function. I would have expected no error.

To Reproduce

See on mypy playground

class M(type):
    pass


def f() -> None:
    _M = M

    class C(metaclass=_M): # Error
        pass
    
    class D(metaclass=M): # OK
        pass

Actual Behavior

main.py:8: error: Invalid metaclass "_M"  [misc]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.1.1
  • Python version used: 3.11

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions