Skip to content

Commit 5d806ac

Browse files
authored
Add py.typed and enable mypy in pre-commit (#102)
Add `py.typed` to the distribution, enabling users to use `LazyDataDir` in type annotations. In addition enable type checking via `mypy` in the `pre-commit` configuration.
1 parent b2bb4ac commit 5d806ac

File tree

8 files changed

+57
-17
lines changed

8 files changed

+57
-17
lines changed

.pre-commit-config.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,8 @@ repos:
4848
- flake8-typing-imports==1.14.0
4949
- flake8-builtins==2.1.0
5050
- flake8-bugbear==23.1.20
51+
- repo: https://github.com/pre-commit/mirrors-mypy
52+
rev: v1.16.0
53+
hooks:
54+
- id: mypy
55+
files: ^(src/|tests/)

CHANGELOG.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
pytest-datadir
22
==============
33

4+
UNRELEASED
5+
----------
6+
7+
*UNRELEASED
8+
9+
- ``py.typed`` was added to the distribution, enabling users to use ``LazyDataDir`` in type annotations.
10+
411
1.7.1
512
-----
613

pyproject.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,21 @@ where = ["src"]
5757

5858
[tool.pytest.ini_options]
5959
testpaths = ["tests"]
60+
61+
[tool.setuptools.package-data]
62+
"pytest_datadir" = ["py.typed"]
63+
64+
[tool.mypy]
65+
disallow_any_generics = true
66+
disallow_subclassing_any = true
67+
disallow_untyped_defs = true
68+
ignore_missing_imports = true
69+
no_implicit_optional = true
70+
pretty = true
71+
show_error_codes = true
72+
strict_equality = true
73+
warn_redundant_casts = true
74+
warn_return_any = true
75+
warn_unreachable = true
76+
warn_unused_configs = true
77+
warn_unused_ignores = true

src/pytest_datadir/plugin.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import pytest
88

99

10-
def _win32_longpath(path):
10+
def _win32_longpath(path: str) -> str:
1111
"""
1212
Helper function to add the long path prefix for Windows, so that shutil.copytree
1313
won't fail while working with paths with 255+ chars.
@@ -33,7 +33,7 @@ def _win32_longpath(path):
3333

3434

3535
@pytest.fixture
36-
def shared_datadir(request, tmp_path):
36+
def shared_datadir(request: pytest.FixtureRequest, tmp_path: Path) -> Path:
3737
original_shared_path = os.path.join(request.fspath.dirname, "data")
3838
temp_path = tmp_path / "data"
3939
shutil.copytree(
@@ -43,12 +43,12 @@ def shared_datadir(request, tmp_path):
4343

4444

4545
@pytest.fixture(scope="module")
46-
def original_datadir(request):
47-
return request.path.parent / request.path.stem
46+
def original_datadir(request: pytest.FixtureRequest) -> Path:
47+
return Path(request.path).with_suffix("")
4848

4949

5050
@pytest.fixture
51-
def datadir(original_datadir, tmp_path):
51+
def datadir(original_datadir: Path, tmp_path: Path) -> Path:
5252
result = tmp_path / original_datadir.stem
5353
if original_datadir.is_dir():
5454
shutil.copytree(

src/pytest_datadir/py.typed

Whitespace-only changes.

tests/test_hello.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
from pathlib import Path
33

44
import pytest
5+
from pytest_datadir.plugin import LazyDataDir
56

67

78
@pytest.fixture(autouse=True, scope="module")
8-
def create_long_file_path():
9+
def create_long_file_path() -> None:
910
"""
1011
Create a very long file name to ensure datadir can copy it correctly.
1112
@@ -21,7 +22,7 @@ def create_long_file_path():
2122
os.chdir(old_cwd)
2223

2324

24-
def test_read_hello(datadir):
25+
def test_read_hello(datadir: Path) -> None:
2526
assert set(os.listdir(str(datadir))) == {
2627
"local_directory",
2728
"hello.txt",
@@ -33,7 +34,12 @@ def test_read_hello(datadir):
3334
assert contents == "Hello, world!\n"
3435

3536

36-
def test_change_test_files(datadir, original_datadir, shared_datadir, request):
37+
def test_change_test_files(
38+
datadir: Path,
39+
original_datadir: Path,
40+
shared_datadir: Path,
41+
request: pytest.FixtureRequest,
42+
) -> None:
3743
filename = datadir / "hello.txt"
3844
with filename.open("w") as fp:
3945
fp.write("Modified text!\n")
@@ -53,14 +59,14 @@ def test_change_test_files(datadir, original_datadir, shared_datadir, request):
5359
assert fp.read().strip() == "8000"
5460

5561

56-
def test_read_spam_from_other_dir(shared_datadir):
62+
def test_read_spam_from_other_dir(shared_datadir: Path) -> None:
5763
filename = shared_datadir / "spam.txt"
5864
with filename.open() as fp:
5965
contents = fp.read()
6066
assert contents == "eggs\n"
6167

6268

63-
def test_file_override(shared_datadir, datadir):
69+
def test_file_override(shared_datadir: Path, datadir: Path) -> None:
6470
"""The same file is in the module dir and global data.
6571
Shared files are kept in a different temp directory"""
6672
shared_filepath = shared_datadir / "over.txt"
@@ -70,7 +76,7 @@ def test_file_override(shared_datadir, datadir):
7076
assert shared_filepath != private_filepath
7177

7278

73-
def test_local_directory(datadir):
79+
def test_local_directory(datadir: Path) -> None:
7480
directory = datadir / "local_directory"
7581
assert directory.is_dir()
7682
filename = directory / "file.txt"
@@ -80,7 +86,7 @@ def test_local_directory(datadir):
8086
assert contents.strip() == "local contents"
8187

8288

83-
def test_shared_directory(shared_datadir):
89+
def test_shared_directory(shared_datadir: Path) -> None:
8490
assert shared_datadir.is_dir()
8591
filename = shared_datadir / "shared_directory" / "file.txt"
8692
assert filename.is_file()
@@ -89,7 +95,7 @@ def test_shared_directory(shared_datadir):
8995
assert contents.strip() == "global contents"
9096

9197

92-
def test_lazy_copy(lazy_datadir):
98+
def test_lazy_copy(lazy_datadir: LazyDataDir) -> None:
9399
# The temporary directory starts empty.
94100
assert {x.name for x in lazy_datadir.tmp_path.iterdir()} == set()
95101

@@ -123,7 +129,7 @@ def test_lazy_copy(lazy_datadir):
123129
}
124130

125131

126-
def test_lazy_copy_sub_directory(lazy_datadir):
132+
def test_lazy_copy_sub_directory(lazy_datadir: LazyDataDir) -> None:
127133
"""Copy via file by using a sub-directory (#99)."""
128134
# The temporary directory starts empty.
129135
assert {x.name for x in lazy_datadir.tmp_path.iterdir()} == set()

tests/test_nonexistent.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1-
def test_missing_data_dir_starts_empty(datadir):
1+
from pathlib import Path
2+
3+
4+
def test_missing_data_dir_starts_empty(datadir: Path) -> None:
25
assert list(datadir.iterdir()) == []

tests/test_pathlib.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import sys
2+
from pathlib import Path
23

34
import pytest
45
from pytest_datadir.plugin import _win32_longpath
56

67

7-
def test_win32_longpath_idempotent(datadir):
8+
def test_win32_longpath_idempotent(datadir: Path) -> None:
89
"""Double application should not prepend twice."""
910
first = _win32_longpath(str(datadir))
1011
second = _win32_longpath(first)
@@ -14,7 +15,7 @@ def test_win32_longpath_idempotent(datadir):
1415
@pytest.mark.skipif(
1516
not sys.platform.startswith("win"), reason="Only makes sense on Windows"
1617
)
17-
def test_win32_longpath_unc(datadir):
18+
def test_win32_longpath_unc(datadir: Path) -> None:
1819
unc_path = r"\\ComputerName\SharedFolder\Resource"
1920
longpath = _win32_longpath(unc_path)
2021
assert longpath.startswith("\\\\?\\UNC\\")

0 commit comments

Comments
 (0)