Skip to content

Commit

Permalink
deal with the fall-out of enabling Mypy type checking on tests. 馃ゴ
Browse files Browse the repository at this point in the history
  • Loading branch information
NiklasRosenstein committed May 28, 2023
1 parent ad22a57 commit 03a56c8
Show file tree
Hide file tree
Showing 7 changed files with 825 additions and 727 deletions.
10 changes: 5 additions & 5 deletions docspec-python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ typed = true
[tool.slap.test]
check = "slap check"
pytest = "pytest -vv"
mypy = "mypy src/ --check-untyped-defs"
black = "black --check src/"
isort = "isort --check-only src/"
flake8 = "flake8 src/"
mypy = "mypy src/ test --check-untyped-defs"
black = "black --check src/ test/"
isort = "isort --check-only src/ test/"
flake8 = "flake8 src/ test/"

[tool.slap.run]
fmt = "black src/ && isort src/"
fmt = "black src/ test/ && isort src/ test/"

[tool.mypy]
python_version = "3.6"
Expand Down
72 changes: 36 additions & 36 deletions docspec-python/test/test_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,69 +24,69 @@
"""

import os
from pathlib import Path
from unittest import mock
import pytest
import site
import typing as t
from pathlib import Path

import docspec
import pytest

import docspec_python


def _assert_is_docspec_python_module(modules: t.List[docspec.Module]) -> None:
assert sorted(m.name for m in modules) == ['docspec_python', 'docspec_python.__main__', 'docspec_python.parser']
assert sorted(m.name for m in modules) == ["docspec_python", "docspec_python.__main__", "docspec_python.parser"]


def test_discovery_from_sys_path() -> None:
""" Tests that the `docspec_python` module can be loaded from `sys.path`. """
"""Tests that the `docspec_python` module can be loaded from `sys.path`."""

modules = list(docspec_python.load_python_modules(packages=['docspec_python']))
_assert_is_docspec_python_module(modules)
modules = list(docspec_python.load_python_modules(packages=["docspec_python"]))
_assert_is_docspec_python_module(modules)


def test_discovery_search_path_overrides():
""" Tests that the `docspec_python` module will not be loaded if an empty search path is supplied. """
def test_discovery_search_path_overrides() -> None:
"""Tests that the `docspec_python` module will not be loaded if an empty search path is supplied."""

modules = list(docspec_python.load_python_modules(
packages=['docspec_python'], search_path=[], raise_=False))
assert not modules
modules = list(docspec_python.load_python_modules(packages=["docspec_python"], search_path=[], raise_=False))
assert not modules


@pytest.mark.skipif(
os.getenv('DOCSPEC_TEST_NO_DEVELOP') != 'true',
reason='DOCSPEC_TEST_NO_DEVELOP needs to be set to "true" to test this case')
os.getenv("DOCSPEC_TEST_NO_DEVELOP") != "true",
reason='DOCSPEC_TEST_NO_DEVELOP needs to be set to "true" to test this case',
)
def test_discovery_search_path_overrides_docspec_python_in_install_mode() -> None:
""" Tests that the `docspec_python` module can be loaded separately from the local project source code as well
as from the system site-packages independently by supplying the right search path. """
"""Tests that the `docspec_python` module can be loaded separately from the local project source code as well
as from the system site-packages independently by supplying the right search path."""

src_dir = os.path.normpath(__file__ + '/../../src')
src_modules = list(docspec_python.load_python_modules(
packages=['docspec_python'], search_path=[src_dir]))
_assert_is_docspec_python_module(src_modules)
src_dir = os.path.normpath(__file__ + "/../../src")
src_modules = list(docspec_python.load_python_modules(packages=["docspec_python"], search_path=[src_dir]))
_assert_is_docspec_python_module(src_modules)

site_modules = list(docspec_python.load_python_modules(
packages=['docspec_python'], search_path=site.getsitepackages()))
_assert_is_docspec_python_module(site_modules)
site_modules = list(
docspec_python.load_python_modules(packages=["docspec_python"], search_path=site.getsitepackages())
)
_assert_is_docspec_python_module(site_modules)

assert site_modules[0].location.filename != src_modules[0].location.filename # type: ignore
assert site_modules[0].location.filename != src_modules[0].location.filename


def test_pep420_namespace_package() -> None:
""" Tests that PEP 420 namespace packages can be loaded. """
"""Tests that PEP 420 namespace packages can be loaded."""

src_dir = Path(__file__).parent / 'src'
src_dir = Path(__file__).parent / "src"

# Test that the module can be loaded explicitly.
src_modules = list(docspec_python.load_python_modules(
modules=['pep420_namespace_package.module'], search_path=[src_dir]))
# Test that the module can be loaded explicitly.
src_modules = list(
docspec_python.load_python_modules(modules=["pep420_namespace_package.module"], search_path=[src_dir])
)

assert len(src_modules) == 1
assert src_modules[0].name == 'pep420_namespace_package.module'
assert len(src_modules) == 1
assert src_modules[0].name == "pep420_namespace_package.module"

# Test that the module can be loaded implicitly.
src_modules = list(docspec_python.load_python_modules(
packages=['pep420_namespace_package'], search_path=[src_dir]))
# Test that the module can be loaded implicitly.
src_modules = list(docspec_python.load_python_modules(packages=["pep420_namespace_package"], search_path=[src_dir]))

assert len(src_modules) == 1
assert src_modules[0].name == 'pep420_namespace_package.module'
assert len(src_modules) == 1
assert src_modules[0].name == "pep420_namespace_package.module"

0 comments on commit 03a56c8

Please sign in to comment.