Skip to content

Commit c712b31

Browse files
committed
test dtype
1 parent aa89d39 commit c712b31

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

pandas-stubs/core/construction.pyi

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ from pandas._libs.tslibs.period import Period
4040
from pandas._libs.tslibs.timedeltas import Timedelta
4141
from pandas._libs.tslibs.timestamps import Timestamp
4242
from pandas._typing import (
43-
BuiltinBooleanDtypeArg,
4443
BuiltinFloatDtypeArg,
4544
BuiltinIntDtypeArg,
4645
BuiltinStrDtypeArg,
@@ -114,7 +113,7 @@ def array(
114113
@overload
115114
def array( # type: ignore[overload-overlap] # pyright: ignore[reportOverlappingOverload]
116115
data: Sequence[bool | np.bool | NAType | None] | np_ndarray_bool | BooleanArray,
117-
dtype: BuiltinBooleanDtypeArg | PandasBooleanDtypeArg | None = None,
116+
dtype: PandasBooleanDtypeArg | None = None,
118117
copy: bool = True,
119118
) -> BooleanArray: ...
120119
@overload

tests/__init__.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
from collections.abc import Generator
34
from contextlib import (
45
AbstractContextManager,
56
nullcontext,
@@ -31,13 +32,15 @@
3132
if TYPE_CHECKING:
3233
from pandas._typing import (
3334
BooleanDtypeArg as BooleanDtypeArg,
35+
BuiltinBooleanDtypeArg as BuiltinBooleanDtypeArg,
3436
BytesDtypeArg as BytesDtypeArg,
3537
CategoryDtypeArg as CategoryDtypeArg,
3638
ComplexDtypeArg as ComplexDtypeArg,
3739
Dtype as Dtype,
3840
FloatDtypeArg as FloatDtypeArg,
3941
IntDtypeArg as IntDtypeArg,
4042
ObjectDtypeArg as ObjectDtypeArg,
43+
PandasBooleanDtypeArg as PandasBooleanDtypeArg,
4144
StrDtypeArg as StrDtypeArg,
4245
T as T,
4346
TimedeltaDtypeArg as TimedeltaDtypeArg,
@@ -67,6 +70,10 @@
6770
np_ndarray_td as np_ndarray_td,
6871
)
6972
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"]
7077
_G = TypeVar("_G", bound=np.generic)
7178
_S = TypeVar("_S", bound=tuple[int, ...])
7279
# Separately define here so pytest works
@@ -244,3 +251,14 @@ def pytest_warns_bounded(
244251
if upper_exception is None:
245252
return nullcontext()
246253
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)

tests/arrays/test_boolean_array.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import numpy as np
22
import pandas as pd
33
from pandas.core.arrays.boolean import BooleanArray
4+
import pytest
45
from typing_extensions import assert_type
56

6-
from tests import check
7+
from tests import (
8+
PandasBooleanDtypeArg,
9+
check,
10+
get_dtype,
11+
)
712

813

914
def test_constructor() -> None:
@@ -15,3 +20,10 @@ def test_constructor() -> None:
1520
check(assert_type(pd.array(np.array([1], np.bool_)), BooleanArray), BooleanArray)
1621

1722
check(assert_type(pd.array(pd.array([True])), BooleanArray), BooleanArray)
23+
24+
pd.array([True], dtype=pd.BooleanDtype())
25+
26+
27+
@pytest.mark.parametrize("dtype", get_dtype(PandasBooleanDtypeArg))
28+
def test_constructors_dtype(dtype: PandasBooleanDtypeArg):
29+
check(assert_type(pd.array([True], dtype=dtype), BooleanArray), BooleanArray)

0 commit comments

Comments
 (0)