Skip to content

Commit

Permalink
“improve” typing
Browse files Browse the repository at this point in the history
  • Loading branch information
flying-sheep committed Oct 22, 2024
1 parent 84e2afb commit 25fee95
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 20 deletions.
7 changes: 4 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,18 @@
from collections.abc import Callable, Generator

from sphinx.application import Sphinx
from sphinx.testing.util import SphinxTestApp


@pytest.fixture
def make_app_setup(
make_app: Callable[..., Sphinx], tmp_path: Path
) -> Callable[..., Sphinx]:
make_app: type[SphinxTestApp], tmp_path: Path
) -> type[SphinxTestApp]:
def make_app_setup(builder: str = "html", /, **conf: Any) -> Sphinx: # noqa: ANN401
(tmp_path / "conf.py").write_text("")
return make_app(buildername=builder, srcdir=tmp_path, confoverrides=conf)

return make_app_setup
return make_app_setup # type: ignore[return-value]


@pytest.fixture
Expand Down
5 changes: 2 additions & 3 deletions tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,16 @@


if TYPE_CHECKING:
from collections.abc import Callable

import pytest
from sphinx.application import Sphinx
from sphinx.testing.util import SphinxTestApp


DEPRECATED = frozenset({"scanpydoc.autosummary_generate_imported"})


def test_all_get_installed(
monkeypatch: pytest.MonkeyPatch, make_app_setup: Callable[..., Sphinx]
monkeypatch: pytest.MonkeyPatch, make_app_setup: type[SphinxTestApp]
) -> None:
setups_seen: set[str] = set()
setups_called: dict[str, Sphinx] = {}
Expand Down
9 changes: 4 additions & 5 deletions tests/test_definition_list_typed_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@


if TYPE_CHECKING:
from collections.abc import Callable

from sphinx.application import Sphinx
from sphinx.testing.util import SphinxTestApp


@pytest.fixture
def app(make_app_setup: Callable[..., Sphinx]) -> Sphinx:
def app(make_app_setup: type[SphinxTestApp]) -> Sphinx:
app = make_app_setup()
app.setup_extension("scanpydoc.definition_list_typed_field")
return app
Expand All @@ -41,7 +40,7 @@ def app(make_app_setup: Callable[..., Sphinx]) -> Sphinx:
"""


def test_apps_separate(app: Sphinx, make_app_setup: Callable[..., Sphinx]) -> None:
def test_apps_separate(app: Sphinx, make_app_setup: type[SphinxTestApp]) -> None:
app_no_setup = make_app_setup()
assert app is not app_no_setup
assert "scanpydoc.definition_list_typed_field" in app.extensions
Expand Down Expand Up @@ -101,7 +100,7 @@ def test_convert_params(
)


def test_load_error(make_app_setup: Callable[..., Sphinx]) -> None:
def test_load_error(make_app_setup: type[SphinxTestApp]) -> None:
with pytest.raises(RuntimeError, match=r"Please load sphinx\.ext\.napoleon before"):
make_app_setup(
extensions=["scanpydoc.definition_list_typed_field", "sphinx.ext.napoleon"]
Expand Down
7 changes: 4 additions & 3 deletions tests/test_elegant_typehints.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from typing import Protocol

from sphinx.application import Sphinx
from sphinx.testing.util import SphinxTestApp

class ProcessDoc(Protocol): # noqa: D101
def __call__( # noqa: D102
Expand Down Expand Up @@ -60,7 +61,7 @@ class Gen(Generic[T]): pass


@pytest.fixture
def app(make_app_setup: Callable[..., Sphinx]) -> Sphinx:
def app(make_app_setup: type[SphinxTestApp]) -> Sphinx:
return make_app_setup(
master_doc="index",
extensions=[
Expand Down Expand Up @@ -472,14 +473,14 @@ def fn() -> tuple[int, str]: # pragma: no cover
assert res[2].startswith(":rtype: :sphinx_autodoc_typehints_type:")


def test_load_without_sat(make_app_setup: Callable[..., Sphinx]) -> None:
def test_load_without_sat(make_app_setup: type[SphinxTestApp]) -> None:
make_app_setup(
master_doc="index",
extensions=["sphinx.ext.autodoc", "scanpydoc.elegant_typehints"],
)


def test_load_error(make_app_setup: Callable[..., Sphinx]) -> None:
def test_load_error(make_app_setup: type[SphinxTestApp]) -> None:
with pytest.raises(
RuntimeError,
match=r"`scanpydoc.elegant_typehints` requires `sphinx.ext.autodoc`",
Expand Down
7 changes: 3 additions & 4 deletions tests/test_release_notes.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
if TYPE_CHECKING:
from typing import TypeAlias
from pathlib import Path
from collections.abc import Mapping, Callable
from collections.abc import Mapping

from sphinx.application import Sphinx
from sphinx.testing.util import SphinxTestApp

Tree: TypeAlias = Mapping[str | Path, "Tree | str"]

Expand All @@ -33,9 +34,7 @@ def mkfiles(root: Path, tree: Tree = MappingProxyType({})) -> None:


@pytest.fixture(params=["rst", "myst"])
def app(
request: pytest.FixtureRequest, make_app_setup: Callable[..., Sphinx]
) -> Sphinx:
def app(request: pytest.FixtureRequest, make_app_setup: type[SphinxTestApp]) -> Sphinx:
return make_app_setup(
"pseudoxml",
extensions=[
Expand Down
4 changes: 2 additions & 2 deletions tests/test_rtd_github_links.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
from typing import Literal
from collections.abc import Callable

from sphinx.application import Sphinx
from _pytest.monkeypatch import MonkeyPatch
from sphinx.testing.util import SphinxTestApp

from scanpydoc.rtd_github_links._linkcode import Domain, DomainInfo

Expand Down Expand Up @@ -111,7 +111,7 @@ def test_infer_vars_error(config: Config, setup: list[str], pattern: str) -> Non
_infer_vars(config)


def test_app(monkeypatch: MonkeyPatch, make_app_setup: Callable[..., Sphinx]) -> None:
def test_app(monkeypatch: MonkeyPatch, make_app_setup: type[SphinxTestApp]) -> None:
filters: dict[str, Callable[..., object]] = {}
monkeypatch.setattr("scanpydoc.rtd_github_links._init_vars", lambda *_: None)
monkeypatch.setattr("scanpydoc.rtd_github_links.DEFAULT_FILTERS", filters)
Expand Down

0 comments on commit 25fee95

Please sign in to comment.