MCPcopy Index your code
hub / github.com/python/mypy / _verify_metaclass

Function _verify_metaclass

mypy/stubtest.py:606–641  ·  view source on GitHub ↗
(
    stub: nodes.TypeInfo, runtime: type[Any], object_path: list[str], *, is_runtime_typeddict: bool
)

Source from the content-addressed store, hash-verified

604
605
606def _verify_metaclass(
607 stub: nodes.TypeInfo, runtime: type[Any], object_path: list[str], *, is_runtime_typeddict: bool
608) -> Iterator[Error]:
609 # We exclude protocols, because of how complex their implementation is in different versions of
610 # python. Enums are also hard, as are runtime TypedDicts; ignoring.
611 # TODO: check that metaclasses are identical?
612 if not stub.is_protocol and not stub.is_enum and not is_runtime_typeddict:
613 runtime_metaclass = type(runtime)
614 if runtime_metaclass is not type and stub.metaclass_type is None:
615 # This means that runtime has a custom metaclass, but a stub does not.
616 yield Error(
617 object_path,
618 "is inconsistent, metaclass differs",
619 stub,
620 runtime,
621 stub_desc="N/A",
622 runtime_desc=f"{runtime_metaclass}",
623 )
624 elif (
625 runtime_metaclass is type
626 and stub.metaclass_type is not None
627 # We ignore extra `ABCMeta` metaclass on stubs, this might be typing hack.
628 # We also ignore `builtins.type` metaclass as an implementation detail in mypy.
629 and not mypy.types.is_named_instance(
630 stub.metaclass_type, ("abc.ABCMeta", "builtins.type")
631 )
632 ):
633 # This means that our stub has a metaclass that is not present at runtime.
634 yield Error(
635 object_path,
636 "metaclass mismatch",
637 stub,
638 runtime,
639 stub_desc=f"{stub.metaclass_type.type.fullname}",
640 runtime_desc="N/A",
641 )
642
643
644@verify.register(nodes.TypeInfo)

Callers 1

verify_typeinfoFunction · 0.85

Calls 2

typeClass · 0.85
ErrorClass · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…