From 71e09396f40729e410cfc9771d852f6e4ea49361 Mon Sep 17 00:00:00 2001 From: Philipp A Date: Mon, 6 Sep 2021 12:39:21 +0200 Subject: [PATCH 1/5] Handle misspecified args to `dict` --- scanpydoc/elegant_typehints/formatting.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scanpydoc/elegant_typehints/formatting.py b/scanpydoc/elegant_typehints/formatting.py index 09192ac..060c647 100644 --- a/scanpydoc/elegant_typehints/formatting.py +++ b/scanpydoc/elegant_typehints/formatting.py @@ -72,8 +72,8 @@ def _format_terse( return f":py:class:`{tilde}typing.Mapping`" # display dict as {k: v} - if origin is dict: - k, v = get_args(annotation) + if origin is dict and len(args) == 2: + k, v = args return f"{{{fmt(k)}: {fmt(v)}}}" # display Callable[[a1, a2], r] as (a1, a2) -> r From d35555d8f2a4f7b1aca1b2b76547d76fad076f65 Mon Sep 17 00:00:00 2001 From: Philipp A Date: Mon, 6 Sep 2021 12:49:58 +0200 Subject: [PATCH 2/5] Switch to actions and pre-commit ci --- .github/workflows/ci.yml | 35 +++++++++++++++++++++++++++++++++++ .travis.yml | 22 ---------------------- pyproject.toml | 4 +++- 3 files changed, 38 insertions(+), 23 deletions(-) create mode 100644 .github/workflows/ci.yml delete mode 100644 .travis.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..5387404 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,35 @@ +# https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions + +name: Python tests + +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + +jobs: + build: + runs-on: ubuntu-latest + strategy: + matrix: + python-version: ['3.7', '3.8', '3.9'] + steps: + - uses: actions/checkout@v2 + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: ${{ matrix.python-version }} + - name: Restore pip and pre-commit caches + uses: actions/cache@v2 + with: + path: | + ~/.cache/pip + ~/.cache/pre-commit + key: ${{ runner.os }}-pip-${{ hashFiles('pyproject.toml') }} + - name: Install dependencies + run: | + pip install --upgrade pip wheel + pip install .[test] + - name: Tests + run: pytest --color=yes diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 3a8dcb5..0000000 --- a/.travis.yml +++ /dev/null @@ -1,22 +0,0 @@ -dist: xenial -language: python -cache: pip -branches: - only: - - master # use PR builder only for other branches -python: -# Travis uses old versions if you specify 3.x, -# and elegant_typehints trigger a Python bug in those. -# There seems to be no way to specify the newest patch version, -# so I’ll just use the newest available at the time of writing. -- '3.7.9' -- '3.8.5' -- '3.9' - -install: -- pip install flit codecov -- flit install --deps develop -script: -- PYTHONPATH=. pytest --cov=scanpydoc --black -- rst2html.py --halt=2 README.rst >/dev/null -after_success: codecov diff --git a/pyproject.toml b/pyproject.toml index 26ec669..d772b69 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -24,10 +24,12 @@ requires = [ ] [tool.flit.metadata.requires-extra] +dev = [ + 'pre-commit', +] test = [ 'pytest', 'pytest-cov', - 'pytest-black', 'typing_extensions>=3.10; python_version<"3.8"', ] doc = [ From 9aa7699821801749c37e76f5c5c6d82a4f957037 Mon Sep 17 00:00:00 2001 From: Philipp A Date: Mon, 6 Sep 2021 12:57:29 +0200 Subject: [PATCH 3/5] isort --- .pre-commit-config.yaml | 14 ++++++++++++++ docs/conf.py | 2 ++ pyproject.toml | 5 +++++ scanpydoc/autosummary_generate_imported.py | 2 +- scanpydoc/definition_list_typed_field.py | 4 ++-- scanpydoc/elegant_typehints/__init__.py | 5 ++--- scanpydoc/elegant_typehints/autodoc_patch.py | 2 +- scanpydoc/elegant_typehints/example.py | 2 +- scanpydoc/elegant_typehints/formatting.py | 9 ++++----- scanpydoc/elegant_typehints/return_tuple.py | 3 ++- scanpydoc/rtd_github_links.py | 2 +- tests/test_elegant_typehints.py | 5 +++-- tests/test_rtd_github_links.py | 3 ++- 13 files changed, 40 insertions(+), 18 deletions(-) create mode 100644 .pre-commit-config.yaml diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..871dc57 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,14 @@ +repos: +- repo: https://github.com/pre-commit/pre-commit-hooks + rev: v2.3.0 + hooks: + - id: trailing-whitespace +- repo: https://github.com/psf/black + rev: 20.8b1 # https://github.com/psf/black/tags + hooks: + - id: black + language_version: python3 +- repo: https://github.com/pycqa/isort + rev: 5.7.0 + hooks: + - id: isort diff --git a/docs/conf.py b/docs/conf.py index 5ac6793..bba1556 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -4,11 +4,13 @@ from sphinx.application import Sphinx + # Allow importing scanpydoc itself HERE = Path(__file__).parent sys.path.insert(0, str(HERE.parent)) import scanpydoc # noqa + # Clean build env for file in HERE.glob("scanpydoc.*.rst"): file.unlink() diff --git a/pyproject.toml b/pyproject.toml index d772b69..0796a19 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -43,6 +43,11 @@ scanpydoc = 'scanpydoc.theme' [tool.black] target-version = ['py37'] +[tool.isort] +profile = 'black' +lines_after_imports = 2 +multi_line_output = 3 + [tool.tox] legacy_tox_ini = """ [tox] diff --git a/scanpydoc/autosummary_generate_imported.py b/scanpydoc/autosummary_generate_imported.py index 5fa90d7..1732f5b 100644 --- a/scanpydoc/autosummary_generate_imported.py +++ b/scanpydoc/autosummary_generate_imported.py @@ -12,7 +12,7 @@ import logging from pathlib import Path -from typing import Dict, Any +from typing import Any, Dict from sphinx.application import Sphinx from sphinx.ext import autosummary diff --git a/scanpydoc/definition_list_typed_field.py b/scanpydoc/definition_list_typed_field.py index 9b36ffb..94076dc 100644 --- a/scanpydoc/definition_list_typed_field.py +++ b/scanpydoc/definition_list_typed_field.py @@ -5,12 +5,12 @@ (e.g. function parameters) as definition lists instead of simple paragraphs. """ -from typing import Dict, List, Tuple, Any +from typing import Any, Dict, List, Tuple from docutils import nodes from sphinx import addnodes from sphinx.application import Sphinx -from sphinx.domains.python import PyTypedField, PyObject +from sphinx.domains.python import PyObject, PyTypedField from sphinx.environment import BuildEnvironment from . import _setup_sig, metadata diff --git a/scanpydoc/elegant_typehints/__init__.py b/scanpydoc/elegant_typehints/__init__.py index d3a56c9..a5692e0 100644 --- a/scanpydoc/elegant_typehints/__init__.py +++ b/scanpydoc/elegant_typehints/__init__.py @@ -48,11 +48,10 @@ def x() -> Tuple[int, float]: from pathlib import Path from typing import Any, Dict - import sphinx_autodoc_typehints +from docutils.parsers.rst import roles from sphinx.application import Sphinx from sphinx.config import Config -from docutils.parsers.rst import roles from sphinx.ext.autodoc import ClassDocumenter from .. import _setup_sig, metadata @@ -96,7 +95,7 @@ def setup(app: Sphinx) -> Dict[str, Any]: app.add_css_file("typehints.css") app.connect("config-inited", _init_vars) - from .formatting import format_annotation, _role_annot + from .formatting import _role_annot, format_annotation sphinx_autodoc_typehints.format_annotation = format_annotation for name in ["annotation-terse", "annotation-full"]: diff --git a/scanpydoc/elegant_typehints/autodoc_patch.py b/scanpydoc/elegant_typehints/autodoc_patch.py index 0328719..b843e8f 100644 --- a/scanpydoc/elegant_typehints/autodoc_patch.py +++ b/scanpydoc/elegant_typehints/autodoc_patch.py @@ -1,5 +1,5 @@ from functools import wraps -from typing import Mapping, Callable, Tuple +from typing import Callable, Mapping, Tuple from docutils.statemachine import StringList from sphinx.ext.autodoc import ClassDocumenter diff --git a/scanpydoc/elegant_typehints/example.py b/scanpydoc/elegant_typehints/example.py index 6aa3991..4941595 100644 --- a/scanpydoc/elegant_typehints/example.py +++ b/scanpydoc/elegant_typehints/example.py @@ -1,4 +1,4 @@ -from typing import Dict, Union, Optional +from typing import Dict, Optional, Union def example_func(a: Optional[str], b: Union[str, int, None] = None) -> Dict[str, int]: diff --git a/scanpydoc/elegant_typehints/formatting.py b/scanpydoc/elegant_typehints/formatting.py index 060c647..3293b0e 100644 --- a/scanpydoc/elegant_typehints/formatting.py +++ b/scanpydoc/elegant_typehints/formatting.py @@ -1,9 +1,8 @@ -import inspect import collections.abc as cabc +import inspect from functools import partial -from typing import Any, Union # Meta -from typing import Type, Sequence, Iterable # ABC -from typing import Dict, List, Tuple # Concrete +from typing import Any, Dict, Iterable, List, Sequence, Tuple, Type, Union + try: # 3.8 additions from typing import Literal, get_args, get_origin @@ -11,12 +10,12 @@ from typing_extensions import Literal, get_args, get_origin import sphinx_autodoc_typehints -from sphinx_autodoc_typehints import format_annotation as _format_orig from docutils import nodes from docutils.nodes import Node from docutils.parsers.rst.roles import set_classes from docutils.parsers.rst.states import Inliner, Struct from docutils.utils import SystemMessage, unescape +from sphinx_autodoc_typehints import format_annotation as _format_orig from scanpydoc import elegant_typehints diff --git a/scanpydoc/elegant_typehints/return_tuple.py b/scanpydoc/elegant_typehints/return_tuple.py index c27ae0c..8fa838e 100644 --- a/scanpydoc/elegant_typehints/return_tuple.py +++ b/scanpydoc/elegant_typehints/return_tuple.py @@ -1,7 +1,8 @@ import inspect import re from logging import getLogger -from typing import get_type_hints, Any, Union, Optional, Type, Tuple, List +from typing import Any, List, Optional, Tuple, Type, Union, get_type_hints + try: # 3.8 additions from typing import get_args, get_origin diff --git a/scanpydoc/rtd_github_links.py b/scanpydoc/rtd_github_links.py index 36d4c49..f256353 100644 --- a/scanpydoc/rtd_github_links.py +++ b/scanpydoc/rtd_github_links.py @@ -39,7 +39,7 @@ import sys from pathlib import Path, PurePosixPath from types import ModuleType -from typing import Dict, Any, Tuple +from typing import Any, Dict, Tuple from jinja2.defaults import DEFAULT_FILTERS from sphinx.application import Sphinx diff --git a/tests/test_elegant_typehints.py b/tests/test_elegant_typehints.py index d97b4ab..37bad22 100644 --- a/tests/test_elegant_typehints.py +++ b/tests/test_elegant_typehints.py @@ -4,6 +4,7 @@ import typing as t from pathlib import Path + try: # 3.8 additions from typing import Literal, get_args, get_origin except ImportError: @@ -14,9 +15,9 @@ from sphinx.application import Sphinx from scanpydoc.elegant_typehints.formatting import ( - format_annotation, - _format_terse, _format_full, + _format_terse, + format_annotation, ) from scanpydoc.elegant_typehints.return_tuple import process_docstring diff --git a/tests/test_rtd_github_links.py b/tests/test_rtd_github_links.py index 27b44fe..f1e81f5 100644 --- a/tests/test_rtd_github_links.py +++ b/tests/test_rtd_github_links.py @@ -3,7 +3,8 @@ import pytest from _pytest.monkeypatch import MonkeyPatch -from scanpydoc.rtd_github_links import github_url, _get_linenos, _get_obj_module +from scanpydoc.rtd_github_links import _get_linenos, _get_obj_module, github_url + HERE = Path(__file__).parent From f29f00234418bad5d8cefd852a1b92bd984c62cc Mon Sep 17 00:00:00 2001 From: Philipp A Date: Mon, 6 Sep 2021 13:06:40 +0200 Subject: [PATCH 4/5] Add extra for typehints --- .github/workflows/ci.yml | 2 +- pyproject.toml | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5387404..78fda33 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -30,6 +30,6 @@ jobs: - name: Install dependencies run: | pip install --upgrade pip wheel - pip install .[test] + pip install .[test,typehints] - name: Tests run: pytest --color=yes diff --git a/pyproject.toml b/pyproject.toml index 0796a19..42fad68 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -36,6 +36,8 @@ doc = [ 'sphinx-autodoc-typehints>=1.12', 'sphinx-rtd-theme', ] +typehints = ['sphinx-autodoc-typehints>=1.12'] +theme = ['sphinx-rtd-theme'] [tool.flit.entrypoints.'sphinx.html_themes'] scanpydoc = 'scanpydoc.theme' From 36c0bbd3bb773317aa9055f81b66a8c746d63c8c Mon Sep 17 00:00:00 2001 From: Philipp A Date: Mon, 6 Sep 2021 13:08:43 +0200 Subject: [PATCH 5/5] slimmer --- .github/workflows/ci.yml | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 78fda33..5400a73 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -16,20 +16,18 @@ jobs: python-version: ['3.7', '3.8', '3.9'] steps: - uses: actions/checkout@v2 - - name: Set up Python - uses: actions/setup-python@v2 + - uses: actions/setup-python@v2 with: python-version: ${{ matrix.python-version }} - - name: Restore pip and pre-commit caches - uses: actions/cache@v2 + - uses: actions/cache@v2 with: path: | ~/.cache/pip ~/.cache/pre-commit key: ${{ runner.os }}-pip-${{ hashFiles('pyproject.toml') }} - - name: Install dependencies + - name: dependencies run: | pip install --upgrade pip wheel pip install .[test,typehints] - - name: Tests + - name: tests run: pytest --color=yes