From 8c8c73d9794eb84c1639ee6c6c4758bf5880460c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 9 Sep 2024 10:13:48 -0700 Subject: [PATCH 01/14] [pre-commit.ci] pre-commit autoupdate (#478) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/tox-dev/pyproject-fmt: 2.2.1 → 2.2.3](https://github.com/tox-dev/pyproject-fmt/compare/2.2.1...2.2.3) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 4b981c1..2556d24 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -20,7 +20,7 @@ repos: - id: tox-ini-fmt args: ["-p", "fix"] - repo: https://github.com/tox-dev/pyproject-fmt - rev: "2.2.1" + rev: "2.2.3" hooks: - id: pyproject-fmt - repo: https://github.com/astral-sh/ruff-pre-commit From 8a90d5725be01ecfdc77d55b1c44038f7dce6bce Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Thu, 12 Sep 2024 15:59:25 +0200 Subject: [PATCH 02/14] Fix placement of return type when there is a doctest (#482) Resolves #480 --- src/sphinx_autodoc_typehints/patches.py | 15 +++++++++++++- tests/test_integration.py | 26 +++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/sphinx_autodoc_typehints/patches.py b/src/sphinx_autodoc_typehints/patches.py index e29a6dc..2307f53 100644 --- a/src/sphinx_autodoc_typehints/patches.py +++ b/src/sphinx_autodoc_typehints/patches.py @@ -5,8 +5,9 @@ from functools import lru_cache from typing import TYPE_CHECKING, Any +from docutils import nodes from docutils.parsers.rst.directives.admonitions import BaseAdmonition -from docutils.parsers.rst.states import Text +from docutils.parsers.rst.states import Body, Text from sphinx.ext.napoleon.docstring import GoogleDocstring from .attributes_patch import patch_attribute_handling @@ -110,6 +111,17 @@ def _patched_text_indent(self: Text, *args: Any) -> Any: return result +def _patched_body_doctest( + self: Body, _match: None, _context: None, next_state: str | None +) -> tuple[list[Any], str | None, list[Any]]: + line = self.document.current_line + 1 + data = "\n".join(self.state_machine.get_text_block()) + n = nodes.doctest_block(data, data) + n.line = line + self.parent += n + return [], next_state, [] + + def _patch_line_numbers() -> None: """ Make the rst parser put line numbers on more nodes. @@ -118,6 +130,7 @@ def _patch_line_numbers() -> None: """ Text.indent = _patched_text_indent BaseAdmonition.run = _patched_base_admonition_run + Body.doctest = _patched_body_doctest def install_patches(app: Sphinx) -> None: diff --git a/tests/test_integration.py b/tests/test_integration.py index cbd218e..3339916 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -1359,6 +1359,32 @@ def docstring_with_see_also() -> str: return "" +@expected( + """ + mod.has_doctest1() + + Test that we place the return type correctly when the function has + a doctest. + + Return type: + "None" + + >>> this is a fake doctest + a + >>> more doctest + b + """ +) +def has_doctest1() -> None: + r"""Test that we place the return type correctly when the function has a doctest. + + >>> this is a fake doctest + a + >>> more doctest + b + """ + + # Config settings for each test run. # Config Name: Sphinx Options as Dict. configs = { From bdf5359350300d82822232fe88a8910bb2568a53 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 16 Sep 2024 10:21:10 -0700 Subject: [PATCH 03/14] [pre-commit.ci] pre-commit autoupdate (#484) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- .pre-commit-config.yaml | 4 ++-- tox.ini | 16 ++++++++-------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 2556d24..43c6308 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -15,7 +15,7 @@ repos: - id: codespell additional_dependencies: ["tomli>=2.0.1"] - repo: https://github.com/tox-dev/tox-ini-fmt - rev: "1.3.2" + rev: "1.4.0" hooks: - id: tox-ini-fmt args: ["-p", "fix"] @@ -24,7 +24,7 @@ repos: hooks: - id: pyproject-fmt - repo: https://github.com/astral-sh/ruff-pre-commit - rev: "v0.6.4" + rev: "v0.6.5" hooks: - id: ruff-format - id: ruff diff --git a/tox.ini b/tox.ini index 39d8ba5..ec9a346 100644 --- a/tox.ini +++ b/tox.ini @@ -3,13 +3,13 @@ requires = tox>=4.2 env_list = fix - type - coverage - readme 3.13 3.12 3.11 3.10 + type + coverage + readme skip_missing_interpreters = true [testenv] @@ -41,6 +41,11 @@ deps = commands = pre-commit run --all-files --show-diff-on-failure +[testenv:3.13] +extras = + testing + type-comment + [testenv:type] description = run type check on code base deps = @@ -86,11 +91,6 @@ commands = pyproject-build -o {envtmpdir} --wheel --sdist . twine check {envtmpdir}/* -[testenv:3.13] -extras = - testing - type-comment - [testenv:dev] description = generate a DEV environment package = editable From ec010d0b436ea798569b92fdb494995f5f09dc9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bern=C3=A1t=20G=C3=A1bor?= Date: Tue, 17 Sep 2024 15:52:15 -0700 Subject: [PATCH 04/14] Improve the CI (#485) --- .github/workflows/check.yaml | 52 +++++++++++++++ .github/workflows/check.yml | 114 --------------------------------- .github/workflows/release.yaml | 48 ++++++++++++++ .github/workflows/release.yml | 27 -------- tox.ini | 67 ++++++------------- 5 files changed, 119 insertions(+), 189 deletions(-) create mode 100644 .github/workflows/check.yaml delete mode 100644 .github/workflows/check.yml create mode 100644 .github/workflows/release.yaml delete mode 100644 .github/workflows/release.yml diff --git a/.github/workflows/check.yaml b/.github/workflows/check.yaml new file mode 100644 index 0000000..0316ecb --- /dev/null +++ b/.github/workflows/check.yaml @@ -0,0 +1,52 @@ +name: check +on: + workflow_dispatch: + push: + branches: ["main"] + tags-ignore: ["**"] + pull_request: + schedule: + - cron: "0 8 * * *" + +concurrency: + group: check-${{ github.ref }} + cancel-in-progress: true + +jobs: + test: + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + env: + - "3.13" + - "3.12" + - "3.11" + - "3.10" + - type + - dev + - pkg_meta + steps: + - name: Install OS dependencies + run: sudo apt-get install graphviz -y + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + - name: Install the latest version of uv + uses: astral-sh/setup-uv@v3 + with: + enable-cache: true + cache-dependency-glob: "pyproject.toml" + github-token: ${{ secrets.GITHUB_TOKEN }} + - name: Install tox + run: uv tool install --python-preference only-managed --python 3.13 tox --with tox-uv + - name: Install Python + if: startsWith(matrix.env, '3.') && matrix.env != '3.13' + run: uv python install --python-preference only-managed ${{ matrix.env }} + - name: Setup test suite + run: tox run -vv --notest --skip-missing-interpreters false -e ${{ matrix.env }} + - name: Run test suite + run: tox run --skip-pkg-install -e ${{ matrix.env }} + env: + PYTEST_ADDOPTS: "-vv --durations=20" + DIFF_AGAINST: HEAD diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml deleted file mode 100644 index fd93e1c..0000000 --- a/.github/workflows/check.yml +++ /dev/null @@ -1,114 +0,0 @@ -name: check -on: - workflow_dispatch: - push: - branches: ["main"] - tags-ignore: ["**"] - pull_request: - schedule: - - cron: "0 8 * * *" - -concurrency: - group: check-${{ github.ref }} - cancel-in-progress: true - -jobs: - test: - name: test with CPython ${{ matrix.py }} - runs-on: ubuntu-latest - strategy: - fail-fast: false - matrix: - py: - - "3.13" - - "3.12" - - "3.11" - - "3.10" - steps: - - name: Setup python for tox - uses: actions/setup-python@v5 - with: - python-version: "3.12" - - name: Install tox - run: python -m pip install tox-uv - - uses: actions/checkout@v4 - with: - fetch-depth: 0 - - name: Setup python for test ${{ matrix.py }} - uses: actions/setup-python@v5 - with: - python-version: ${{ matrix.py }} - allow-prereleases: true - - name: setup test suite - run: tox run -vv --notest --skip-missing-interpreters false -e ${{ matrix.py }} - - name: run test suite - run: tox run --skip-pkg-install -e ${{ matrix.py }} - env: - PYTEST_ADDOPTS: "-vv --durations=20" - CI_RUN: "yes" - DIFF_AGAINST: HEAD - - name: Upload coverage data - uses: actions/upload-artifact@v4 - with: - include-hidden-files: true - name: .coverage.${{ matrix.py }} - path: ".tox/.coverage.*" - retention-days: 3 - - coverage: - name: Combine coverage - runs-on: ubuntu-latest - needs: test - steps: - - uses: actions/checkout@v4 - with: - fetch-depth: 0 - - uses: actions/setup-python@v5 - with: - python-version: "3.12" - - name: Install tox - run: python -m pip install tox-uv - - name: Setup coverage tool - run: tox -e coverage --notest - - name: Install package builder - run: python -m pip install build[uv] - - name: Build package - run: pyproject-build --wheel --installer uv . - - name: Download coverage data - uses: actions/download-artifact@v4 - with: - path: .tox - pattern: .coverage.* - merge-multiple: true - - name: Combine and report coverage - run: tox -e coverage - - name: Upload HTML report - uses: actions/upload-artifact@v4 - with: - name: html-report - path: .tox/htmlcov - - check: - name: tox env ${{ matrix.tox_env }} - runs-on: ubuntu-latest - strategy: - fail-fast: false - matrix: - tox_env: - - dev - - readme - - type - steps: - - uses: actions/checkout@v4 - with: - fetch-depth: 0 - - name: Setup Python "3.12" - uses: actions/setup-python@v5 - with: - python-version: "3.12" - - name: Install tox - run: python -m pip install tox-uv - - name: Setup test suite - run: tox -vv --notest --skip-missing-interpreters false -e ${{ matrix.tox_env }} - - name: Run test suite - run: tox --skip-pkg-install -e ${{ matrix.tox_env }} diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml new file mode 100644 index 0000000..49a6c8a --- /dev/null +++ b/.github/workflows/release.yaml @@ -0,0 +1,48 @@ +name: Release to PyPI +on: + push: + tags: ["*"] + +env: + dists-artifact-name: python-package-distributions + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + - name: Install the latest version of uv + uses: astral-sh/setup-uv@v3 + with: + enable-cache: true + cache-dependency-glob: "pyproject.toml" + github-token: ${{ secrets.GITHUB_TOKEN }} + - name: Build package + run: uv build --python 3.13 --python-preference only-managed --sdist --wheel . --out-dir dist + - name: Store the distribution packages + uses: actions/upload-artifact@v4 + with: + name: ${{ env.dists-artifact-name }} + path: dist/* + + release: + needs: + - build + runs-on: ubuntu-latest + environment: + name: release + url: https://pypi.org/project/sphinx-autodoc-typehints/${{ github.ref_name }} + permissions: + id-token: write + steps: + - name: Download all the dists + uses: actions/download-artifact@v4 + with: + name: ${{ env.dists-artifact-name }} + path: dist/ + - name: Publish to PyPI + uses: pypa/gh-action-pypi-publish@v1.10.1 + with: + attestations: true diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml deleted file mode 100644 index ceebf6e..0000000 --- a/.github/workflows/release.yml +++ /dev/null @@ -1,27 +0,0 @@ -name: Release to PyPI -on: - push: - tags: ["*"] - -jobs: - release: - runs-on: ubuntu-latest - environment: - name: release - url: https://pypi.org/p/sphinx-autodoc-typehints - permissions: - id-token: write - steps: - - name: Setup python to build package - uses: actions/setup-python@v5 - with: - python-version: "3.12" - - name: Install build - run: python -m pip install build - - uses: actions/checkout@v4 - with: - fetch-depth: 0 - - name: Build package - run: pyproject-build -s -w . -o dist - - name: Publish to PyPI - uses: pypa/gh-action-pypi-publish@v1.10.1 diff --git a/tox.ini b/tox.ini index ec9a346..7c95248 100644 --- a/tox.ini +++ b/tox.ini @@ -1,6 +1,7 @@ [tox] requires = tox>=4.2 + tox-uv>=1.11.3 env_list = fix 3.13 @@ -8,12 +9,11 @@ env_list = 3.11 3.10 type - coverage - readme + pkg_meta skip_missing_interpreters = true [testenv] -description = run tests with {basepython} +description = run the unit tests with pytest under {base_python} package = wheel wheel_build_env = .pkg extras = @@ -21,31 +21,27 @@ extras = testing type-comment pass_env = - PIP_* + DIFF_AGAINST PYTEST_* set_env = - COVERAGE_FILE = {toxworkdir}{/}.coverage.{envname} + COVERAGE_FILE = {work_dir}/.coverage.{env_name} commands = - pytest {tty:--color=yes} {posargs: \ - --junitxml {toxworkdir}{/}junit.{envname}.xml --cov {envsitepackagesdir}{/}sphinx_autodoc_typehints --cov {toxinidir}{/}tests \ + python -m pytest {tty:--color=yes} {posargs: \ + --cov {env_site_packages_dir}{/}sphinx_autodoc_typehints --cov {tox_root}{/}tests \ --cov-config=pyproject.toml --no-cov-on-fail --cov-report term-missing:skip-covered --cov-context=test \ - --cov-report html:{envtmpdir}{/}htmlcov --cov-report xml:{toxworkdir}{/}coverage.{envname}.xml \ + --cov-report html:{env_tmp_dir}{/}htmlcov --cov-report xml:{work_dir}{/}coverage.{env_name}.xml \ + --junitxml {work_dir}{/}junit.{env_name}.xml \ tests} - diff-cover --compare-branch {env:DIFF_AGAINST:origin/main} {toxworkdir}{/}coverage.{envname}.xml + diff-cover --compare-branch {env:DIFF_AGAINST:origin/main} {work_dir}{/}coverage.{env_name}.xml --fail-under 100 [testenv:fix] description = format the code base to adhere to our styles, and complain about what we cannot do automatically skip_install = true deps = - pre-commit-uv>=4.1 + pre-commit-uv>=4.1.1 commands = pre-commit run --all-files --show-diff-on-failure -[testenv:3.13] -extras = - testing - type-comment - [testenv:type] description = run type check on code base deps = @@ -55,46 +51,21 @@ commands = mypy src mypy tests -[testenv:coverage] -description = combine coverage files and generate diff (against DIFF_AGAINST defaulting to origin/main) -skip_install = true -deps = - covdefaults>=2.3 - coverage>=7.6.1 - diff-cover>=9.1.1 -extras = -parallel_show_output = true -pass_env = - DIFF_AGAINST -set_env = - COVERAGE_FILE = {toxworkdir}/.coverage -commands = - coverage combine - coverage report --skip-covered --show-missing - coverage xml -o {toxworkdir}/coverage.xml - coverage html -d {toxworkdir}/htmlcov - diff-cover --compare-branch {env:DIFF_AGAINST:origin/main} {toxworkdir}/coverage.xml -depends = - 3.13 - 3.12 - 3.11 - 3.10 - -[testenv:readme] -description = check that the long description is valid (need for PyPI) +[testenv:pkg_meta] +description = check that the long description is valid skip_install = true deps = - build[virtualenv]>=1.2.2 + check-wheel-contents>=0.6 twine>=5.1.1 -extras = + uv>=0.4.10 commands = - pyproject-build -o {envtmpdir} --wheel --sdist . - twine check {envtmpdir}/* + uv build --sdist --wheel --out-dir {env_tmp_dir} . + twine check {env_tmp_dir}{/}* + check-wheel-contents --no-config {env_tmp_dir} [testenv:dev] description = generate a DEV environment package = editable commands = - python -m pip list --format=columns + uv pip tree python -c 'import sys; print(sys.executable)' -uv_seed = true From 658bc809b4a8594e09ebbb9d699beec59ff956f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bern=C3=A1t=20G=C3=A1bor?= Date: Tue, 17 Sep 2024 15:53:52 -0700 Subject: [PATCH 05/14] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 745ece3..5a8b70d 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ [![Supported Python versions](https://img.shields.io/pypi/pyversions/sphinx-autodoc-typehints.svg)](https://pypi.org/project/sphinx-autodoc-typehints/) [![Downloads](https://pepy.tech/badge/sphinx-autodoc-typehints/month)](https://pepy.tech/project/sphinx-autodoc-typehints) -[![check](https://github.com/tox-dev/sphinx-autodoc-typehints/actions/workflows/check.yml/badge.svg)](https://github.com/tox-dev/sphinx-autodoc-typehints/actions/workflows/check.yml) +[![check](https://github.com/tox-dev/sphinx-autodoc-typehints/actions/workflows/check.yaml/badge.svg)](https://github.com/tox-dev/sphinx-autodoc-typehints/actions/workflows/check.yaml) This extension allows you to use Python 3 annotations for documenting acceptable argument types and return value types of functions. See an example of the Sphinx render at the From 51f6cb37bf99c07e52b2a06a5a3abd47f742fd2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bern=C3=A1t=20G=C3=A1bor?= Date: Wed, 18 Sep 2024 10:20:04 -0700 Subject: [PATCH 06/14] Fix broken link in README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5a8b70d..ce70947 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ versions](https://img.shields.io/pypi/pyversions/sphinx-autodoc-typehints.svg)]( This extension allows you to use Python 3 annotations for documenting acceptable argument types and return value types of functions. See an example of the Sphinx render at the -[pyproject-api docs](https://pyproject-api.readthedocs.io/en/latest/). +[pyproject-api docs](https://pyproject-api.readthedocs.io/latest/). This allows you to use type hints in a very natural fashion, allowing you to migrate from this: From 4b19ed2c51b0163e1c25339589078a3581aa407c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bern=C3=A1t=20G=C3=A1bor?= Date: Wed, 18 Sep 2024 10:20:33 -0700 Subject: [PATCH 07/14] Link to API directly --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ce70947..7ddbf09 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ versions](https://img.shields.io/pypi/pyversions/sphinx-autodoc-typehints.svg)]( This extension allows you to use Python 3 annotations for documenting acceptable argument types and return value types of functions. See an example of the Sphinx render at the -[pyproject-api docs](https://pyproject-api.readthedocs.io/latest/). +[pyproject-api docs](https://pyproject-api.readthedocs.io/latest/api.html). This allows you to use type hints in a very natural fashion, allowing you to migrate from this: From c632481355ddfa3ca867d9af4358e1a2c37da03a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 23 Sep 2024 16:30:38 -0700 Subject: [PATCH 08/14] [pre-commit.ci] pre-commit autoupdate (#489) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- .pre-commit-config.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 43c6308..4c2089c 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -15,16 +15,16 @@ repos: - id: codespell additional_dependencies: ["tomli>=2.0.1"] - repo: https://github.com/tox-dev/tox-ini-fmt - rev: "1.4.0" + rev: "1.4.1" hooks: - id: tox-ini-fmt args: ["-p", "fix"] - repo: https://github.com/tox-dev/pyproject-fmt - rev: "2.2.3" + rev: "2.2.4" hooks: - id: pyproject-fmt - repo: https://github.com/astral-sh/ruff-pre-commit - rev: "v0.6.5" + rev: "v0.6.7" hooks: - id: ruff-format - id: ruff From 73293b9a578f36d8a0abe97b1f985e7ca4023c7c Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 23 Sep 2024 16:31:45 -0700 Subject: [PATCH 09/14] Bump pypa/gh-action-pypi-publish from 1.10.1 to 1.10.2 (#488) Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/release.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 49a6c8a..70ef2ac 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -43,6 +43,6 @@ jobs: name: ${{ env.dists-artifact-name }} path: dist/ - name: Publish to PyPI - uses: pypa/gh-action-pypi-publish@v1.10.1 + uses: pypa/gh-action-pypi-publish@v1.10.2 with: attestations: true From 70b7c4fe21a01ae6e74005612ae2a121946ee206 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 30 Sep 2024 20:32:40 -0700 Subject: [PATCH 10/14] [pre-commit.ci] pre-commit autoupdate (#490) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- .pre-commit-config.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 4c2089c..463affa 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -5,7 +5,7 @@ repos: - id: end-of-file-fixer - id: trailing-whitespace - repo: https://github.com/python-jsonschema/check-jsonschema - rev: 0.29.2 + rev: 0.29.3 hooks: - id: check-github-workflows args: ["--verbose"] @@ -24,7 +24,7 @@ repos: hooks: - id: pyproject-fmt - repo: https://github.com/astral-sh/ruff-pre-commit - rev: "v0.6.7" + rev: "v0.6.8" hooks: - id: ruff-format - id: ruff From 07c27518fde4e18493e8a2a8ea766da25412bead Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bern=C3=A1t=20G=C3=A1bor?= Date: Mon, 7 Oct 2024 12:01:42 -0700 Subject: [PATCH 11/14] Fix the type checker (#493) --- src/sphinx_autodoc_typehints/patches.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sphinx_autodoc_typehints/patches.py b/src/sphinx_autodoc_typehints/patches.py index 2307f53..236b38d 100644 --- a/src/sphinx_autodoc_typehints/patches.py +++ b/src/sphinx_autodoc_typehints/patches.py @@ -129,7 +129,7 @@ def _patch_line_numbers() -> None: When the line numbers are missing, we have a hard time placing the :rtype:. """ Text.indent = _patched_text_indent - BaseAdmonition.run = _patched_base_admonition_run + BaseAdmonition.run = _patched_base_admonition_run # type: ignore[method-assign,assignment] Body.doctest = _patched_body_doctest From e24bf5ec675692b8f632407afc35de4e5d24e9df Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 7 Oct 2024 12:01:52 -0700 Subject: [PATCH 12/14] Bump pypa/gh-action-pypi-publish from 1.10.2 to 1.10.3 (#491) Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/release.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 70ef2ac..c163260 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -43,6 +43,6 @@ jobs: name: ${{ env.dists-artifact-name }} path: dist/ - name: Publish to PyPI - uses: pypa/gh-action-pypi-publish@v1.10.2 + uses: pypa/gh-action-pypi-publish@v1.10.3 with: attestations: true From e17a669da15e9cdd8728a9290dadee8dcb59c95e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 7 Oct 2024 12:02:01 -0700 Subject: [PATCH 13/14] [pre-commit.ci] pre-commit autoupdate (#492) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- .pre-commit-config.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 463affa..23726ed 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,6 +1,6 @@ repos: - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.6.0 + rev: v5.0.0 hooks: - id: end-of-file-fixer - id: trailing-whitespace @@ -24,7 +24,7 @@ repos: hooks: - id: pyproject-fmt - repo: https://github.com/astral-sh/ruff-pre-commit - rev: "v0.6.8" + rev: "v0.6.9" hooks: - id: ruff-format - id: ruff From 244af2f0c18944a4cb3e69c86c886c6dd80aca78 Mon Sep 17 00:00:00 2001 From: Priyansh Agrawal Date: Wed, 9 Oct 2024 02:40:58 +0100 Subject: [PATCH 14/14] Apply typehints_formatter to signature (#494) * Apply typehints_formatter to signature * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix lint --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- src/sphinx_autodoc_typehints/__init__.py | 17 ++++++++++- tests/test_integration.py | 36 ++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/src/sphinx_autodoc_typehints/__init__.py b/src/sphinx_autodoc_typehints/__init__.py index d2b1b06..e67180f 100644 --- a/src/sphinx_autodoc_typehints/__init__.py +++ b/src/sphinx_autodoc_typehints/__init__.py @@ -354,9 +354,24 @@ def process_signature( # noqa: C901, PLR0913, PLR0917 obj = inspect.unwrap(obj) sph_signature = sphinx_signature(obj, type_aliases=app.config["autodoc_type_aliases"]) + typehints_formatter: Callable[..., str | None] | None = getattr(app.config, "typehints_formatter", None) + + def _get_formatted_annotation(annotation: TypeVar) -> TypeVar: + if typehints_formatter is None: + return annotation + formatted_name = typehints_formatter(annotation) + return annotation if not isinstance(formatted_name, str) else TypeVar(formatted_name) + + if app.config.typehints_use_signature_return: + sph_signature = sph_signature.replace( + return_annotation=_get_formatted_annotation(sph_signature.return_annotation) + ) if app.config.typehints_use_signature: - parameters = list(sph_signature.parameters.values()) + parameters = [ + param.replace(annotation=_get_formatted_annotation(param.annotation)) + for param in sph_signature.parameters.values() + ] else: parameters = [param.replace(annotation=inspect.Parameter.empty) for param in sph_signature.parameters.values()] diff --git a/tests/test_integration.py b/tests/test_integration.py index 3339916..93aa1e5 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -1385,6 +1385,42 @@ def has_doctest1() -> None: """ +Unformatted = TypeVar("Unformatted") + + +@warns("cannot cache unpickable configuration value: 'typehints_formatter'") +@expected( + """ + mod.typehints_formatter_applied_to_signature(param: Formatted) -> Formatted + + Do nothing + + Parameters: + **param** (Formatted) -- A parameter + + Return type: + Formatted + + Returns: + The return value + """, + typehints_use_signature=True, + typehints_use_signature_return=True, + typehints_formatter=lambda _, __=None: "Formatted", +) +def typehints_formatter_applied_to_signature(param: Unformatted) -> Unformatted: + """ + Do nothing + + Args: + param: A parameter + + Returns: + The return value + """ + return param + + # Config settings for each test run. # Config Name: Sphinx Options as Dict. configs = {