From ee0a1c6b5bb7c42096cf6ea088865dd841dec438 Mon Sep 17 00:00:00 2001 From: Eli Schwartz Date: Sun, 21 Sep 2025 01:41:06 -0400 Subject: mypy: enable allow-redefinition-new and fix fallout MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reduces 3 errors that show up in newer mypy versions than pinned in CI. It is new since 1.16 and a likely future default for mypy 2.0. It allows things like: ``` for i in ['one', 'two', 'three']: frob_a(i) for i in [1, 2, 3]: frob_b(i) ``` since "i" is obviously used as a loop holder and its type can be freely reinvented. Note: allow-redefinition-new isn't actually about this at all, it has greater scope than loop holders and allows redefining "unannotated variables" of all kinds. No granularity in what to accept redefinition of. :P To enable this, we must also opt in to local-partial-types, which has some overlap with None-safety. Specifically: > the most common cases for partial types are variables initialized > using None, but without explicit X | None annotations. By default, mypy > won’t check partial types spanning module top level or class top level. > This flag changes the behavior to only allow partial types at local > level, therefore it disallows inferring variable type for None from two > assignments in different scopes. So with this, we also fix a couple of actual type errors this revealed. Where possible, stop None-initializing at all -- it's not strictly needed for global variables, anyway, and it's a coding error if it is possible to hit these variables without defining them first. Bug: https://github.com/python/mypy/issues/19280 --- run_project_tests.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'run_project_tests.py') diff --git a/run_project_tests.py b/run_project_tests.py index b2442703e..7067bfa0b 100755 --- a/run_project_tests.py +++ b/run_project_tests.py @@ -1577,11 +1577,11 @@ def detect_tools(report: bool = True) -> None: print('{0:<{2}}: {1}'.format(tool.tool, get_version(tool), max_width)) print() -symlink_test_dir1 = None -symlink_test_dir2 = None -symlink_file1 = None -symlink_file2 = None -symlink_file3 = None +symlink_test_dir1: T.Optional[Path] = None +symlink_test_dir2: T.Optional[Path] = None +symlink_file1: T.Optional[Path] = None +symlink_file2: T.Optional[Path] = None +symlink_file3: T.Optional[Path] = None def scan_test_data_symlinks() -> None: global symlink_test_dir1, symlink_test_dir2, symlink_file1, symlink_file2, symlink_file3 -- cgit v1.2.3