Skip to content

Commit 0b036b5

Browse files
authored
Merge pull request #167 from pradyunsg/typing
Type annotate the public API
2 parents 084b02e + 1eea736 commit 0b036b5

File tree

12 files changed

+150
-44
lines changed

12 files changed

+150
-44
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ jobs:
2929
strategy:
3030
fail-fast: false
3131
matrix:
32-
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11"]
32+
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11", "3.12"]
3333
os: [Ubuntu, macOS, Windows]
3434

3535
steps:

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ __pycache__/
33
/dist/
44
.nox
55
.pytest_cache
6-
doc/_build/
6+
docs/_build/
77
*.egg-info/
88
.coverage
99
htmlcov/

.pre-commit-config.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ repos:
99
hooks:
1010
- id: black
1111

12+
- repo: https://github.com/pre-commit/mirrors-mypy
13+
rev: v1.1.1
14+
hooks:
15+
- id: mypy
16+
exclude: tests/samples
17+
1218
- repo: https://github.com/pre-commit/pre-commit-hooks
1319
rev: v4.4.0
1420
hooks:

.readthedocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ sphinx:
44
configuration: docs/conf.py
55

66
python:
7+
version: 3.8
78
install:
89
- requirements: docs/requirements.txt
910
- method: pip

docs/changelog.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
Changelog
22
=========
33

4+
v1.1
5+
----
6+
7+
- Add type annotations to the public API.
8+
49
v1.0
510
----
611

docs/conf.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,11 @@
3030
# -- Options for autodoc -----------------------------------------------------
3131
# https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html#configuration
3232

33+
autodoc_class_signature = "separated"
3334
autodoc_member_order = "bysource"
35+
autodoc_preserve_defaults = True
3436
autodoc_typehints = "description"
37+
autodoc_typehints_description_target = "documented_params"
3538

3639
# -- Options for intersphinx -------------------------------------------------
3740
# https://www.sphinx-doc.org/en/master/usage/extensions/intersphinx.html#configuration

docs/pyproject_hooks.rst

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,31 @@ Custom Subprocess Runners
2222

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

25-
.. function:: subprocess_runner_protocol(cmd, cwd, extra_environ)
25+
.. function:: subprocess_runner_protocol(cmd, cwd=None, extra_environ=None)
2626
:noindex:
2727

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

3535
:rtype: None
3636

37+
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:
38+
39+
.. code-block:: python
40+
41+
from typing import TYPE_CHECKING
42+
43+
if TYPE_CHECKING:
44+
from pyproject_hooks import SubprocessRunner
45+
46+
# Example usage
47+
def build(awesome_runner: "SubprocessRunner") -> None:
48+
...
49+
3750
Exceptions
3851
----------
3952

noxfile.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
nox.options.reuse_existing_virtualenvs = True
77

88

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

4242

43-
@nox.session(name="docs-live")
43+
@nox.session
4444
def release(session: nox.Session) -> None:
4545
session.install("flit")
4646
session.run("flit", "publish")

pyproject.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,8 @@ Source = "https://github.com/pypa/pyproject-hooks"
2222
Documentation = "https://pyproject-hooks.readthedocs.io/"
2323
Changelog = "https://pyproject-hooks.readthedocs.io/en/latest/changelog.html"
2424

25-
[tool.isort]
26-
profile = "black"
25+
[tool.ruff]
26+
src = ["src", "tests"]
27+
28+
[tool.ruff.isort]
29+
known-first-party = ["pyproject_hooks", "tests"]

src/pyproject_hooks/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"""Wrappers to call pyproject.toml-based build backend hooks.
22
"""
33

4+
from typing import TYPE_CHECKING
5+
46
from ._impl import (
57
BackendUnavailable,
68
BuildBackendHookCaller,
@@ -19,3 +21,6 @@
1921
"quiet_subprocess_runner",
2022
"BuildBackendHookCaller",
2123
]
24+
25+
if TYPE_CHECKING:
26+
from ._impl import SubprocessRunner # noqa: F401

0 commit comments

Comments
 (0)