Skip to content

Commit fee1de5

Browse files
committed
Index[Any]
1 parent 1ebfe2b commit fee1de5

File tree

6 files changed

+159
-21
lines changed

6 files changed

+159
-21
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ ci:
33
autofix_prs: false
44
repos:
55
- repo: https://github.com/astral-sh/ruff-pre-commit
6-
rev: v0.13.3
6+
rev: v0.14.0
77
hooks:
88
- id: ruff-check
99
args: [--exit-non-zero-on-fix]

tests/indexes/arithmetic/bool/test_truediv.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@
1616
@pytest.fixture
1717
def left() -> "pd.Index[bool]":
1818
"""Left operand"""
19-
return check(
20-
assert_type(pd.Index([True, False, True]), "pd.Index[bool]"), pd.Index, np.bool_
21-
)
19+
lo = pd.Index([True, False, True])
20+
return check(assert_type(lo, "pd.Index[bool]"), pd.Index, np.bool_)
2221

2322

2423
def test_truediv_py_scalar(left: "pd.Index[bool]") -> None:
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
from pathlib import Path
2+
from typing import Any
3+
4+
import numpy as np
5+
import pandas as pd
6+
import pytest
7+
from typing_extensions import assert_type
8+
9+
from tests import (
10+
PD_LTE_23,
11+
TYPE_CHECKING_INVALID_USAGE,
12+
check,
13+
)
14+
15+
16+
@pytest.fixture
17+
def left_i() -> pd.Index:
18+
"""Left operand"""
19+
lo = pd.MultiIndex.from_arrays([[1, 2, 3]]).levels[0]
20+
return check(assert_type(lo, pd.Index), pd.Index, np.integer)
21+
22+
23+
def test_truediv_py_scalar(left_i: pd.Index) -> None:
24+
"""Test pd.Index[Any] (int) / Python native scalars"""
25+
b, i, f, c = True, 1, 1.0, 1j
26+
27+
check(assert_type(left_i / b, pd.Index), pd.Index)
28+
check(assert_type(left_i / i, pd.Index), pd.Index)
29+
check(assert_type(left_i / f, pd.Index), pd.Index)
30+
check(assert_type(left_i / c, pd.Index), pd.Index)
31+
32+
check(assert_type(b / left_i, pd.Index), pd.Index)
33+
check(assert_type(i / left_i, pd.Index), pd.Index)
34+
check(assert_type(f / left_i, pd.Index), pd.Index)
35+
check(assert_type(c / left_i, pd.Index), pd.Index)
36+
37+
38+
def test_truediv_py_sequence(left_i: pd.Index) -> None:
39+
"""Test pd.Index[Any] (int) / Python native sequences"""
40+
b, i, f, c = [True, False, True], [2, 3, 5], [1.0, 2.0, 3.0], [1j, 1j, 4j]
41+
42+
check(assert_type(left_i / b, pd.Index), pd.Index)
43+
check(assert_type(left_i / i, pd.Index), pd.Index)
44+
check(assert_type(left_i / f, pd.Index), pd.Index)
45+
check(assert_type(left_i / c, pd.Index), pd.Index)
46+
47+
check(assert_type(b / left_i, pd.Index), pd.Index)
48+
check(assert_type(i / left_i, pd.Index), pd.Index)
49+
check(assert_type(f / left_i, pd.Index), pd.Index)
50+
check(assert_type(c / left_i, pd.Index), pd.Index)
51+
52+
53+
def test_truediv_numpy_array(left_i: pd.Index) -> None:
54+
"""Test pd.Index[Any] (int) / numpy arrays"""
55+
b = np.array([True, False, True], np.bool_)
56+
i = np.array([2, 3, 5], np.int64)
57+
f = np.array([1.0, 2.0, 3.0], np.float64)
58+
c = np.array([1.1j, 2.2j, 4.1j], np.complex64)
59+
60+
check(assert_type(left_i / b, pd.Index), pd.Index)
61+
check(assert_type(left_i / i, pd.Index), pd.Index)
62+
check(assert_type(left_i / f, pd.Index), pd.Index)
63+
check(assert_type(left_i / c, pd.Index), pd.Index)
64+
65+
# `numpy` typing gives the corresponding `ndarray`s in the static type
66+
# checking, where our `__rtruediv__` cannot override. At runtime, they return
67+
# `Index`.
68+
# microsoft/pyright#10924
69+
check(
70+
assert_type(b / left_i, Any), # pyright: ignore[reportAssertTypeFailure]
71+
pd.Index,
72+
)
73+
check(
74+
assert_type(i / left_i, Any), # pyright: ignore[reportAssertTypeFailure]
75+
pd.Index,
76+
)
77+
check(
78+
assert_type(f / left_i, Any), # pyright: ignore[reportAssertTypeFailure]
79+
pd.Index,
80+
)
81+
check(
82+
assert_type(c / left_i, Any), # pyright: ignore[reportAssertTypeFailure]
83+
pd.Index,
84+
)
85+
86+
87+
def test_truediv_pd_index(left_i: pd.Index) -> None:
88+
"""Test pd.Index[Any] (int) / pandas Indexes"""
89+
a = pd.MultiIndex.from_tuples([(1,), (2,), (3,)]).levels[0]
90+
b = pd.Index([True, False, True])
91+
i = pd.Index([2, 3, 5])
92+
f = pd.Index([1.0, 2.0, 3.0])
93+
c = pd.Index([1.1j, 2.2j, 4.1j])
94+
95+
check(assert_type(left_i / a, pd.Index), pd.Index)
96+
check(assert_type(left_i / b, pd.Index), pd.Index)
97+
check(assert_type(left_i / i, pd.Index), pd.Index)
98+
check(assert_type(left_i / f, pd.Index), pd.Index)
99+
check(assert_type(left_i / c, pd.Index), pd.Index)
100+
101+
check(assert_type(a / left_i, pd.Index), pd.Index)
102+
check(assert_type(b / left_i, pd.Index), pd.Index)
103+
check(assert_type(i / left_i, pd.Index), pd.Index)
104+
check(assert_type(f / left_i, pd.Index), pd.Index)
105+
check(assert_type(c / left_i, pd.Index), pd.Index)
106+
107+
108+
def test_truediv_paths(tmp_path: Path) -> None:
109+
"""Test pd.Index of paths / path object.
110+
111+
Also GH 682."""
112+
fpath = Path("a.png")
113+
folders, fpaths = pd.Index([tmp_path, tmp_path]), pd.Index([fpath, fpath])
114+
115+
check(assert_type(folders / fpath, pd.Index), pd.Index, Path)
116+
117+
# mypy thinks it's `Path`, in contrast to Index.__rtruediv__(self, other: Path) -> Index: ...
118+
check(assert_type(tmp_path / fpaths, pd.Index), pd.Index, Path) # type: ignore[assert-type]
119+
120+
121+
def test_truediv_path(tmp_path: Path) -> None:
122+
"""Test pd.Index / path object.
123+
124+
Also GH 682."""
125+
fnames = pd.Index(["a.png", "b.gz", "c.txt"])
126+
127+
if PD_LTE_23:
128+
# Bug in 3.0 https://github.com/pandas-dev/pandas/issues/61940 (pyarrow.lib.ArrowInvalid)
129+
check(assert_type(fnames / tmp_path, pd.Index), pd.Index, Path)
130+
check(assert_type(tmp_path / fnames, pd.Index), pd.Index, Path)
131+
132+
133+
def test_truediv_str_py_str(left_i: pd.Index) -> None:
134+
"""Test pd.Index[Any] (int) / Python str"""
135+
s = "abc"
136+
137+
if TYPE_CHECKING_INVALID_USAGE:
138+
_0 = left_i / s # type: ignore[operator] # pyright:ignore[reportOperatorIssue]
139+
_1 = s / left_i # type: ignore[operator] # pyright:ignore[reportOperatorIssue]

