Skip to content

Commit a8906d0

Browse files
committed
make writing tests easier for contributors
1 parent 3a09258 commit a8906d0

22 files changed

+196
-261
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,5 +136,5 @@ dmypy.json
136136
/poetry.lock
137137
.idea/**/*
138138

139-
# duplication of stub specific objects for testing
140-
tests/_typing.py
139+
# duplication of stub specific objects upon testing
140+
tests/_typing.pyi

tests/_typing.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Committed content should be import-only. At runtime of tests, this file will
2+
# be replaced by pandas-stubs/_typing.pyi.
3+
4+
from pandas._typing import (
5+
BooleanDtypeArg,
6+
BytesDtypeArg,
7+
CategoryDtypeArg,
8+
ComplexDtypeArg,
9+
Dtype,
10+
FloatDtypeArg,
11+
IntDtypeArg,
12+
ObjectDtypeArg,
13+
PandasAstypeComplexDtypeArg,
14+
PandasAstypeFloatDtypeArg,
15+
PandasAstypeTimedeltaDtypeArg,
16+
PandasAstypeTimestampDtypeArg,
17+
PandasFloatDtypeArg,
18+
StrDtypeArg,
19+
TimedeltaDtypeArg,
20+
TimestampDtypeArg,
21+
UIntDtypeArg,
22+
VoidDtypeArg,
23+
np_1darray,
24+
np_1darray_anyint,
25+
np_1darray_bool,
26+
np_1darray_bytes,
27+
np_1darray_complex,
28+
np_1darray_dt,
29+
np_1darray_float,
30+
np_1darray_int64,
31+
np_1darray_intp,
32+
np_1darray_object,
33+
np_1darray_td,
34+
np_2darray,
35+
np_ndarray,
36+
np_ndarray_bool,
37+
np_ndarray_dt,
38+
np_ndarray_num,
39+
np_ndarray_str,
40+
np_ndarray_td,
41+
)
42+
43+
__all__ = [
44+
"np_1darray",
45+
"np_2darray",
46+
"np_ndarray",
47+
"np_1darray_bool",
48+
"BooleanDtypeArg",
49+
"BytesDtypeArg",
50+
"CategoryDtypeArg",
51+
"ComplexDtypeArg",
52+
"IntDtypeArg",
53+
"np_ndarray_str",
54+
"ObjectDtypeArg",
55+
"PandasAstypeComplexDtypeArg",
56+
"PandasAstypeTimedeltaDtypeArg",
57+
"PandasAstypeTimestampDtypeArg",
58+
"StrDtypeArg",
59+
"TimedeltaDtypeArg",
60+
"TimestampDtypeArg",
61+
"UIntDtypeArg",
62+
"PandasFloatDtypeArg",
63+
"VoidDtypeArg",
64+
"np_1darray",
65+
"np_1darray_anyint",
66+
"np_1darray_bool",
67+
"np_1darray_bytes",
68+
"np_1darray_complex",
69+
"np_1darray_dt",
70+
"np_1darray_float",
71+
"np_ndarray_bool",
72+
"np_ndarray_dt",
73+
"np_1darray_object",
74+
"np_1darray_td",
75+
"np_1darray_int64",
76+
"np_ndarray_num",
77+
"FloatDtypeArg",
78+
"PandasAstypeFloatDtypeArg",
79+
"np_ndarray_bool",
80+
"np_ndarray_dt",
81+
"np_ndarray_td",
82+
"np_1darray_intp",
83+
"Dtype",
84+
]

tests/arrays/test_extension_array.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# Test common ExtensionArray methods
2-
from typing import TYPE_CHECKING
32

43
import numpy as np
54
import pandas as pd
@@ -14,17 +13,10 @@
1413
from pandas._typing import ArrayLike
1514

1615
from tests import check
17-
18-
if TYPE_CHECKING:
19-
from pandas._typing import (
20-
np_1darray,
21-
np_1darray_intp,
22-
)
23-
else:
24-
from tests._typing import (
25-
np_1darray,
26-
np_1darray_intp,
27-
)
16+
from tests._typing import (
17+
np_1darray,
18+
np_1darray_intp,
19+
)
2820

2921

3022
def test_ea_common() -> None:

tests/arrays/test_floating_array.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111
check,
1212
exception_on_platform,
1313
)
14-
15-
if TYPE_CHECKING:
16-
from pandas._typing import PandasFloatDtypeArg
14+
from tests._typing import PandasFloatDtypeArg
1715

1816

1917
def test_constructor() -> None:
@@ -29,7 +27,7 @@ def test_constructor() -> None:
2927

3028

3129
@pytest.mark.parametrize(("dtype", "target_dtype"), PANDAS_FLOAT_ARGS.items(), ids=repr)
32-
def test_constructor_dtype(dtype: "PandasFloatDtypeArg", target_dtype: type) -> None:
30+
def test_constructor_dtype(dtype: PandasFloatDtypeArg, target_dtype: type) -> None:
3331
exc = exception_on_platform(dtype)
3432
if exc:
3533
with pytest.raises(exc, match=rf"data type {dtype!r} not understood"):

