diff --git a/mypy/semanal.py b/mypy/semanal.py index ce88d033e01c..b1077a5fd17d 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -597,9 +597,12 @@ def refresh_top_level(self, file_node: MypyFile) -> None: def add_implicit_module_attrs(self, file_node: MypyFile) -> None: """Manually add implicit definitions of module '__name__' etc.""" + str_type: Type | None = self.named_type_or_none("builtins.str") + if str_type is None: + str_type = UnboundType("builtins.str") for name, t in implicit_module_attrs.items(): if name == "__doc__": - typ: Type = UnboundType("__builtins__.str") + typ: Type = str_type elif name == "__path__": if not file_node.is_package_init_file(): continue @@ -612,7 +615,7 @@ def add_implicit_module_attrs(self, file_node: MypyFile) -> None: if not isinstance(node, TypeInfo): self.defer(node) return - typ = Instance(node, [self.str_type()]) + typ = Instance(node, [str_type]) elif name == "__annotations__": sym = self.lookup_qualified("__builtins__.dict", Context(), suppress_errors=True) if not sym: @@ -621,7 +624,7 @@ def add_implicit_module_attrs(self, file_node: MypyFile) -> None: if not isinstance(node, TypeInfo): self.defer(node) return - typ = Instance(node, [self.str_type(), AnyType(TypeOfAny.special_form)]) + typ = Instance(node, [str_type, AnyType(TypeOfAny.special_form)]) else: assert t is not None, f"type should be specified for {name}" typ = UnboundType(t) diff --git a/mypy/semanal_main.py b/mypy/semanal_main.py index 9e3aeaa7fa4b..31bcdc2b703d 100644 --- a/mypy/semanal_main.py +++ b/mypy/semanal_main.py @@ -66,7 +66,14 @@ # Number of passes over core modules before going on to the rest of the builtin SCC. CORE_WARMUP: Final = 2 -core_modules: Final = ["typing", "builtins", "abc", "collections"] +core_modules: Final = [ + "typing", + "_collections_abc", + "builtins", + "abc", + "collections", + "collections.abc", +] def semantic_analysis_for_scc(graph: Graph, scc: list[str], errors: Errors) -> None: