From 601d0b1929c4612a82ebc05f66b313e19b6f1af7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bern=C3=A1t=20G=C3=A1bor?= Date: Tue, 17 Nov 2026 22:56:18 -0800 Subject: [PATCH 1/2] =?UTF-8?q?=E2=9A=A1=20perf(import):=20defer=20pyproje?= =?UTF-8?q?ct-api=20modules?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Importing the public API and opening CLI help load frontend dependencies before either path uses them on Python 3.15. Defer eligible modules until their operations run. --- .pre-commit-config.yaml | 6 ++++++ docs/changelog.d/+lazy-imports.performance.rst | 1 + src/pyproject_api/__init__.py | 6 ++++++ src/pyproject_api/__main__.py | 2 ++ src/pyproject_api/_backend.py | 2 ++ src/pyproject_api/_frontend.py | 8 ++++++++ src/pyproject_api/_util.py | 2 ++ src/pyproject_api/_via_fresh_subprocess.py | 2 ++ 8 files changed, 29 insertions(+) create mode 100644 docs/changelog.d/+lazy-imports.performance.rst diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 9469465..17bbb7a 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -44,6 +44,12 @@ repos: - id: ruff priority: 5 args: ["--fix", "--unsafe-fixes", "--exit-non-zero-on-fix"] + - repo: https://github.com/henryiii/flake8-lazy + rev: 7f268b3b3aaf7feca756ca610e37971382976365 # frozen: v0.11.0 + hooks: + - id: flake8-lazy + priority: 4 + files: ^src/pyproject_api/ - repo: https://github.com/gaborbernat/simplint-pre-commit rev: "a12d8eaac290be24041a3e2778c636c56453722e" # frozen: v0.12.2 hooks: diff --git a/docs/changelog.d/+lazy-imports.performance.rst b/docs/changelog.d/+lazy-imports.performance.rst new file mode 100644 index 0000000..3af7433 --- /dev/null +++ b/docs/changelog.d/+lazy-imports.performance.rst @@ -0,0 +1 @@ +Importing ``pyproject-api`` and opening CLI help are faster on Python 3.15 because unused modules load when needed. diff --git a/src/pyproject_api/__init__.py b/src/pyproject_api/__init__.py index b969461..ccb7024 100644 --- a/src/pyproject_api/__init__.py +++ b/src/pyproject_api/__init__.py @@ -2,6 +2,12 @@ from __future__ import annotations +__lazy_modules__ = [ + f"{__spec__.parent}._frontend", # ty: ignore[unresolved-attribute] # imported modules have a spec + f"{__spec__.parent}._version", # ty: ignore[unresolved-attribute] # imported modules have a spec + f"{__spec__.parent}._via_fresh_subprocess", # ty: ignore[unresolved-attribute] # imported modules have a spec +] + from ._frontend import ( BackendFailed, CmdStatus, diff --git a/src/pyproject_api/__main__.py b/src/pyproject_api/__main__.py index 8b9ee46..a81e8b7 100644 --- a/src/pyproject_api/__main__.py +++ b/src/pyproject_api/__main__.py @@ -1,5 +1,7 @@ from __future__ import annotations # ruff:ignore[undocumented-public-module] # entry point for python -m, not an importable API +__lazy_modules__ = ["argparse", "pathlib", f"{__spec__.parent}._via_fresh_subprocess"] # ty: ignore[unresolved-attribute] # imported modules have a spec + import argparse import os import pathlib diff --git a/src/pyproject_api/_backend.py b/src/pyproject_api/_backend.py index e6dd909..30eecf7 100644 --- a/src/pyproject_api/_backend.py +++ b/src/pyproject_api/_backend.py @@ -5,6 +5,8 @@ See https://tox.readthedocs.io/en/rewrite/development.html#code-style-guide """ +__lazy_modules__ = ["importlib", "json", "locale", "traceback"] + import importlib import json import locale diff --git a/src/pyproject_api/_frontend.py b/src/pyproject_api/_frontend.py index b71d4f0..07c6eab 100644 --- a/src/pyproject_api/_frontend.py +++ b/src/pyproject_api/_frontend.py @@ -2,6 +2,14 @@ from __future__ import annotations +__lazy_modules__ = [ + "json", + "tempfile", + "tomllib", + "zipfile", + f"{__spec__.parent}._util", # ty: ignore[unresolved-attribute] # imported modules have a spec +] + import json import tomllib from abc import ABC, abstractmethod diff --git a/src/pyproject_api/_util.py b/src/pyproject_api/_util.py index 32c69d2..1733f1d 100644 --- a/src/pyproject_api/_util.py +++ b/src/pyproject_api/_util.py @@ -1,5 +1,7 @@ from __future__ import annotations +__lazy_modules__ = ["shutil"] + from shutil import rmtree from typing import TYPE_CHECKING diff --git a/src/pyproject_api/_via_fresh_subprocess.py b/src/pyproject_api/_via_fresh_subprocess.py index a14606e..417b2d5 100644 --- a/src/pyproject_api/_via_fresh_subprocess.py +++ b/src/pyproject_api/_via_fresh_subprocess.py @@ -1,5 +1,7 @@ from __future__ import annotations +__lazy_modules__ = ["subprocess"] + import os import sys from contextlib import contextmanager From 8c7a6e3baf6a0b0cf2a6809e2352e85442ab1d0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bern=C3=A1t=20G=C3=A1bor?= Date: Tue, 17 Nov 2026 23:53:14 -0800 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=8E=A8=20style(import):=20format=20la?= =?UTF-8?q?zy=20module=20lists?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pyproject_api/__main__.py | 6 +++++- src/pyproject_api/_backend.py | 7 ++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/pyproject_api/__main__.py b/src/pyproject_api/__main__.py index a81e8b7..05fca61 100644 --- a/src/pyproject_api/__main__.py +++ b/src/pyproject_api/__main__.py @@ -1,6 +1,10 @@ from __future__ import annotations # ruff:ignore[undocumented-public-module] # entry point for python -m, not an importable API -__lazy_modules__ = ["argparse", "pathlib", f"{__spec__.parent}._via_fresh_subprocess"] # ty: ignore[unresolved-attribute] # imported modules have a spec +__lazy_modules__ = [ + "argparse", + "pathlib", + f"{__spec__.parent}._via_fresh_subprocess", # ty: ignore[unresolved-attribute] # imported modules have a spec +] import argparse import os diff --git a/src/pyproject_api/_backend.py b/src/pyproject_api/_backend.py index 30eecf7..9f02250 100644 --- a/src/pyproject_api/_backend.py +++ b/src/pyproject_api/_backend.py @@ -5,7 +5,12 @@ See https://tox.readthedocs.io/en/rewrite/development.html#code-style-guide """ -__lazy_modules__ = ["importlib", "json", "locale", "traceback"] +__lazy_modules__ = [ + "importlib", + "json", + "locale", + "traceback", +] import importlib import json