tests/conftest.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,18 @@
44

55
import pytest
66

7+
# Use pandas._testing from the stubs for testing
8+
groundtruth = Path(__file__).parents[1] / "pandas-stubs" / "_typing.pyi"
79
target = Path(__file__).parent / "_typing.py"
8-
copyfile(Path(__file__).parents[1] / "pandas-stubs" / "_typing.pyi", target)
10+
staging = Path(__file__).parent / "_typing.pyi"
11+
copyfile(target, staging)
12+
copyfile(groundtruth, target)
913

1014

1115
@pytest.fixture(autouse=True, scope="session")
1216
def setup_typing_module() -> Generator[None, None, None]:
13-
"""Ensure that tests._typing is removed after running tests"""
17+
"""Ensure that tests._typing is recovered after running tests"""
1418
yield
15-
target.unlink()
19+
20+
copyfile(staging, target)
21+
staging.unlink()

tests/extension/decimal/array.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import numbers
1212
import sys
1313
from typing import (
14-
TYPE_CHECKING,
1514
Any,
1615
cast,
1716
overload,
@@ -53,18 +52,11 @@
5352
pandas_dtype,
5453
)
5554

56-
if TYPE_CHECKING:
57-
from pandas._typing import (
58-
np_1darray,
59-
np_1darray_bool,
60-
np_ndarray,
61-
)
62-
else:
63-
from tests._typing import (
64-
np_1darray,
65-
np_1darray_bool,
66-
np_ndarray,
67-
)
55+
from tests._typing import (
56+
np_1darray,
57+
np_1darray_bool,
58+
np_ndarray,
59+
)
6860

6961

7062
@register_extension_dtype

tests/indexes/test_categoricalindex.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
from __future__ import annotations
22

3-
from typing import TYPE_CHECKING
4-
53
import pandas as pd
64
from typing_extensions import (
75
assert_type,
86
)
97

108
from tests import check
11-
12-
if TYPE_CHECKING:
13-
from pandas._typing import np_1darray_intp
14-
else:
15-
from tests._typing import np_1darray_intp
9+
from tests._typing import np_1darray_intp
1610

1711

1812
def test_categoricalindex_unique() -> None:

tests/indexes/test_datetime_index.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,16 @@
11
from __future__ import annotations
22

33
from datetime import time
4-
from typing import TYPE_CHECKING
54

65
import numpy as np
76
import pandas as pd
87
from typing_extensions import assert_type
98

109
from tests import check
11-
12-
if TYPE_CHECKING:
13-
from pandas._typing import (
14-
np_1darray_bool,
15-
np_1darray_intp,
16-
)
17-
else:
18-
from tests._typing import (
19-
np_1darray_bool,
20-
np_1darray_intp,
21-
)
10+
from tests._typing import (
11+
np_1darray_bool,
12+
np_1darray_intp,
13+
)
2214

2315

2416
def test_index_relops() -> None:

tests/indexes/test_index_float.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@
1111
check,
1212
exception_on_platform,
1313
)
14+
from tests._typing import PandasAstypeFloatDtypeArg
1415

1516
if TYPE_CHECKING:
1617
from pandas.core.indexes.base import FloatNotNumpy16DtypeArg
1718

18-
from pandas._typing import PandasAstypeFloatDtypeArg
19-
2019

2120
def test_constructor() -> None:
2221
check(assert_type(pd.Index([1.0]), "pd.Index[float]"), pd.Index, np.floating)

tests/indexes/test_indexes.py

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from collections.abc import Hashable
44
import datetime as dt
55
from typing import (
6-
TYPE_CHECKING,
76
Any,
87
cast,
98
)
@@ -23,31 +22,21 @@
2322
assert_type,
2423
)
2524

25+
from pandas._typing import Dtype # noqa: F401
26+
2627
from tests import (
2728
PD_LTE_23,
2829
TYPE_CHECKING_INVALID_USAGE,
2930
check,
3031
pytest_warns_bounded,
3132
)
32-
33-
if TYPE_CHECKING:
34-
from pandas._typing import (
35-
Dtype,
36-
np_1darray,
37-
np_1darray_bool,
38-
np_1darray_int64,
39-
np_1darray_intp,
40-
np_ndarray_dt,
41-
)
42-
else:
43-
from tests._typing import (
44-
np_1darray,
45-
np_1darray_bool,
46-
np_1darray_int64,
47-
np_1darray_intp,
48-
np_ndarray_dt,
49-
)
50-
from tests._typing import Dtype # noqa: F401
33+
from tests._typing import (
34+
np_1darray,
35+
np_1darray_bool,
36+
np_1darray_int64,
37+
np_1darray_intp,
38+
np_ndarray_dt,
39+
)
5140

5241

5342
def test_index_unique() -> None:

0 commit comments

Comments
 (0)