From 6e46bc13b4dea3b246614da62682a1ab5178d205 Mon Sep 17 00:00:00 2001 From: junkmd Date: Thu, 1 Oct 2026 08:23:06 +0900 Subject: [PATCH 1/5] refactor: Optimize version lookup in `test_pip_install`. Cache `read_version()` result in `setUpClass` to avoid repeated calls across test setup and assertions, and modernize formatting with f-strings. --- test_pip_install.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/test_pip_install.py b/test_pip_install.py index 1084c69d..62de17ae 100644 --- a/test_pip_install.py +++ b/test_pip_install.py @@ -19,12 +19,15 @@ def read_version(): class TestPipInstall(unittest.TestCase): + @classmethod + def setUpClass(cls): + cls.version = read_version() def setUp(self): """prepare the same package that is usually uploaded to PyPI""" subprocess.check_call([sys.executable, '-m', 'build', '--sdist']) - filename_for_upload = 'comtypes-%s.tar.gz' % read_version() + filename_for_upload = f'comtypes-{self.version}.tar.gz' self.target_package = os.path.join(os.getcwd(), 'dist', filename_for_upload) self.pip_exe = os.path.join(os.path.dirname(sys.executable), 'Scripts', 'pip.exe') @@ -35,7 +38,7 @@ def test_pip_install(self): def test_py_typed_is_packaged(self): with tarfile.open(self.target_package, "r:gz") as tar: self.assertIn( - f"comtypes-{read_version()}/comtypes/py.typed", tar.getnames() + f"comtypes-{self.version}/comtypes/py.typed", tar.getnames() ) def test_no_cache_dir_custom_location(self): From 740d68f48bfc88b60d0a80f7fb3ed2dbd365811d Mon Sep 17 00:00:00 2001 From: junkmd Date: Thu, 1 Oct 2026 08:23:06 +0900 Subject: [PATCH 2/5] refactor: Use f-string in `test_no_cache_dir_custom_location`. --- test_pip_install.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test_pip_install.py b/test_pip_install.py index 62de17ae..97dcff76 100644 --- a/test_pip_install.py +++ b/test_pip_install.py @@ -49,8 +49,7 @@ def test_no_cache_dir_custom_location(self): os.makedirs(custom_dir) # this test catches issue #158 - subprocess.check_call('{0} install {1} --no-cache-dir --target="{2}"' \ - ''.format(self.pip_exe, self.target_package, custom_dir)) + subprocess.check_call(f'{self.pip_exe} install {self.target_package} --no-cache-dir --target="{custom_dir}"') if __name__ == '__main__': From 6a6d09293601a467b74f6ca16471b66c6cc9699d Mon Sep 17 00:00:00 2001 From: junkmd Date: Thu, 1 Oct 2026 08:23:06 +0900 Subject: [PATCH 3/5] style: Format `test_pip_install.py` with `ruff==0.6.9`. --- test_pip_install.py | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/test_pip_install.py b/test_pip_install.py index 97dcff76..eaf1390f 100644 --- a/test_pip_install.py +++ b/test_pip_install.py @@ -1,11 +1,13 @@ """This test covers 'pip install' issue #155""" + import os -import sys import shutil import subprocess +import sys import tarfile import unittest + def read_version(): # Determine the version number by reading it from the file # 'comtypes\__init__.py'. We cannot import this file (with py3, @@ -13,7 +15,7 @@ def read_version(): with open("comtypes/__init__.py") as ofi: for line in ofi: if line.startswith("__version__ = "): - var, value = line.split('=') + var, value = line.split("=") return value.strip().strip('"').strip("'") raise NotImplementedError("__version__ is not found in __init__.py") @@ -25,32 +27,34 @@ def setUpClass(cls): def setUp(self): """prepare the same package that is usually uploaded to PyPI""" - subprocess.check_call([sys.executable, '-m', 'build', '--sdist']) + subprocess.check_call([sys.executable, "-m", "build", "--sdist"]) - filename_for_upload = f'comtypes-{self.version}.tar.gz' - self.target_package = os.path.join(os.getcwd(), 'dist', filename_for_upload) - self.pip_exe = os.path.join(os.path.dirname(sys.executable), 'Scripts', 'pip.exe') + filename_for_upload = f"comtypes-{self.version}.tar.gz" + self.target_package = os.path.join(os.getcwd(), "dist", filename_for_upload) + self.pip_exe = os.path.join( + os.path.dirname(sys.executable), "Scripts", "pip.exe" + ) def test_pip_install(self): """Test that "pip install comtypes-x.y.z.tar.gz" works""" - subprocess.check_call([self.pip_exe, 'install', self.target_package]) + subprocess.check_call([self.pip_exe, "install", self.target_package]) def test_py_typed_is_packaged(self): with tarfile.open(self.target_package, "r:gz") as tar: - self.assertIn( - f"comtypes-{self.version}/comtypes/py.typed", tar.getnames() - ) + self.assertIn(f"comtypes-{self.version}/comtypes/py.typed", tar.getnames()) def test_no_cache_dir_custom_location(self): """Test that 'pip install comtypes-x.y.z.tar.gz --no-cache-dir --target="...\custom location"' works""" - custom_dir = os.path.join(os.getcwd(), 'custom location') + custom_dir = os.path.join(os.getcwd(), "custom location") if os.path.exists(custom_dir): shutil.rmtree(custom_dir) os.makedirs(custom_dir) # this test catches issue #158 - subprocess.check_call(f'{self.pip_exe} install {self.target_package} --no-cache-dir --target="{custom_dir}"') + subprocess.check_call( + f'{self.pip_exe} install {self.target_package} --no-cache-dir --target="{custom_dir}"' + ) -if __name__ == '__main__': +if __name__ == "__main__": unittest.main() From 4d63bfedf8c31c83b9301ff4daea515e51b087bc Mon Sep 17 00:00:00 2001 From: junkmd Date: Thu, 1 Oct 2026 08:23:06 +0900 Subject: [PATCH 4/5] style: Split long command string in `test_no_cache_dir_custom_location`. --- test_pip_install.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test_pip_install.py b/test_pip_install.py index eaf1390f..ebd8ed4d 100644 --- a/test_pip_install.py +++ b/test_pip_install.py @@ -52,7 +52,8 @@ def test_no_cache_dir_custom_location(self): # this test catches issue #158 subprocess.check_call( - f'{self.pip_exe} install {self.target_package} --no-cache-dir --target="{custom_dir}"' + f"{self.pip_exe} install {self.target_package} " + f'--no-cache-dir --target="{custom_dir}"' ) From f22bba010f7dd5b0ce38e19ea8deaa6fe09fdc9a Mon Sep 17 00:00:00 2001 From: junkmd Date: Thu, 1 Oct 2026 08:23:06 +0900 Subject: [PATCH 5/5] ci: Include `test_pip_install.py` in `autofmt` workflow. --- .github/workflows/autofmt.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/autofmt.yml b/.github/workflows/autofmt.yml index d6d70f25..02f35052 100644 --- a/.github/workflows/autofmt.yml +++ b/.github/workflows/autofmt.yml @@ -16,6 +16,6 @@ jobs: - name: Install ruff run: pip install ruff==0.6.9 - name: Check format - run: python -m ruff format comtypes/. --check --diff + run: python -m ruff format test_pip_install.py comtypes/. --check --diff - name: Check lint - run: python -m ruff check --output-format=github comtypes/. + run: python -m ruff check --output-format=github test_pip_install.py comtypes/.