Skip to content

Commit 6442b02

Browse files
authored
Adjust SCC setup to enable earlier collections.abc import in typeshed (#14088)
Fixes #11860 (?) Typeshed is currently unable to import Sequence, MutableSequence, or ByteString from collections.abc within builtins.pyi. It seems this is because: 1. In order to analyze `collections.abc`, we first need to analyze `collections`. 2. Since `collections` is a package containing an `__init__.pyi` file, the `add_implicit_module_attrs` function will try adding the `__path__` variable to the symboltable. 3. The `__path__` variable has type `builtins.str`. But str is a subclass of Sequence, which we have not analyzed yet since we're still in the middle of analyzing `collections` and `collections.abc`. This diff tries repairing this by: 1. Adding `_collections_abc` and `collections.abc` to the set of special-cased core modules we deliberately process early. 2. Modifying `add_implicit_module_attrs` so it does the same trick we do for the `__doc__` symbol and fall back to using an UnboundType if `builtins.str` is not defined yet. To be 100% honest, I'm not really sold on this PR for a few reasons: - I was able to test these changes manually, but wasn't sure how to write tests for them. - We have 3-4 subtly different lists of "core modules" scattered throughout mypy. For example, see `CORE_BUILTIN_MODULES` in mypy/build.py or try grepping for the string `"typing"` in the mypy dir. Arguably, we should defer landing this PR until we've had a chance to consolidate these lists and confirm there are no additional places where we need to special-case `_collections_abc`, `collections`, and `collections.abc`. - PEP 585 attempted to declare that we should one day remove entries like Sequence from `typing` module, but this realistically doesn't seem ever achievable given that (a) it would break backwards compat and (b) there doesn't seem to be any incentives for users to proactively switch. In that case, is there any pressing reason to change typeshed? Regardless, this is a crash and my goal atm is to de-crash mypy, so I'm throwing this over the wall.
1 parent 0665ce9 commit 6442b02

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

mypy/semanal.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -615,9 +615,12 @@ def refresh_top_level(self, file_node: MypyFile) -> None:
615615

616616
def add_implicit_module_attrs(self, file_node: MypyFile) -> None:
617617
"""Manually add implicit definitions of module '__name__' etc."""
618+
str_type: Type | None = self.named_type_or_none("builtins.str")
619+
if str_type is None:
620+
str_type = UnboundType("builtins.str")
618621
for name, t in implicit_module_attrs.items():
619622
if name == "__doc__":
620-
typ: Type = UnboundType("__builtins__.str")
623+
typ: Type = str_type
621624
elif name == "__path__":
622625
if not file_node.is_package_init_file():
623626
continue
@@ -630,7 +633,7 @@ def add_implicit_module_attrs(self, file_node: MypyFile) -> None:
630633
if not isinstance(node, TypeInfo):
631634
self.defer(node)
632635
return
633-
typ = Instance(node, [self.str_type()])
636+
typ = Instance(node, [str_type])
634637
elif name == "__annotations__":
635638
sym = self.lookup_qualified("__builtins__.dict", Context(), suppress_errors=True)
636639
if not sym:
@@ -639,7 +642,7 @@ def add_implicit_module_attrs(self, file_node: MypyFile) -> None:
639642
if not isinstance(node, TypeInfo):
640643
self.defer(node)
641644
return
642-
typ = Instance(node, [self.str_type(), AnyType(TypeOfAny.special_form)])
645+
typ = Instance(node, [str_type, AnyType(TypeOfAny.special_form)])
643646
else:
644647
assert t is not None, f"type should be specified for {name}"
645648
typ = UnboundType(t)

mypy/semanal_main.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,14 @@
6666

6767
# Number of passes over core modules before going on to the rest of the builtin SCC.
6868
CORE_WARMUP: Final = 2
69-
core_modules: Final = ["typing", "builtins", "abc", "collections"]
69+
core_modules: Final = [
70+
"typing",
71+
"_collections_abc",
72+
"builtins",
73+
"abc",
74+
"collections",
75+
"collections.abc",
76+
]
7077

7178

7279
def semantic_analysis_for_scc(graph: Graph, scc: list[str], errors: Errors) -> None:

0 commit comments

Comments
 (0)