11import numpy as np
22from numpy import typing as npt # noqa: F401
33import pandas as pd
4- from typing_extensions import assert_type
4+ from typing_extensions import (
5+ Never ,
6+ assert_type ,
7+ )
58
6- from tests import check
9+ from tests import (
10+ TYPE_CHECKING_INVALID_USAGE ,
11+ check ,
12+ )
713
8- # left operand
14+ # left operands
915left_i = pd .MultiIndex .from_tuples ([(1 ,), (2 ,), (3 ,)]).levels [0 ]
16+ left_str = pd .MultiIndex .from_tuples ([("1" ,), ("2" ,), ("3_" ,)]).levels [0 ]
1017
1118
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"""
1421 b , i , f , c = True , 1 , 1.0 , 1j
1522
1623 check (assert_type (left_i + b , pd .Index ), pd .Index )
@@ -24,8 +31,8 @@ def test_add_py_scalar() -> None:
2431 check (assert_type (c + left_i , pd .Index ), pd .Index )
2532
2633
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"""
2936 b , i , f , c = [True , False , True ], [2 , 3 , 5 ], [1.0 , 2.0 , 3.0 ], [1j , 1j , 4j ]
3037
3138 check (assert_type (left_i + b , pd .Index ), pd .Index )
@@ -39,8 +46,8 @@ def test_add_py_sequence() -> None:
3946 check (assert_type (c + left_i , pd .Index ), pd .Index )
4047
4148
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"""
4451 b = np .array ([True , False , True ], np .bool_ )
4552 i = np .array ([2 , 3 , 5 ], np .int64 )
4653 f = np .array ([1.0 , 2.0 , 3.0 ], np .float64 )
@@ -53,7 +60,7 @@ def test_add_numpy_array() -> None:
5360
5461 # `numpy` typing gives the corresponding `ndarray`s in the static type
5562 # checking, where our `__radd__` cannot override. At runtime, they return
56- # `Series `s.
63+ # `Index `s.
5764 # `mypy` thinks the return types are `Any`, which is a bug.
5865 check (
5966 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:
6976 )
7077
7178
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 ]
7482 b = pd .Index ([True , False , True ])
7583 i = pd .Index ([2 , 3 , 5 ])
7684 f = pd .Index ([1.0 , 2.0 , 3.0 ])
7785 c = pd .Index ([1.1j , 2.2j , 4.1j ])
7886
87+ check (assert_type (left_i + a , pd .Index ), pd .Index )
7988 check (assert_type (left_i + b , pd .Index ), pd .Index )
8089 check (assert_type (left_i + i , pd .Index ), pd .Index )
8190 check (assert_type (left_i + f , pd .Index ), pd .Index )
8291 check (assert_type (left_i + c , pd .Index ), pd .Index )
8392
93+ check (assert_type (a + left_i , pd .Index ), pd .Index )
8494 check (assert_type (b + left_i , pd .Index ), pd .Index )
8595 check (assert_type (i + left_i , pd .Index ), pd .Index )
8696 check (assert_type (f + left_i , pd .Index ), pd .Index )
8797 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