python / python/mypy

"Dynamic metaclass not supported" false positive on generics

Open
#11,672 8 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-metaclasses
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

from typing import TypeVar, Generic

T = TypeVar("T")

class FooMetaclass(type, Generic[T]): pass

class Foo(metaclass=FooMetaclass[T]): # error: Dynamic metaclass not supported for "Foo"
    pass

https://mypy-play.net/?mypy=master&python=3.10&gist=d4b169a5ece08ed0607b19fff8e9b749

according to the documentation:

Mypy does not understand dynamically-computed metaclasses, such as class A(metaclass=f()): ...

however this isn't dynamically computed, it's just a generic so mypy should be able to understand it statically

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the example in the linked mypy-play case and reading the metaclass-support documentation, especially its limitations. Trace how the generic metaclass expression is classified versus a dynamically computed metaclass. Done means this example no longer reports the false positive, while genuinely dynamic metaclasses retain their documented behavior and regression coverage is added.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers, devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.