|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +from collections.abc import Generator |
3 | 4 | from contextlib import ( |
4 | 5 | AbstractContextManager, |
5 | 6 | nullcontext, |
|
31 | 32 | if TYPE_CHECKING: |
32 | 33 | from pandas._typing import ( |
33 | 34 | BooleanDtypeArg as BooleanDtypeArg, |
| 35 | + BuiltinBooleanDtypeArg as BuiltinBooleanDtypeArg, |
34 | 36 | BytesDtypeArg as BytesDtypeArg, |
35 | 37 | CategoryDtypeArg as CategoryDtypeArg, |
36 | 38 | ComplexDtypeArg as ComplexDtypeArg, |
37 | 39 | Dtype as Dtype, |
38 | 40 | FloatDtypeArg as FloatDtypeArg, |
39 | 41 | IntDtypeArg as IntDtypeArg, |
40 | 42 | ObjectDtypeArg as ObjectDtypeArg, |
| 43 | + PandasBooleanDtypeArg as PandasBooleanDtypeArg, |
41 | 44 | StrDtypeArg as StrDtypeArg, |
42 | 45 | T as T, |
43 | 46 | TimedeltaDtypeArg as TimedeltaDtypeArg, |
|
67 | 70 | np_ndarray_td as np_ndarray_td, |
68 | 71 | ) |
69 | 72 | else: |
| 73 | + # Builtin bool type and its string alias |
| 74 | + BuiltinBooleanDtypeArg: TypeAlias = type[bool] | Literal["bool"] |
| 75 | + # Pandas nullable boolean type and its string alias |
| 76 | + PandasBooleanDtypeArg: TypeAlias = pd.BooleanDtype | Literal["boolean"] |
70 | 77 | _G = TypeVar("_G", bound=np.generic) |
71 | 78 | _S = TypeVar("_S", bound=tuple[int, ...]) |
72 | 79 | # Separately define here so pytest works |
@@ -244,3 +251,14 @@ def pytest_warns_bounded( |
244 | 251 | if upper_exception is None: |
245 | 252 | return nullcontext() |
246 | 253 | return suppress(upper_exception) |
| 254 | + |
| 255 | + |
| 256 | +def get_dtype(dtype: object) -> Generator[type | str, None, None]: |
| 257 | + """Extract types and string literals from a Union or Literal type.""" |
| 258 | + if isinstance(dtype, str): |
| 259 | + yield dtype |
| 260 | + elif isinstance(dtype, type): |
| 261 | + yield dtype() |
| 262 | + else: |
| 263 | + for arg in get_args(dtype): |
| 264 | + yield from get_dtype(arg) |
0 commit comments