tests/indexes/arithmetic/timedeltaindex/test_truediv.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,8 @@
2222
@pytest.fixture
2323
def left() -> pd.TimedeltaIndex:
2424
"""Left operand"""
25-
return check(
26-
assert_type(pd.to_timedelta(["1 second"]), pd.TimedeltaIndex),
27-
pd.TimedeltaIndex,
28-
pd.Timedelta,
29-
)
25+
lo = pd.to_timedelta(["1 second"])
26+
return check(assert_type(lo, pd.TimedeltaIndex), pd.TimedeltaIndex, pd.Timedelta)
3027

3128

3229
def test_truediv_py_scalar(left: pd.TimedeltaIndex) -> None:

tests/series/arithmetic/bool/test_truediv.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,8 @@
1616
@pytest.fixture
1717
def left() -> "pd.Series[bool]":
1818
"""Left operand"""
19-
return check(
20-
assert_type(pd.Series([True, False, True]), "pd.Series[bool]"),
21-
pd.Series,
22-
np.bool_,
23-
)
19+
lo = pd.Series([True, False, True])
20+
return check(assert_type(lo, "pd.Series[bool]"), pd.Series, np.bool_)
2421

2522

2623
def test_truediv_py_scalar(left: "pd.Series[bool]") -> None:

tests/series/arithmetic/test_truediv.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import numpy as np
55
import pandas as pd
6+
import pytest
67
from typing_extensions import assert_type
78

