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

Method prepare_typing_namespace

mypy/semanal.py:610–639  ·  view source on GitHub ↗

Remove dummy alias definitions such as List = TypeAlias(object) from typing. They will be replaced with real aliases when corresponding targets are ready.

(self, file_node: MypyFile, aliases: dict[str, str])

Source from the content-addressed store, hash-verified

608 self.prepare_typing_namespace(file_node, typing_extensions_aliases)
609
610 def prepare_typing_namespace(self, file_node: MypyFile, aliases: dict[str, str]) -> None:
611 """Remove dummy alias definitions such as List = TypeAlias(object) from typing.
612
613 They will be replaced with real aliases when corresponding targets are ready.
614 """
615
616 # This is all pretty unfortunate. typeshed now has a
617 # sys.version_info check for OrderedDict, and we shouldn't
618 # take it out, because it is correct and a typechecker should
619 # use that as a source of truth. But instead we rummage
620 # through IfStmts to remove the info first. (I tried to
621 # remove this whole machinery and ran into issues with the
622 # builtins/typing import cycle.)
623 def helper(defs: list[Statement]) -> None:
624 for stmt in defs.copy():
625 if isinstance(stmt, IfStmt):
626 for body in stmt.body:
627 helper(body.body)
628 if stmt.else_body:
629 helper(stmt.else_body.body)
630 if (
631 isinstance(stmt, AssignmentStmt)
632 and len(stmt.lvalues) == 1
633 and isinstance(stmt.lvalues[0], NameExpr)
634 ):
635 # Assignment to a simple name, remove it if it is a dummy alias.
636 if f"{file_node.fullname}.{stmt.lvalues[0].name}" in aliases:
637 defs.remove(stmt)
638
639 helper(file_node.defs)
640
641 def prepare_builtins_namespace(self, file_node: MypyFile) -> None:
642 """Add certain special-cased definitions to the builtins module.

Callers 1

prepare_fileMethod · 0.95

Calls 1

helperFunction · 0.85

Tested by

no test coverage detected