Skip to content

Commit 4e00523

Browse files
committed
test dtype
1 parent 4604b6d commit 4e00523

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-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: 12 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,
@@ -242,3 +243,14 @@ def exception_on_platform(dtype: type | str | ExtensionDtype) -> type[Exception]
242243
if (WINDOWS or MAC) and dtype in {"f16", "float128"}:
243244
return TypeError
244245
return None
246+
247+
248+
def get_dtype(dtype: object) -> Generator[type | str, None, None]:
249+
"""Extract types and string literals from a Union or Literal type."""
250+
if isinstance(dtype, str):
251+
yield dtype
252+
elif isinstance(dtype, type):
253+
yield dtype()
254+
else:
255+
for arg in get_args(dtype):
256+
yield from get_dtype(arg)

tests/_typing.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
PandasAstypeFloatDtypeArg,
1919
PandasAstypeTimedeltaDtypeArg,
2020
PandasAstypeTimestampDtypeArg,
21+
PandasBooleanDtypeArg,
2122
PandasFloatDtypeArg,
2223
StrDtypeArg,
2324
TimedeltaDtypeArg,
@@ -53,6 +54,7 @@
5354
"np_1darray",
5455
"np_2darray",
5556
"np_ndarray",
57+
"PandasBooleanDtypeArg",
5658
"np_1darray_bool",
5759
"BooleanDtypeArg",
5860
"BytesDtypeArg",

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+
check,
9+
get_dtype,
10+
)
11+
from tests._typing import PandasBooleanDtypeArg
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)