Skip to content
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

Simplify suppression of spurious 'unused-ignore's #15419

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion mypy/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -2239,7 +2239,6 @@ def semantic_analysis_pass1(self) -> None:
analyzer = SemanticAnalyzerPreAnalysis()
with self.wrap_context():
analyzer.visit_file(self.tree, self.xpath, self.id, options)
self.manager.errors.set_unreachable_lines(self.xpath, self.tree.unreachable_lines)
# TODO: Do this while constructing the AST?
self.tree.names = SymbolTable()
if not self.tree.is_stub:
Expand Down
9 changes: 0 additions & 9 deletions mypy/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,6 @@ class Errors:
# (path -> line -> error-codes)
ignored_lines: dict[str, dict[int, list[str]]]

# Lines that are statically unreachable (e.g. due to platform/version check).
unreachable_lines: dict[str, set[int]]

# Lines on which an error was actually ignored.
used_ignored_lines: dict[str, dict[int, list[str]]]

Expand Down Expand Up @@ -280,7 +277,6 @@ def initialize(self) -> None:
self.import_ctx = []
self.function_or_member = [None]
self.ignored_lines = {}
self.unreachable_lines = {}
self.used_ignored_lines = defaultdict(lambda: defaultdict(list))
self.ignored_files = set()
self.only_once_messages = set()
Expand Down Expand Up @@ -329,9 +325,6 @@ def set_file_ignored_lines(
if ignore_all:
self.ignored_files.add(file)

def set_unreachable_lines(self, file: str, unreachable_lines: set[int]) -> None:
self.unreachable_lines[file] = unreachable_lines

def current_target(self) -> str | None:
"""Retrieves the current target from the associated scope.

Expand Down Expand Up @@ -630,8 +623,6 @@ def generate_unused_ignore_errors(self, file: str) -> None:
ignored_lines = self.ignored_lines[file]
used_ignored_lines = self.used_ignored_lines[file]
for line, ignored_codes in ignored_lines.items():
if line in self.unreachable_lines[file]:
continue
if codes.UNUSED_IGNORE.code in ignored_codes:
continue
used_ignored_codes = used_ignored_lines[line]
Expand Down
9 changes: 6 additions & 3 deletions mypy/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
Any,
Callable,
Dict,
Iterable,
Iterator,
List,
Optional,
Expand Down Expand Up @@ -314,8 +315,6 @@ class MypyFile(SymbolNode):
# If the value is empty, ignore all errors; otherwise, the list contains all
# error codes to ignore.
ignored_lines: dict[int, list[str]]
# Lines that are statically unreachable (e.g. due to platform/version check).
unreachable_lines: set[int]
# Is this file represented by a stub file (.pyi)?
is_stub: bool
# Is this loaded from the cache and thus missing the actual body of the file?
Expand Down Expand Up @@ -348,7 +347,6 @@ def __init__(
self.ignored_lines = ignored_lines
else:
self.ignored_lines = {}
self.unreachable_lines = set()

self.path = ""
self.is_stub = False
Expand Down Expand Up @@ -380,6 +378,11 @@ def is_package_init_file(self) -> bool:
def is_future_flag_set(self, flag: str) -> bool:
return flag in self.future_import_flags

def remove_ignored_lines(self, lines: Iterable[int]) -> None:
"""Called to prevent spurious 'unused-ignore' errors in lines that we skip checking."""
for line in lines:
self.ignored_lines.pop(line, None)

def serialize(self) -> JsonDict:
return {
".class": "MypyFile",
Expand Down
8 changes: 3 additions & 5 deletions mypy/semanal_pass1.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class SemanticAnalyzerPreAnalysis(TraverserVisitor):

def do_stuff():
# type: () -> None:
if sys.python_version < (3,):
if sys.version_info < (3,):
import xyz # Only available in Python 2
xyz.whatever()
...
Expand All @@ -62,7 +62,6 @@ def visit_file(self, file: MypyFile, fnam: str, mod_id: str, options: Options) -
self.cur_mod_node = file
self.options = options
self.is_global_scope = True
self.unreachable_lines: set[int] = set()

for i, defn in enumerate(file.defs):
defn.accept(self)
Expand All @@ -74,10 +73,9 @@ def visit_file(self, file: MypyFile, fnam: str, mod_id: str, options: Options) -
next_def, last = file.defs[i + 1], file.defs[-1]
if last.end_line is not None:
# We are on a Python version recent enough to support end lines.
self.unreachable_lines |= set(range(next_def.line, last.end_line + 1))
file.remove_ignored_lines(range(next_def.line, last.end_line + 1))
del file.defs[i + 1 :]
break
file.unreachable_lines = self.unreachable_lines

def visit_func_def(self, node: FuncDef) -> None:
old_global_scope = self.is_global_scope
Expand Down Expand Up @@ -127,7 +125,7 @@ def visit_block(self, b: Block) -> None:
if b.is_unreachable:
if b.end_line is not None:
# We are on a Python version recent enough to support end lines.
self.unreachable_lines |= set(range(b.line, b.end_line + 1))
self.cur_mod_node.remove_ignored_lines(range(b.line, b.end_line + 1))
return
super().visit_block(b)

Expand Down
1 change: 0 additions & 1 deletion mypy/server/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -986,7 +986,6 @@ def key(node: FineGrainedDeferredNode) -> int:
manager.errors.set_file_ignored_lines(
file_node.path, file_node.ignored_lines, options.ignore_errors or state.ignore_all
)
manager.errors.set_unreachable_lines(file_node.path, file_node.unreachable_lines)

targets = set()
for node in nodes:
Expand Down