From 7ed9700a95ea0d35acd3c3e1763f1888ad50baae Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Wed, 4 Nov 2026 17:47:26 -0500 Subject: [PATCH] flake8: fix nested import checks Import modules at the top of the module in most cases. Many of these weren't actually saving any time: * editor: shlex is already imported by the main module * platform_utils: On Windows, the win32 module is almost always needed * tests: There's no point in optimizing these. The trace import in the main module is about the only one worth allowing since that module itself is rarely used, and only when in a specific debug mode. Change-Id: I5d3fcecbe51e0ce78d4e856d7033565150ef4526 Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/654103 Commit-Queue: Mike Frysinger Reviewed-by: Brian Gan Tested-by: Mike Frysinger --- editor.py | 3 +-- main.py | 4 +++- platform_utils.py | 10 ++++------ tests/test_subcmds.py | 5 ++--- tests/test_subcmds_sync.py | 3 +-- 5 files changed, 11 insertions(+), 14 deletions(-) diff --git a/editor.py b/editor.py index 5b8f08a7b..75537995d 100644 --- a/editor.py +++ b/editor.py @@ -14,6 +14,7 @@ import os import re +import shlex import subprocess import sys import tempfile @@ -89,8 +90,6 @@ least one of these before using this command.""", # noqa: E501 if platform_utils.isWindows(): # Split on spaces, respecting quoted strings - import shlex - args = shlex.split(editor) shell = False elif re.compile("^.*[$ \t'].*$").match(editor): diff --git a/main.py b/main.py index 91e96b5ff..8f5cf32b0 100755 --- a/main.py +++ b/main.py @@ -274,7 +274,9 @@ class _Repo: first_trace=True, ): if gopts.trace_python: - import trace + # This is a debug module that is not otherwise used, so don't + # import it all the time. + import trace # noqa: IMR200 tracer = trace.Trace( count=False, diff --git a/platform_utils.py b/platform_utils.py index 58df6a9e2..f2fe66749 100644 --- a/platform_utils.py +++ b/platform_utils.py @@ -28,6 +28,10 @@ def isWindows(): return platform.system() == "Windows" +if isWindows(): + import platform_utils_win32 + + def symlink(source, link_name): """Creates a symbolic link pointing to source named link_name. @@ -35,8 +39,6 @@ def symlink(source, link_name): to know whether to create a "File" or a "Directory" symbolic link. """ if isWindows(): - import platform_utils_win32 - source = _validate_winpath(source) link_name = _validate_winpath(link_name) target = os.path.join(os.path.dirname(link_name), source) @@ -273,8 +275,6 @@ def islink(path): Availability: Windows, Unix. """ if isWindows(): - import platform_utils_win32 - return platform_utils_win32.islink(_makelongpath(path)) else: return os.path.islink(path) @@ -289,8 +289,6 @@ def readlink(path): Availability: Windows, Unix. """ if isWindows(): - import platform_utils_win32 - return platform_utils_win32.readlink(_makelongpath(path)) else: return os.readlink(path) diff --git a/tests/test_subcmds.py b/tests/test_subcmds.py index 12bbb2700..3a52f2284 100644 --- a/tests/test_subcmds.py +++ b/tests/test_subcmds.py @@ -21,6 +21,7 @@ import pytest from command import Command import subcmds +from subcmds import sync # NB: We don't test all subcommands as we want to avoid "change detection" @@ -164,9 +165,7 @@ def test_common_validate_options(name: str, cls: Type[Command]) -> None: def test_attribute_error_repro() -> None: """Confirm that accessing verbose before CommonValidateOptions fails.""" - from subcmds.sync import Sync - - cmd = Sync() + cmd = sync.Sync() opts, args = cmd.OptionParser.parse_args([]) # This confirms that without the fix in main.py, an AttributeError diff --git a/tests/test_subcmds_sync.py b/tests/test_subcmds_sync.py index e695e8e4e..4cdc954c2 100644 --- a/tests/test_subcmds_sync.py +++ b/tests/test_subcmds_sync.py @@ -19,6 +19,7 @@ import optparse import os from pathlib import Path import shutil +import subprocess import tempfile import threading import time @@ -3012,8 +3013,6 @@ class TestSmartSyncSetupRemoteHelper(unittest.TestCase): self, mock_transport_class, mock_server_class, mock_popen, mock_which ): """Test _SmartSyncSetup when a helper is present and succeeds.""" - import subprocess - self.manifest.manifest_server = ( "persistent-https://android-smartsync.corp.google.com/" "manifestserver"