Skip to content

Commit 4044eb0

Browse files
committed
test dtype
1 parent 2afbe2d commit 4044eb0

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,
@@ -33,6 +34,7 @@
3334
if TYPE_CHECKING:
3435
from pandas._typing import (
3536
BooleanDtypeArg as BooleanDtypeArg,
37+
BuiltinBooleanDtypeArg as BuiltinBooleanDtypeArg,
3638
BuiltinDtypeArg as BuiltinDtypeArg,
3739
BytesDtypeArg as BytesDtypeArg,
3840
CategoryDtypeArg as CategoryDtypeArg,
@@ -672,3 +674,14 @@ def pytest_warns_bounded(
672674

673675
def is_dtype_invalid_for_platform(dtype: type | str | ExtensionDtype) -> bool:
674676
return (WINDOWS or MAC_ARM) and dtype in {"f16", "float128"}
677+
678+
679+
def get_dtype(dtype: object) -> Generator[type | str, None, None]:
680+
"""Extract types and string literals from a Union or Literal type."""
681+
if isinstance(dtype, str):
682+
yield dtype
683+
elif isinstance(dtype, type):
684+
yield dtype()
685+
else:
686+
for arg in get_args(dtype):
687+
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)