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..05fca61 100644 --- a/src/pyproject_api/__main__.py +++ b/src/pyproject_api/__main__.py @@ -1,5 +1,11 @@ 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..9f02250 100644 --- a/src/pyproject_api/_backend.py +++ b/src/pyproject_api/_backend.py @@ -5,6 +5,13 @@ 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