|
| 1 | +from typing import Any |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +import pandas as pd |
| 5 | +import pytest |
| 6 | +from typing_extensions import assert_type |
| 7 | + |
| 8 | +from tests import ( |
| 9 | + TYPE_CHECKING_INVALID_USAGE, |
| 10 | + check, |
| 11 | +) |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture |
| 15 | +def left_i() -> "pd.Index[Any]": |
| 16 | + """left operand""" |
| 17 | + lo = pd.MultiIndex.from_tuples([(1,), (2,), (3,)]).levels[0] |
| 18 | + return check(assert_type(lo, pd.Index), pd.Index) |
| 19 | + |
| 20 | + |
| 21 | +def test_mul_py_scalar(left_i: "pd.Index[Any]") -> None: |
| 22 | + """Test pd.Index[Any] (int) * Python native scalars""" |
| 23 | + b, i, f, c = True, 1, 1.0, 1j |
| 24 | + |
| 25 | + check(assert_type(left_i * b, pd.Index), pd.Index) |
| 26 | + check(assert_type(left_i * i, pd.Index), pd.Index) |
| 27 | + check(assert_type(left_i * f, pd.Index), pd.Index) |
| 28 | + check(assert_type(left_i * c, pd.Index), pd.Index) |
| 29 | + |
| 30 | + check(assert_type(b * left_i, pd.Index), pd.Index) |
| 31 | + check(assert_type(i * left_i, pd.Index), pd.Index) |
| 32 | + check(assert_type(f * left_i, pd.Index), pd.Index) |
| 33 | + check(assert_type(c * left_i, pd.Index), pd.Index) |
| 34 | + |
| 35 | + |
| 36 | +def test_mul_py_sequence(left_i: "pd.Index[Any]") -> None: |
| 37 | + """Test pd.Index[Any] (int) * Python native sequences""" |
| 38 | + b, i, f, c = [True, False, True], [2, 3, 5], [1.0, 2.0, 3.0], [1j, 1j, 4j] |
| 39 | + |
| 40 | + check(assert_type(left_i * b, pd.Index), pd.Index) |
| 41 | + check(assert_type(left_i * i, pd.Index), pd.Index) |
| 42 | + check(assert_type(left_i * f, pd.Index), pd.Index) |
| 43 | + check(assert_type(left_i * c, pd.Index), pd.Index) |
| 44 | + |
| 45 | + check(assert_type(b * left_i, pd.Index), pd.Index) |
| 46 | + check(assert_type(i * left_i, pd.Index), pd.Index) |
| 47 | + check(assert_type(f * left_i, pd.Index), pd.Index) |
| 48 | + check(assert_type(c * left_i, pd.Index), pd.Index) |
| 49 | + |
| 50 | + |
| 51 | +def test_mul_numpy_array(left_i: "pd.Index[Any]") -> None: |
| 52 | + """Test pd.Index[Any] (int) * numpy arrays""" |
| 53 | + b = np.array([True, False, True], np.bool_) |
| 54 | + i = np.array([2, 3, 5], np.int64) |
| 55 | + f = np.array([1.0, 2.0, 3.0], np.float64) |
| 56 | + c = np.array([1.1j, 2.2j, 4.1j], np.complex128) |
| 57 | + |
| 58 | + check(assert_type(left_i * b, pd.Index), pd.Index) |
| 59 | + check(assert_type(left_i * i, pd.Index), pd.Index) |
| 60 | + check(assert_type(left_i * f, pd.Index), pd.Index) |
| 61 | + check(assert_type(left_i * c, pd.Index), pd.Index) |
| 62 | + |
| 63 | + # `numpy` typing gives the corresponding `ndarray`s in the static type |
| 64 | + # checking, where our `__rmul__` cannot override. At runtime, they return |
| 65 | + # `Index`. |
| 66 | + # microsoft/pyright#10924 |
| 67 | + check( |
| 68 | + assert_type(b * left_i, Any), # pyright: ignore[reportAssertTypeFailure] |
| 69 | + pd.Index, |
| 70 | + ) |
| 71 | + check( |
| 72 | + assert_type(i * left_i, Any), # pyright: ignore[reportAssertTypeFailure] |
| 73 | + pd.Index, |
| 74 | + ) |
| 75 | + check( |
| 76 | + assert_type(f * left_i, Any), # pyright: ignore[reportAssertTypeFailure] |
| 77 | + pd.Index, |
| 78 | + ) |
| 79 | + check( |
| 80 | + assert_type(c * left_i, Any), # pyright: ignore[reportAssertTypeFailure] |
| 81 | + pd.Index, |
| 82 | + ) |
| 83 | + |
| 84 | + |
| 85 | +def test_mul_pd_index(left_i: "pd.Index[Any]") -> None: |
| 86 | + """Test pd.Index[Any] (int) * pandas Indexes""" |
| 87 | + a = pd.MultiIndex.from_tuples([(1,), (2,), (3,)]).levels[0] |
| 88 | + b = pd.Index([True, False, True]) |
| 89 | + i = pd.Index([2, 3, 5]) |
| 90 | + f = pd.Index([1.0, 2.0, 3.0]) |
| 91 | + c = pd.Index([1.1j, 2.2j, 4.1j]) |
| 92 | + |
| 93 | + check(assert_type(left_i * a, pd.Index), pd.Index) |
| 94 | + check(assert_type(left_i * b, pd.Index), pd.Index) |
| 95 | + check(assert_type(left_i * i, pd.Index), pd.Index) |
| 96 | + check(assert_type(left_i * f, pd.Index), pd.Index) |
| 97 | + check(assert_type(left_i * c, pd.Index), pd.Index) |
| 98 | + |
| 99 | + check(assert_type(a * left_i, pd.Index), pd.Index) |
| 100 | + check(assert_type(b * left_i, pd.Index), pd.Index) |
| 101 | + check(assert_type(i * left_i, pd.Index), pd.Index) |
| 102 | + check(assert_type(f * left_i, pd.Index), pd.Index) |
| 103 | + check(assert_type(c * left_i, pd.Index), pd.Index) |
| 104 | + |
| 105 | + |
| 106 | +def test_mul_str_py_str(left_i: "pd.Index[Any]") -> None: |
| 107 | + """Test pd.Index[Any] (int) * Python str""" |
| 108 | + s = "abc" |
| 109 | + |
| 110 | + if TYPE_CHECKING_INVALID_USAGE: |
| 111 | + _0 = left_i * s # type: ignore[operator] # pyright:ignore[reportOperatorIssue] |
| 112 | + _1 = s * left_i # type: ignore[operator] # pyright:ignore[reportOperatorIssue] |
0 commit comments