[red-knot] Either support metaclasses that are not subclasses of type
, or emit a diagnostic when encountering them
#14208
Labels
red-knot
Multi-file analysis & type inference
Believe it or not, Python accepts arbitrary callables as metaclasses in
class
statements -- and those callables don't have to return instances oftype
. As long as the callable accepts the same arguments astype.__new__
, it's an acceptable metaclass; the binding created by the class statement is set to whatever calling the metaclass returns:Currently we don't understand this in red-knot: we think that the
class A
statement constitutes a class definition, when it does not. Mypy "solves" this problem by emitting a diagnostic saying that a dynamic metaclass like this isn't supported. Pyright doesn't emit a diagnostic, and just gets it wrong.I think mypy's "solution" here is acceptable (metaclasses that are not subclasses of
type
are really very rare). But it also might not be too much work for us to just add an accurate understanding of the runtime semantics here, and infer that theclass A
statement binds theA
symbol to an instance ofint
rather than a newly created class.The text was updated successfully, but these errors were encountered: