Skip to content

Commit 80f37a3

Browse files
committed
Don't ignore errors in files passed on the command line
that were under a directory in `sys.path`, which sometimes includes the current working directory, resulting in no errors reported at all. Fixes #14042.
1 parent dbcbb3f commit 80f37a3

File tree

3 files changed

+70
-3
lines changed

3 files changed

+70
-3
lines changed

mypy/build.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1940,7 +1940,7 @@ def __init__(
19401940
raise
19411941
if follow_imports == "silent":
19421942
self.ignore_all = True
1943-
elif path and is_silent_import_module(manager, path):
1943+
elif path and is_silent_import_module(manager, path) and not root_source:
19441944
self.ignore_all = True
19451945
self.path = path
19461946
if path:
@@ -2629,7 +2629,7 @@ def find_module_and_diagnose(
26292629
else:
26302630
skipping_module(manager, caller_line, caller_state, id, result)
26312631
raise ModuleNotFound
2632-
if is_silent_import_module(manager, result):
2632+
if is_silent_import_module(manager, result) and not root_source:
26332633
follow_imports = "silent"
26342634
return (result, follow_imports)
26352635
else:

mypy/test/testcmdline.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,13 @@ def test_python_cmdline(testcase: DataDrivenTestCase, step: int) -> None:
6969
env["PYTHONPATH"] = PREFIX
7070
if os.path.isdir(extra_path):
7171
env["PYTHONPATH"] += os.pathsep + extra_path
72+
cwd = os.path.join(test_temp_dir, custom_cwd or "")
73+
args = [arg.replace("$CWD", os.path.abspath(cwd)) for arg in args]
7274
process = subprocess.Popen(
7375
fixed + args,
7476
stdout=subprocess.PIPE,
7577
stderr=subprocess.PIPE,
76-
cwd=os.path.join(test_temp_dir, custom_cwd or ""),
78+
cwd=cwd,
7779
env=env,
7880
)
7981
outb, errb = process.communicate()

test-data/unit/cmdline.test

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1505,3 +1505,68 @@ def f():
15051505
[out]
15061506
a.py:2: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs
15071507
== Return code: 0
1508+
1509+
[case testCustomTypeshedDirFilePassedExplicitly]
1510+
# cmd: mypy --custom-typeshed-dir dir m.py dir/stdlib/foo.pyi
1511+
[file m.py]
1512+
1()
1513+
[file dir/stdlib/abc.pyi]
1514+
1() # Errors are not reported from typeshed by default
1515+
[file dir/stdlib/builtins.pyi]
1516+
class object: pass
1517+
class str(object): pass
1518+
class int(object): pass
1519+
[file dir/stdlib/sys.pyi]
1520+
[file dir/stdlib/types.pyi]
1521+
[file dir/stdlib/typing.pyi]
1522+
[file dir/stdlib/mypy_extensions.pyi]
1523+
[file dir/stdlib/typing_extensions.pyi]
1524+
[file dir/stdlib/foo.pyi]
1525+
1() # Errors are reported if the file was explicitly passed on the command line
1526+
[file dir/stdlib/VERSIONS]
1527+
[out]
1528+
dir/stdlib/foo.pyi:1: error: "int" not callable
1529+
m.py:1: error: "int" not callable
1530+
1531+
[case testFileInPythonPathPassedExplicitly1]
1532+
# cmd: mypy $CWD/pypath/foo.py
1533+
[file pypath/foo.py]
1534+
1()
1535+
[out]
1536+
pypath/foo.py:1: error: "int" not callable
1537+
1538+
[case testFileInPythonPathPassedExplicitly2]
1539+
# cmd: mypy pypath/foo.py
1540+
[file pypath/foo.py]
1541+
1()
1542+
[out]
1543+
pypath/foo.py:1: error: "int" not callable
1544+
1545+
[case testFileInPythonPathPassedExplicitly3]
1546+
# cmd: mypy -p foo
1547+
# cwd: pypath
1548+
[file pypath/foo/__init__.py]
1549+
1()
1550+
[file pypath/foo/m.py]
1551+
1()
1552+
[out]
1553+
foo/m.py:1: error: "int" not callable
1554+
foo/__init__.py:1: error: "int" not callable
1555+
1556+
[case testFileInPythonPathPassedExplicitly4]
1557+
# cmd: mypy -m foo
1558+
# cwd: pypath
1559+
[file pypath/foo.py]
1560+
1()
1561+
[out]
1562+
foo.py:1: error: "int" not callable
1563+
1564+
[case testFileInPythonPathPassedExplicitly5]
1565+
# cmd: mypy -m foo.m
1566+
# cwd: pypath
1567+
[file pypath/foo/__init__.py]
1568+
1() # TODO: Maybe this should generate errors as well? But how would we decide?
1569+
[file pypath/foo/m.py]
1570+
1()
1571+
[out]
1572+
foo/m.py:1: error: "int" not callable

0 commit comments

Comments
 (0)