Skip to content

Commit 4f907d5

Browse files
committed
[stubtest] Verify __all__ exists in stub
Closes: #13300
1 parent 5bae05d commit 4f907d5

File tree

2 files changed

+10
-15
lines changed

2 files changed

+10
-15
lines changed

mypy/stubtest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,8 @@ def verify_mypyfile(
348348
# Only verify the contents of the stub's __all__
349349
# if the stub actually defines __all__
350350
yield from _verify_exported_names(object_path, stub, runtime_all_as_set)
351+
else:
352+
yield Error(object_path + ["__all__"], "is not present in stub", stub, runtime)
351353
else:
352354
runtime_all_as_set = None
353355

mypy/test/teststubtest.py

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,14 @@ def run_stubtest_with_stderr(
169169
filtered_output = remove_color_code(
170170
output.getvalue()
171171
# remove cwd as it's not available from outside
172-
.replace(os.path.realpath(tmp_dir) + os.sep, "").replace(tmp_dir + os.sep, "")
172+
.replace(os.path.realpath(tmp_dir) + os.sep, "")
173+
.replace(tmp_dir + os.sep, "")
173174
)
174175
filtered_outerr = remove_color_code(
175176
outerr.getvalue()
176177
# remove cwd as it's not available from outside
177-
.replace(os.path.realpath(tmp_dir) + os.sep, "").replace(tmp_dir + os.sep, "")
178+
.replace(os.path.realpath(tmp_dir) + os.sep, "")
179+
.replace(tmp_dir + os.sep, "")
178180
)
179181
return filtered_output, filtered_outerr
180182

@@ -1403,7 +1405,7 @@ def test_all_at_runtime_not_stub(self) -> Iterator[Case]:
14031405
runtime="""
14041406
__all__ = []
14051407
Z = 5""",
1406-
error=None,
1408+
error="__all__",
14071409
)
14081410

14091411
@collect_cases
@@ -1443,7 +1445,7 @@ def h(x: str): ...
14431445
runtime="",
14441446
error="h",
14451447
)
1446-
yield Case(stub="", runtime="__all__ = []", error=None) # dummy case
1448+
yield Case(stub="", runtime="__all__ = []", error="__all__") # dummy case
14471449
yield Case(stub="", runtime="__all__ += ['y']\ny = 5", error="y")
14481450
yield Case(stub="", runtime="__all__ += ['g']\ndef g(): pass", error="g")
14491451
# Here we should only check that runtime has B, since the stub explicitly re-exports it
@@ -2435,11 +2437,6 @@ def test_output(self) -> None:
24352437
assert output == expected
24362438

24372439
def test_ignore_flags(self) -> None:
2438-
output = run_stubtest(
2439-
stub="", runtime="__all__ = ['f']\ndef f(): pass", options=["--ignore-missing-stub"]
2440-
)
2441-
assert output == "Success: no issues found in 1 module\n"
2442-
24432440
output = run_stubtest(stub="", runtime="def f(): pass", options=["--ignore-missing-stub"])
24442441
assert output == "Success: no issues found in 1 module\n"
24452442

@@ -2488,18 +2485,14 @@ def test_allowlist(self) -> None:
24882485
def good() -> None: ...
24892486
def bad(number: int) -> None: ...
24902487
def also_bad(number: int) -> None: ...
2491-
""".lstrip(
2492-
"\n"
2493-
)
2488+
""".lstrip("\n")
24942489
),
24952490
runtime=textwrap.dedent(
24962491
"""
24972492
def good(): pass
24982493
def bad(asdf): pass
24992494
def also_bad(asdf): pass
2500-
""".lstrip(
2501-
"\n"
2502-
)
2495+
""".lstrip("\n")
25032496
),
25042497
options=["--allowlist", allowlist.name, "--generate-allowlist"],
25052498
)

0 commit comments

Comments
 (0)