|
1 | | -from typing import TYPE_CHECKING |
| 1 | +from typing import ( |
| 2 | + TYPE_CHECKING, |
| 3 | +) |
2 | 4 |
|
3 | 5 | import numpy as np |
4 | 6 | import pandas as pd |
5 | 7 | import pytest |
6 | | -from typing_extensions import assert_type |
| 8 | +from typing_extensions import ( |
| 9 | + Never, |
| 10 | + assert_type, |
| 11 | +) |
7 | 12 |
|
8 | 13 | from tests import ( |
9 | 14 | ASTYPE_FLOAT_NOT_NUMPY16_ARGS, |
| 15 | + TYPE_CHECKING_INVALID_USAGE, |
10 | 16 | TYPE_FLOAT_NOT_NUMPY16_ARGS, |
11 | 17 | check, |
12 | 18 | exception_on_platform, |
@@ -89,14 +95,22 @@ def test_constructor_dtype( |
89 | 95 | assert_type(pd.Index([1.0], dtype="float64[pyarrow]"), "pd.Index[float]") |
90 | 96 | assert_type(pd.Index([1.0], dtype="double[pyarrow]"), "pd.Index[float]") |
91 | 97 |
|
92 | | - # TODO: pandas-dev/pandas-stubs#1501 |
93 | | - # if TYPE_CHECKING_INVALID_USAGE: |
94 | | - # # numpy float16 |
95 | | - # pd.Index([1.0], dtype=np.half) |
96 | | - # pd.Index([1.0], dtype="half") |
97 | | - # pd.Index([1.0], dtype="float16") |
98 | | - # pd.Index([1.0], dtype="e") |
99 | | - # pd.Index([1.0], dtype="f2") |
| 98 | + if TYPE_CHECKING_INVALID_USAGE: |
| 99 | + # numpy float16 |
| 100 | + def _0() -> None: # pyright: ignore[reportUnusedFunction] |
| 101 | + assert_type(pd.Index([1.0], dtype=np.half), Never) |
| 102 | + |
| 103 | + def _1() -> None: # pyright: ignore[reportUnusedFunction] |
| 104 | + assert_type(pd.Index([1.0], dtype="half"), Never) |
| 105 | + |
| 106 | + def _2() -> None: # pyright: ignore[reportUnusedFunction] |
| 107 | + assert_type(pd.Index([1.0], dtype="float16"), Never) |
| 108 | + |
| 109 | + def _3() -> None: # pyright: ignore[reportUnusedFunction] |
| 110 | + assert_type(pd.Index([1.0], dtype="e"), Never) |
| 111 | + |
| 112 | + def _4() -> None: # pyright: ignore[reportUnusedFunction] |
| 113 | + assert_type(pd.Index([1.0], dtype="f2"), Never) |
100 | 114 |
|
101 | 115 |
|
102 | 116 | @pytest.mark.parametrize( |
@@ -149,10 +163,24 @@ def test_astype_float( |
149 | 163 | assert_type(s.astype("float64[pyarrow]"), "pd.Index[float]") |
150 | 164 | assert_type(s.astype("double[pyarrow]"), "pd.Index[float]") |
151 | 165 |
|
152 | | - # if TYPE_CHECKING_INVALID_USAGE: |
153 | | - # # numpy float16 |
154 | | - # s.astype(np.half) |
155 | | - # s.astype("half") |
156 | | - # s.astype("float16") |
157 | | - # s.astype("e") |
158 | | - # s.astype("f2") |
| 166 | + |
| 167 | +def test_new_astype_float16() -> None: |
| 168 | + """Test that a series cannot be built or cast to a float16 type.""" |
| 169 | + s = pd.Index([1, 2, 3]) |
| 170 | + |
| 171 | + if TYPE_CHECKING_INVALID_USAGE: |
| 172 | + |
| 173 | + def _0() -> None: # pyright: ignore[reportUnusedFunction] |
| 174 | + assert_type(s.astype(np.half), Never) |
| 175 | + |
| 176 | + def _1() -> None: # pyright: ignore[reportUnusedFunction] |
| 177 | + assert_type(s.astype("half"), Never) |
| 178 | + |
| 179 | + def _2() -> None: # pyright: ignore[reportUnusedFunction] |
| 180 | + assert_type(s.astype("float16"), Never) |
| 181 | + |
| 182 | + def _3() -> None: # pyright: ignore[reportUnusedFunction] |
| 183 | + assert_type(s.astype("e"), Never) |
| 184 | + |
| 185 | + def _4() -> None: # pyright: ignore[reportUnusedFunction] |
| 186 | + assert_type(s.astype("f2"), Never) |
0 commit comments