From 109c8ce84685136d1cd89c86d4b3458272a6144f Mon Sep 17 00:00:00 2001 From: Stas Ilinskiy Date: Wed, 28 Dec 2022 06:01:14 -0800 Subject: [PATCH] [undefined vars] fix per-module error code override bug (#14351) 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. --- mypy/build.py | 10 +++++----- mypy/server/update.py | 2 +- test-data/unit/check-flags.test | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/mypy/build.py b/mypy/build.py index 2e0fa455554a..1747c4518c63 100644 --- a/mypy/build.py +++ b/mypy/build.py @@ -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 ) ) @@ -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: @@ -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() diff --git a/mypy/server/update.py b/mypy/server/update.py index 9bea1998c0e5..83cce22873a1 100644 --- a/mypy/server/update.py +++ b/mypy/server/update.py @@ -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() diff --git a/test-data/unit/check-flags.test b/test-data/unit/check-flags.test index 33723b7fee76..a76463e3106b 100644 --- a/test-data/unit/check-flags.test +++ b/test-data/unit/check-flags.test @@ -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