Skip to content

Do not display import-related errors after module-level always false assert #19347

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions mypy/semanal_pass1.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ def visit_file(self, file: MypyFile, fnam: str, mod_id: str, options: Options) -
if last.end_line is not None:
# We are on a Python version recent enough to support end lines.
self.skipped_lines |= set(range(next_def.line, last.end_line + 1))
file.imports = [
i for i in file.imports if (i.line, i.column) <= (defn.line, defn.column)
]
del file.defs[i + 1 :]
break
file.skipped_lines = self.skipped_lines
Expand Down
9 changes: 9 additions & 0 deletions test-data/unit/check-incremental.test
Original file line number Diff line number Diff line change
Expand Up @@ -6886,3 +6886,12 @@ class A:
[out]
[out2]
main:3: error: Too few arguments

[case testUnreachableAfterToplevelAssertImportThirdParty]
# flags: --platform unknown
import sys
assert sys.platform == 'linux'
import does_not_exist
[builtins fixtures/ops.pyi]
[out]
[out2]
16 changes: 16 additions & 0 deletions test-data/unit/check-unreachable-code.test
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,22 @@ assert sys.platform == 'lol'
def bar() -> None: pass
[builtins fixtures/ops.pyi]

[case testUnreachableAfterToplevelAssertImportThirdParty]
# flags: --platform unknown
import sys
assert sys.platform == 'linux'
import does_not_exist
[builtins fixtures/ops.pyi]

[case testUnreachableAfterToplevelAssertImportThirdParty2]
# flags: --platform unknown
import sys
import bad; assert sys.platform == 'linux'; import does_not_exist
[builtins fixtures/ops.pyi]
[out]
main:3: error: Cannot find implementation or library stub for module named "bad"
main:3: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports

[case testUnreachableAfterToplevelAssertNotInsideIf]
import sys
if sys.version_info[0] >= 2:
Expand Down