8000 stubtest: increase coverage (#11634) · DeepSourceCorp/mypy@eb59cf9 · GitHub
[go: up one dir, main page]

Skip to content

Commit eb59cf9

Browse files
hauntsaninjatushar-deepsource
authored andcommitted
stubtest: increase coverage (python#11634)
Co-authored-by: hauntsaninja <>
1 parent ec82adb commit eb59cf9

File tree

2 files changed

+41
-14
lines changed

2 files changed

+41
-14
lines changed

mypy/stubtest.py

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -200,23 +200,34 @@ def verify_mypyfile(
200200
yield Error(object_path, "is not a module", stub, runtime)
201201
return
202202

203-
# Check things in the stub that are public
203+
# Check things in the stub
204204
to_check = set(
205205
m
206206
for m, o in stub.names.items()
207-
# TODO: change `o.module_public` to `not o.module_hidden`
208-
if o.module_public and (not m.startswith("_") or hasattr(runtime, m))
207+
if not o.module_hidden and (not m.startswith("_") or hasattr(runtime, m))
209208
)
210-
runtime_public_contents = [
211-
m
212-
for m in dir(runtime)
213-
if not m.startswith("_")
214-
# Ensure that the object's module is `runtime`, since in the absence of __all__ we don't
215-
# have a good way to detect re-exports at runtime.
216-
and getattr(getattr(runtime, m), "__module__", None) == runtime.__name__
217-
]
218-
# Check all things declared in module's __all__, falling back to runtime_public_contents
219-
to_check.update(getattr(runtime, "__all__", runtime_public_contents))
209+
210+
def _belongs_to_runtime(r: types.ModuleType, attr: str) -> bool:
211+
obj = getattr(r, attr)
212+
obj_mod = getattr(obj, "__module__", None)
213+
if obj_mod is not None:
214+
return obj_mod == r.__name__
215+
return not isinstance(obj, types.ModuleType)
216+
217+
runtime_public_contents = (
218+
runtime.__all__
219+
if hasattr(runtime, "__all__")
220+
else [
221+
m
222+
for m in dir(runtime)
223+
if not m.startswith("_")
224+
# Ensure that the object's module is `runtime`, since in the absence of __all__ we
225+
# don't have a good way to detect re-exports at runtime.
226+
and _belongs_to_runtime(runtime, m)
227+
]
228+
)
229+
# Check all things declared in module's __all__, falling back to our best guess
230+
to_check.update(runtime_public_contents)
220231
to_check.difference_update({"__file__", "__doc__", "__name__", "__builtins__", "__package__"})
221232

222233
for entry in sorted(to_check):

mypy/test/teststubtest.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ class bool(int): ...
5252
class str: ...
5353
class bytes: ...
5454
55+
class list(Sequence[T]): ...
56+
5557
def property(f: T) -> T: ...
5658
def classmethod(f: T) -> T: ...
5759
def staticmethod(f: T) -> T: ...
@@ -648,7 +650,7 @@ def h(x: str): ...
648650
runtime="",
649651
error="h",
650652
)
651-
yield Case("", "__all__ = []", None) # dummy case
653+
yield Case(stub="", runtime="__all__ = []", error=None) # dummy case
652654
yield Case( 8000 stub="", runtime="__all__ += ['y']\ny = 5", error="y")
653655
yield Case(stub="", runtime="__all__ += ['g']\ndef g(): pass", error="g")
654656
# Here we should only check that runtime has B, since the stub explicitly re-exports it
@@ -660,6 +662,20 @@ def h(x: str): ...
660662
def test_missing_no_runtime_all(self) -> Iterator[Case]:
661663
yield Case(stub="", runtime="import sys", error=None)
662664
yield Case(stub="", runtime="def g(): ...", error="g")
665+
yield Case(stub="", runtime="CONSTANT = 0", error="CONSTANT")
666+
667+
@collect_cases
668+
def test_non_public_1(self) -> Iterator[Case]:
669+
yield Case(stub="__all__: list[str]", runtime="", error=None) # dummy case
670+
yield Case(stub="_f: int", runtime="def _f(): ...", error="_f")
671+
672+
@collect_cases
673+
def test_non_public_2(self) -> Iterator[Case]:
674+
yield Case(
675+
stub="__all__: list[str] = ['f']", runtime="__all__ = ['f']", error=None
676+
)
677+
yield Case(stub="f: int", runtime="def f(): ...", error="f")
678+
yield Case(stub="g: int", runtime="def g(): ...", error="g")
663679

664680
@collect_cases
665681
def test_special_dunders(self) -> Iterator[Case]:

0 commit comments

Comments
 (0)
0