Skip to content

Commit 4b86a0b

Browse files
committed
test dtype
1 parent e5d6aab commit 4b86a0b

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-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: 13 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,
@@ -32,6 +33,7 @@
3233
if TYPE_CHECKING:
3334
from pandas._typing import (
3435
BooleanDtypeArg as BooleanDtypeArg,
36+
BuiltinBooleanDtypeArg as BuiltinBooleanDtypeArg,
3537
BuiltinDtypeArg as BuiltinDtypeArg,
3638
BytesDtypeArg as BytesDtypeArg,
3739
CategoryDtypeArg as CategoryDtypeArg,
@@ -674,3 +676,14 @@ def exception_on_platform(dtype: type | str | ExtensionDtype) -> type[Exception]
674676
if (WINDOWS or MAC) and dtype in {"f16", "float128"}:
675677
return TypeError
676678
return None
679+
680+
681+
def get_dtype(dtype: object) -> Generator[type | str, None, None]:
682+
"""Extract types and string literals from a Union or Literal type."""
683+
if isinstance(dtype, str):
684+
yield dtype
685+
elif isinstance(dtype, type):
686+
yield dtype()
687+
else:
688+
for arg in get_args(dtype):
689+
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)