Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type annotate the public API #167

Merged
merged 21 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
62fb515
Add changelog section for next release
pradyunsg Apr 7, 2023
cb566f5
Type annotate the public API
pradyunsg Apr 4, 2023
88906ab
Update configuration for `ruff`
pradyunsg Apr 5, 2023
69ed8b1
Drop support for Python 3.7
pradyunsg Apr 5, 2023
a2cd373
Add support for Python 3.12
pradyunsg Apr 7, 2023
87c779b
Enforce a Python version in Read the Docs builds
pradyunsg Apr 7, 2023
8a2adbf
Add changelog entry for type annotations
pradyunsg Apr 7, 2023
9cf1649
Revert "Drop support for Python 3.7"
pradyunsg Apr 8, 2023
26460ba
Add a `py.typed` file to denote type annotated sources
pradyunsg Apr 8, 2023
58e2814
Use more generic types in type annotations
pradyunsg Apr 8, 2023
96a6691
Correct the type of `prepare_metadata_for_build_wheel`
pradyunsg Apr 8, 2023
5b264f5
Use the correct directory for documentation gitignore
pradyunsg Apr 8, 2023
1017004
Drop the return types from docstrings
pradyunsg Apr 8, 2023
cffda1f
Don't set an incorrect name on `nox -s release`
pradyunsg Apr 8, 2023
12401f6
Fully document all parameters in the public API
pradyunsg Apr 8, 2023
db5ed31
Merge branch 'main' into typing
pradyunsg Apr 8, 2023
5c83df5
Expose `SubprocessRunner` for type checking
pradyunsg Apr 8, 2023
375b8fc
Don't meddle with `__all__`
pradyunsg Apr 8, 2023
228aef1
Document the usage of the subprocess protocol for type checking
pradyunsg Apr 8, 2023
47a3347
Merge branch 'main' into typing
pradyunsg Oct 31, 2023
1eea736
Remove whitespace that was accidentally added in a merge commit
pradyunsg Nov 1, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11"]
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11", "3.12"]
os: [Ubuntu, macOS, Windows]

steps:
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ __pycache__/
/dist/
.nox
.pytest_cache
doc/_build/
docs/_build/
*.egg-info/
.coverage
htmlcov/
6 changes: 6 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ repos:
hooks:
- id: black

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.1.1
hooks:
- id: mypy
exclude: tests/samples

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
Expand Down
1 change: 1 addition & 0 deletions .readthedocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ sphinx:
configuration: docs/conf.py

python:
version: 3.8
install:
- requirements: docs/requirements.txt
- method: pip
Expand Down
5 changes: 5 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Changelog
=========

v1.1
----

- Add type annotations to the public API.

v1.0
----

Expand Down
3 changes: 3 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@
# -- Options for autodoc -----------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html#configuration

autodoc_class_signature = "separated"
autodoc_member_order = "bysource"
autodoc_preserve_defaults = True
autodoc_typehints = "description"
autodoc_typehints_description_target = "documented_params"

# -- Options for intersphinx -------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/extensions/intersphinx.html#configuration
Expand Down
21 changes: 17 additions & 4 deletions docs/pyproject_hooks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,31 @@ Custom Subprocess Runners

It is possible to provide a custom subprocess runner, that behaves differently. The expected protocol for subprocess runners is as follows:

.. function:: subprocess_runner_protocol(cmd, cwd, extra_environ)
.. function:: subprocess_runner_protocol(cmd, cwd=None, extra_environ=None)
:noindex:

:param cmd: The command and arguments to execute, as would be passed to :func:`subprocess.run`.
:type cmd: list[str]
:type cmd: typing.Sequence[str]
:param cwd: The working directory that must be used for the subprocess.
:type cwd: str
:type cwd: typing.Optional[str]
:param extra_environ: Mapping of environment variables (name to value) which must be set for the subprocess execution.
:type extra_environ: dict[str, str]
:type extra_environ: typing.Optional[typing.Mapping[str, str]]

:rtype: None

Since this codebase is currently Python 3.7-compatible, the type annotation for this protocol is only available to type checkers. To annotate a variable as a subprocess runner, you can do something along the lines of:

.. code-block:: python

from typing import TYPE_CHECKING

if TYPE_CHECKING:
from pyproject_hooks import SubprocessRunner

# Example usage
def build(awesome_runner: "SubprocessRunner") -> None:
...

Exceptions
----------

Expand Down
4 changes: 2 additions & 2 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
nox.options.reuse_existing_virtualenvs = True


@nox.session(python=["3.7", "3.8", "3.9", "3.10", "3.11", "pypy3"])
@nox.session(python=["3.7", "3.8", "3.9", "3.10", "3.11", "3.12", "pypy3"])
def test(session: nox.Session) -> None:
session.install("-r", "dev-requirements.txt")
session.install(".")
Expand Down Expand Up @@ -40,7 +40,7 @@ def lint(session: nox.Session) -> None:
session.run("pre-commit", "run", *args)


@nox.session(name="docs-live")
@nox.session
def release(session: nox.Session) -> None:
session.install("flit")
session.run("flit", "publish")
7 changes: 5 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,8 @@ Source = "https://github.com/pypa/pyproject-hooks"
Documentation = "https://pyproject-hooks.readthedocs.io/"
Changelog = "https://pyproject-hooks.readthedocs.io/en/latest/changelog.html"

[tool.isort]
profile = "black"
[tool.ruff]
src = ["src", "tests"]

[tool.ruff.isort]
known-first-party = ["pyproject_hooks", "tests"]
5 changes: 5 additions & 0 deletions src/pyproject_hooks/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Wrappers to call pyproject.toml-based build backend hooks.
"""

from typing import TYPE_CHECKING

from ._impl import (
BackendUnavailable,
BuildBackendHookCaller,
Expand All @@ -19,3 +21,6 @@
"quiet_subprocess_runner",
"BuildBackendHookCaller",
]

if TYPE_CHECKING:
from ._impl import SubprocessRunner # noqa: F401
Loading