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 <vapier@google.com>
Reviewed-by: Brian Gan <brgan@google.com>
Tested-by: Mike Frysinger <vapier@google.com>
This commit is contained in:
Mike Frysinger authored and gerrit-scoped@luci-project-accounts.iam.gserviceaccount.com committed 2026-11-05 10:50:14 -08:00
1 parent d621aa8ee4
commit 7ed9700a95
5 files changed
+11 -14

No files matched your search

+1 -2
View File
@@ -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):
+3 -1
View File
@@ -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,
+4 -6
View File
@@ -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)
+2 -3
View File
@@ -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
+1 -2
View File
@@ -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"