Skip to content

Commit b69309b

Browse files
authored
Prevent synthetic intersections from leaking to module public interfaces (#20459)
Fixes #20457. --- In addition, 642cf64 fixes a bug which can be triggered in stubtest. Reproducer: ```python # mod.pyi class A: ... class B: ... class AB(A, B): ... a: A if isinstance(a, B): ... ``` ```python # mod.py class A: pass class B: pass class AB(A, B): pass a = A() ``` ```shell $ stubtest mod error: mod.<subclass of "mod.A" and "mod.B"> is not present at runtime Stub: in file mod.pyi <TypeInfo mod.<subclass of "mod.A" and "mod.B">> Runtime: MISSING ``` Admittedly, nobody should be writing `isinstance()` in a stub, but it doesn't seem to be illegal to do so, and in any case auto-generated stubs may forget to strip such constructs and surface weird error messages like the one above. I tried adding a test to `teststubtest.py`, but it fails because there doesn't seem to be a way to add fixtures to test cases there, which is needed to invoke `isinstance()`. The test is ```python @collect_cases def test_generated_intersection_non_public(self) -> Iterator[Case]: yield Case( stub=""" class A: ... class B: ... class AB(A, B): ... a: A if isinstance(a, B): ... """, runtime=""" class A: pass class B: pass class AB(A, B): pass a: A """, error=None, ) ```
1 parent 72cff30 commit b69309b

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

mypy/checker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5968,7 +5968,7 @@ def _make_fake_typeinfo_and_full_name(
59685968
errors.append((pretty_names_list, "would have incompatible method signatures"))
59695969
return None
59705970

5971-
curr_module.names[full_name] = SymbolTableNode(GDEF, info)
5971+
curr_module.names[full_name] = SymbolTableNode(GDEF, info, False, module_hidden=True)
59725972
return Instance(info, [], extra_attrs=instances[0].extra_attrs or instances[1].extra_attrs)
59735973

59745974
def intersect_instance_callable(self, typ: Instance, callable_type: CallableType) -> Instance:

test-data/unit/check-modules.test

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,25 @@ x = 1
593593
[file m2.py]
594594
x = 1
595595

596+
[case testStarImportDoesNotImportGeneratedIntersections]
597+
from m1 import *
598+
from m2 import *
599+
[file m1.py]
600+
from m3 import A, B
601+
602+
a: A
603+
if isinstance(a, B): ...
604+
[file m2.py]
605+
from m3 import A, B
606+
607+
a: A
608+
if isinstance(a, B): ...
609+
[file m3.py]
610+
class A: ...
611+
class B: ...
612+
class AB(A, B): ...
613+
[builtins fixtures/isinstance.pyi]
614+
596615
[case testAssignToFuncDefViaImport]
597616

598617
# Errors differ with the new analyzer. (Old analyzer gave error on the

0 commit comments

Comments
 (0)