1
1
import numpy as np
2
2
from numpy import typing as npt # noqa: F401
3
3
import pandas as pd
4
- from typing_extensions import assert_type
4
+ from typing_extensions import (
5
+ Never ,
6
+ assert_type ,
7
+ )
5
8
6
- from tests import check
9
+ from tests import (
10
+ TYPE_CHECKING_INVALID_USAGE ,
11
+ check ,
12
+ )
7
13
8
- # left operand
14
+ # left operands
9
15
left_i = pd .MultiIndex .from_tuples ([(1 ,), (2 ,), (3 ,)]).levels [0 ]
16
+ left_str = pd .MultiIndex .from_tuples ([("1" ,), ("2" ,), ("3_" ,)]).levels [0 ]
10
17
11
18
12
- def test_add_py_scalar () -> None :
13
- """Test pd.Index[Any] + Python native scalars"""
19
+ def test_add_i_py_scalar () -> None :
20
+ """Test pd.Index[Any] (int) + Python native scalars"""
14
21
b , i , f , c = True , 1 , 1.0 , 1j
15
22
16
23
check (assert_type (left_i + b , pd .Index ), pd .Index )
@@ -24,8 +31,8 @@ def test_add_py_scalar() -> None:
24
31
check (assert_type (c + left_i , pd .Index ), pd .Index )
25
32
26
33
27
- def test_add_py_sequence () -> None :
28
- """Test pd.Index[Any] + Python native sequence"""
34
+ def test_add_i_py_sequence () -> None :
35
+ """Test pd.Index[Any] (int) + Python native sequence"""
29
36
b , i , f , c = [True , False , True ], [2 , 3 , 5 ], [1.0 , 2.0 , 3.0 ], [1j , 1j , 4j ]
30
37
31
38
check (assert_type (left_i + b , pd .Index ), pd .Index )
@@ -39,8 +46,8 @@ def test_add_py_sequence() -> None:
39
46
check (assert_type (c + left_i , pd .Index ), pd .Index )
40
47
41
48
42
- def test_add_numpy_array () -> None :
43
- """Test pd.Index[Any] + numpy array"""
49
+ def test_add_i_numpy_array () -> None :
50
+ """Test pd.Index[Any] (int) + numpy array"""
44
51
b = np .array ([True , False , True ], np .bool_ )
45
52
i = np .array ([2 , 3 , 5 ], np .int64 )
46
53
f = np .array ([1.0 , 2.0 , 3.0 ], np .float64 )
@@ -53,7 +60,7 @@ def test_add_numpy_array() -> None:
53
60
54
61
# `numpy` typing gives the corresponding `ndarray`s in the static type
55
62
# checking, where our `__radd__` cannot override. At runtime, they return
56
- # `Series `s.
63
+ # `Index `s.
57
64
# `mypy` thinks the return types are `Any`, which is a bug.
58
65
check (
59
66
assert_type (b + left_i , "npt.NDArray[np.bool_]" ), pd .Index # type: ignore[assert-type]
@@ -69,19 +76,31 @@ def test_add_numpy_array() -> None:
69
76
)
70
77
71
78
72
- def test_add_pd_series () -> None :
73
- """Test pd.Index[Any] + pandas index"""
79
+ def test_add_i_pd_series () -> None :
80
+ """Test pd.Index[Any] (int) + pandas index"""
81
+ a = pd .MultiIndex .from_tuples ([(1 ,), (2 ,), (3 ,)]).levels [0 ]
74
82
b = pd .Index ([True , False , True ])
75
83
i = pd .Index ([2 , 3 , 5 ])
76
84
f = pd .Index ([1.0 , 2.0 , 3.0 ])
77
85
c = pd .Index ([1.1j , 2.2j , 4.1j ])
78
86
87
+ check (assert_type (left_i + a , pd .Index ), pd .Index )
79
88
check (assert_type (left_i + b , pd .Index ), pd .Index )
80
89
check (assert_type (left_i + i , pd .Index ), pd .Index )
81
90
check (assert_type (left_i + f , pd .Index ), pd .Index )
82
91
check (assert_type (left_i + c , pd .Index ), pd .Index )
83
92
93
+ check (assert_type (a + left_i , pd .Index ), pd .Index )
84
94
check (assert_type (b + left_i , pd .Index ), pd .Index )
85
95
check (assert_type (i + left_i , pd .Index ), pd .Index )
86
96
check (assert_type (f + left_i , pd .Index ), pd .Index )
87
97
check (assert_type (c + left_i , pd .Index ), pd .Index )
98
+
99
+
100
+ def test_add_i_py_str () -> None :
101
+ """Test pd.Index[Any] (int) + Python str"""
102
+ s = "abc"
103
+
104
+ if TYPE_CHECKING_INVALID_USAGE :
105
+ assert_type (left_i + s , Never )
106
+ assert_type (s + left_i , Never )
0 commit comments