|
| 1 | +import numpy as np |
| 2 | +from numpy import typing as npt # noqa: F401 |
| 3 | +import pandas as pd |
| 4 | +import pytest |
| 5 | +from typing_extensions import ( |
| 6 | + Never, |
| 7 | + assert_type, |
| 8 | +) |
| 9 | + |
| 10 | +from tests import ( |
| 11 | + TYPE_CHECKING_INVALID_USAGE, |
| 12 | + check, |
| 13 | +) |
| 14 | + |
| 15 | + |
| 16 | +@pytest.fixture |
| 17 | +def left() -> "pd.Index[bool]": |
| 18 | + """Left operand""" |
| 19 | + return check( |
| 20 | + assert_type(pd.Index([True, False, True]), "pd.Index[bool]"), pd.Index, np.bool_ |
| 21 | + ) |
| 22 | + |
| 23 | + |
| 24 | +def test_truediv_py_scalar(left: "pd.Index[bool]") -> None: |
| 25 | + """Test pd.Index[bool] / Python native scalars""" |
| 26 | + b, i, f, c = True, 1, 1.0, 1j |
| 27 | + |
| 28 | + if TYPE_CHECKING_INVALID_USAGE: |
| 29 | + # TODO: python/mypy#20061 |
| 30 | + _00 = left / b # pyright: ignore[reportOperatorIssue] |
| 31 | + check(assert_type(left / i, "pd.Index[float]"), pd.Index, np.floating) |
| 32 | + check(assert_type(left / f, "pd.Index[float]"), pd.Index, np.floating) |
| 33 | + check(assert_type(left / c, "pd.Index[complex]"), pd.Index, np.complexfloating) |
| 34 | + |
| 35 | + if TYPE_CHECKING_INVALID_USAGE: |
| 36 | + # TODO: python/mypy#20061 |
| 37 | + _10 = b / left # pyright: ignore[reportOperatorIssue] |
| 38 | + check(assert_type(i / left, "pd.Index[float]"), pd.Index, np.floating) |
| 39 | + check(assert_type(f / left, "pd.Index[float]"), pd.Index, np.floating) |
| 40 | + check(assert_type(c / left, "pd.Index[complex]"), pd.Index, np.complexfloating) |
| 41 | + |
| 42 | + |
| 43 | +def test_truediv_py_sequence(left: "pd.Index[bool]") -> None: |
| 44 | + """Test pd.Index[bool] / Python native sequences""" |
| 45 | + b, i, f, c = [True, False, True], [2, 3, 5], [1.0, 2.0, 3.0], [1j, 1j, 4j] |
| 46 | + |
| 47 | + check(assert_type(left / b, "pd.Index[float]"), pd.Index, np.floating) |
| 48 | + check(assert_type(left / i, "pd.Index[float]"), pd.Index, np.floating) |
| 49 | + check(assert_type(left / f, "pd.Index[float]"), pd.Index, np.floating) |
| 50 | + check(assert_type(left / c, "pd.Index[complex]"), pd.Index, np.complexfloating) |
| 51 | + |
| 52 | + check(assert_type(b / left, "pd.Index[float]"), pd.Index, np.floating) |
| 53 | + check(assert_type(i / left, "pd.Index[float]"), pd.Index, np.floating) |
| 54 | + check(assert_type(f / left, "pd.Index[float]"), pd.Index, np.floating) |
| 55 | + check(assert_type(c / left, "pd.Index[complex]"), pd.Index, np.complexfloating) |
| 56 | + |
| 57 | + |
| 58 | +def test_truediv_numpy_array(left: "pd.Index[bool]") -> None: |
| 59 | + """Test pd.Index[bool] / numpy arrays""" |
| 60 | + b = np.array([True, False, True], np.bool_) |
| 61 | + i = np.array([2, 3, 5], np.int64) |
| 62 | + f = np.array([1.0, 2.0, 3.0], np.float64) |
| 63 | + c = np.array([1.1j, 2.2j, 4.1j], np.complex128) |
| 64 | + |
| 65 | + if TYPE_CHECKING_INVALID_USAGE: |
| 66 | + assert_type(left / b, Never) |
| 67 | + check(assert_type(left / i, "pd.Index[float]"), pd.Index, np.floating) |
| 68 | + check(assert_type(left / f, "pd.Index[float]"), pd.Index, np.floating) |
| 69 | + check(assert_type(left / c, "pd.Index[complex]"), pd.Index, np.complexfloating) |
| 70 | + |
| 71 | + # `numpy` typing gives the corresponding `ndarray`s in the static type |
| 72 | + # checking, where our `__rtruediv__` cannot override. At runtime, they return |
| 73 | + # `Index` with the correct element type. |
| 74 | + if TYPE_CHECKING_INVALID_USAGE: |
| 75 | + assert_type(b / left, "npt.NDArray[np.float64]") |
| 76 | + check(assert_type(i / left, "npt.NDArray[np.float64]"), pd.Index, np.floating) |
| 77 | + check(assert_type(f / left, "npt.NDArray[np.float64]"), pd.Index, np.floating) |
| 78 | + check( |
| 79 | + assert_type(c / left, "npt.NDArray[np.complex128]"), |
| 80 | + pd.Index, |
| 81 | + np.complexfloating, |
| 82 | + ) |
| 83 | + |
| 84 | + |
| 85 | +def test_truediv_pd_index(left: "pd.Index[bool]") -> None: |
| 86 | + """Test pd.Index[bool] / pandas Indexes""" |
| 87 | + b = pd.Index([True, False, True]) |
| 88 | + i = pd.Index([2, 3, 5]) |
| 89 | + f = pd.Index([1.0, 2.0, 3.0]) |
| 90 | + c = pd.Index([1.1j, 2.2j, 4.1j]) |
| 91 | + |
| 92 | + if TYPE_CHECKING_INVALID_USAGE: |
| 93 | + _00 = left / b # type: ignore[operator] # pyright: ignore[reportOperatorIssue] |
| 94 | + check(assert_type(left / i, "pd.Index[float]"), pd.Index, np.floating) |
| 95 | + check(assert_type(left / f, "pd.Index[float]"), pd.Index, np.floating) |
| 96 | + check(assert_type(left / c, "pd.Index[complex]"), pd.Index, np.complexfloating) |
| 97 | + |
| 98 | + if TYPE_CHECKING_INVALID_USAGE: |
| 99 | + _10 = b / left # type: ignore[operator] # pyright: ignore[reportOperatorIssue] |
| 100 | + check(assert_type(i / left, "pd.Index[float]"), pd.Index, np.floating) |
| 101 | + check(assert_type(f / left, "pd.Index[float]"), pd.Index, np.floating) |
| 102 | + check(assert_type(c / left, "pd.Index[complex]"), pd.Index, np.complexfloating) |
0 commit comments