89
from tests import (
@@ -11,10 +12,15 @@
1112
check,
1213
)
1314

14-
left_i = pd.DataFrame({"a": [1, 2, 3]})["a"] # left operand
1515

16+
@pytest.fixture
17+
def left_i() -> pd.Series:
18+
"""Left operand"""
19+
lo = pd.DataFrame({"a": [1, 2, 3]})["a"]
20+
return check(assert_type(lo, pd.Series), pd.Series, np.integer)
1621

17-
def test_truediv_py_scalar() -> None:
22+
23+
def test_truediv_py_scalar(left_i: pd.Series) -> None:
1824
"""Test pd.Series[Any] (int) / Python native scalars"""
1925
b, i, f, c = True, 1, 1.0, 1j
2026

@@ -49,7 +55,7 @@ def test_truediv_py_scalar() -> None:
4955
check(assert_type(left_i.rdiv(c), pd.Series), pd.Series)
5056

5157

52-
def test_truediv_py_sequence() -> None:
58+
def test_truediv_py_sequence(left_i: pd.Series) -> None:
5359
"""Test pd.Series[Any] (int) / Python native sequences"""
5460
b, i, f, c = [True, False, True], [2, 3, 5], [1.0, 2.0, 3.0], [1j, 1j, 4j]
5561

@@ -84,7 +90,7 @@ def test_truediv_py_sequence() -> None:
8490
check(assert_type(left_i.rdiv(c), pd.Series), pd.Series)
8591

8692

87-
def test_truediv_numpy_array() -> None:
93+
def test_truediv_numpy_array(left_i: pd.Series) -> None:
8894
"""Test pd.Series[Any] (int) / numpy arrays"""
8995
b = np.array([True, False, True], np.bool_)
9096
i = np.array([2, 3, 5], np.int64)
@@ -138,7 +144,7 @@ def test_truediv_numpy_array() -> None:
138144
check(assert_type(left_i.rdiv(c), pd.Series), pd.Series)
139145

140146

141-
def test_truediv_pd_index() -> None:
147+
def test_truediv_pd_index(left_i: pd.Series) -> None:
142148
"""Test pd.Series[Any] (int) / pandas Indexes"""
143149
a = pd.MultiIndex.from_tuples([(1,), (2,), (3,)]).levels[0]
144150
b = pd.Index([True, False, True])
@@ -183,7 +189,7 @@ def test_truediv_pd_index() -> None:
183189
check(assert_type(left_i.rdiv(c), pd.Series), pd.Series)
184190

185191

186-
def test_truediv_pd_series() -> None:
192+
def test_truediv_pd_series(left_i: pd.Series) -> None:
187193
"""Test pd.Series[Any] (int) / pandas Series"""
188194
a = pd.DataFrame({"a": [1, 2, 3]})["a"]
189195
b = pd.Series([True, False, True])
@@ -263,7 +269,7 @@ def test_truediv_path(tmp_path: Path) -> None:
263269
check(assert_type(fnames.rdiv(tmp_path), pd.Series), pd.Series, Path)
264270

265271

266-
def test_truediv_str_py_str() -> None:
272+
def test_truediv_str_py_str(left_i: pd.Series) -> None:
267273
"""Test pd.Series[Any] (int) / Python str"""
268274
s = "abc"
269275

0 commit comments

Comments
 (0)