Skip to content

Commit

Permalink
[undefined vars] fix per-module error code override bug (#14351)
Browse files Browse the repository at this point in the history
This one would occur because we set the errors module with global
options, instead of per-module override ones.
It only mattered for checks that happened after the partially undefined
checks, which (I believe) is only the unused `type: ignore` checks.

This was discovered when updating tests for #14166.

I've also cleaned up the function signature a little.
  • Loading branch information
ilinum committed Dec 28, 2022
1 parent 1f8621c commit 109c8ce
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
10 changes: 5 additions & 5 deletions mypy/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -2347,19 +2347,19 @@ def type_check_second_pass(self) -> bool:
self.time_spent_us += time_spent_us(t0)
return result

def detect_possibly_undefined_vars(self, type_map: dict[Expression, Type]) -> None:
def detect_possibly_undefined_vars(self) -> None:
assert self.tree is not None, "Internal error: method must be called on parsed file only"
if self.tree.is_stub:
# We skip stub files because they aren't actually executed.
return
manager = self.manager
manager.errors.set_file(self.xpath, self.tree.fullname, options=self.options)
if manager.errors.is_error_code_enabled(
codes.POSSIBLY_UNDEFINED
) or manager.errors.is_error_code_enabled(codes.USED_BEFORE_DEF):
manager.errors.set_file(self.xpath, self.tree.fullname, options=manager.options)
self.tree.accept(
PossiblyUndefinedVariableVisitor(
MessageBuilder(manager.errors, manager.modules), type_map, manager.options
MessageBuilder(manager.errors, manager.modules), self.type_map(), self.options
)
)

Expand Down Expand Up @@ -3418,7 +3418,7 @@ def process_stale_scc(graph: Graph, scc: list[str], manager: BuildManager) -> No
graph[id].type_check_first_pass()
if not graph[id].type_checker().deferred_nodes:
unfinished_modules.discard(id)
graph[id].detect_possibly_undefined_vars(graph[id].type_map())
graph[id].detect_possibly_undefined_vars()
graph[id].finish_passes()

while unfinished_modules:
Expand All @@ -3427,7 +3427,7 @@ def process_stale_scc(graph: Graph, scc: list[str], manager: BuildManager) -> No
continue
if not graph[id].type_check_second_pass():
unfinished_modules.discard(id)
graph[id].detect_possibly_undefined_vars(graph[id].type_map())
graph[id].detect_possibly_undefined_vars()
graph[id].finish_passes()
for id in stale:
graph[id].generate_unused_ignore_notes()
Expand Down
2 changes: 1 addition & 1 deletion mypy/server/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ def restore(ids: list[str]) -> None:
state.type_checker().reset()
state.type_check_first_pass()
state.type_check_second_pass()
state.detect_possibly_undefined_vars(state.type_map())
state.detect_possibly_undefined_vars()
t2 = time.time()
state.finish_passes()
t3 = time.time()
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-flags.test
Original file line number Diff line number Diff line change
Expand Up @@ -2110,7 +2110,7 @@ if foo: ... # E: Function "Callable[[], int]" could always be true in boolean c
42 + "no" # type: ignore
[file mypy.ini]
\[mypy]
enable_error_code = ignore-without-code, truthy-bool
enable_error_code = ignore-without-code, truthy-bool, used-before-def

\[mypy-tests.*]
disable_error_code = ignore-without-code
Expand Down

0 comments on commit 109c8ce

Please sign in to comment.