MCPcopy
hub / github.com/python/mypy / verify_typealias

Function verify_typealias

mypy/stubtest.py:1687–1753  ·  view source on GitHub ↗
(
    stub: nodes.TypeAlias, runtime: MaybeMissing[Any], object_path: list[str]
)

Source from the content-addressed store, hash-verified

1685
1686@verify.register(nodes.TypeAlias)
1687def verify_typealias(
1688 stub: nodes.TypeAlias, runtime: MaybeMissing[Any], object_path: list[str]
1689) -> Iterator[Error]:
1690 stub_target = mypy.types.get_proper_type(stub.target)
1691 stub_desc = f"Type alias for {stub_target}"
1692 if isinstance(runtime, Missing):
1693 yield Error(object_path, "is not present at runtime", stub, runtime, stub_desc=stub_desc)
1694 return
1695 runtime_origin = get_origin(runtime) or runtime
1696 if isinstance(stub_target, mypy.types.Instance):
1697 if not isinstance(runtime_origin, type):
1698 yield Error(
1699 object_path,
1700 "is inconsistent, runtime is not a type",
1701 stub,
1702 runtime,
1703 stub_desc=stub_desc,
1704 )
1705 return
1706
1707 stub_origin = stub_target.type
1708 # Do our best to figure out the fullname of the runtime object...
1709 runtime_name: object
1710 try:
1711 runtime_name = runtime_origin.__qualname__
1712 except AttributeError:
1713 runtime_name = getattr(runtime_origin, "__name__", MISSING)
1714 if isinstance(runtime_name, str):
1715 runtime_module: object = getattr(runtime_origin, "__module__", MISSING)
1716 if isinstance(runtime_module, str):
1717 if runtime_module == "collections.abc" or (
1718 runtime_module == "re" and runtime_name in {"Match", "Pattern"}
1719 ):
1720 runtime_module = "typing"
1721 runtime_fullname = f"{runtime_module}.{runtime_name}"
1722 if re.fullmatch(rf"_?{re.escape(stub_origin.fullname)}", runtime_fullname):
1723 # Okay, we're probably fine.
1724 return
1725
1726 # Okay, either we couldn't construct a fullname
1727 # or the fullname of the stub didn't match the fullname of the runtime.
1728 # Fallback to a full structural check of the runtime vis-a-vis the stub.
1729 yield from verify_typeinfo(stub_origin, runtime_origin, object_path, is_alias_target=True)
1730 return
1731 if isinstance(stub_target, mypy.types.UnionType):
1732 # complain if runtime is not a Union or UnionType
1733 if runtime_origin is not Union and not isinstance(runtime, types.UnionType):
1734 yield Error(object_path, "is not a Union", stub, runtime, stub_desc=str(stub_target))
1735 # could check Union contents here...
1736 return
1737 if isinstance(stub_target, mypy.types.TupleType):
1738 if tuple not in getattr(runtime_origin, "__mro__", ()):
1739 yield Error(
1740 object_path, "is not a subclass of tuple", stub, runtime, stub_desc=stub_desc
1741 )
1742 # could check Tuple contents here...
1743 return
1744 if isinstance(stub_target, mypy.types.CallableType):

Callers

nothing calls this directly

Calls 5

isinstanceFunction · 0.85
ErrorClass · 0.85
getattrFunction · 0.85
verify_typeinfoFunction · 0.85
strClass · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…