@@ -65,7 +65,7 @@ def __extension_duck_array__astype(
65
65
subok : bool = True ,
66
66
copy : bool = True ,
67
67
device : str | None = None ,
68
- ) -> T_ExtensionArray :
68
+ ) -> ExtensionArray :
69
69
if (
70
70
not (
71
71
is_extension_array_dtype (array_or_scalar ) or is_extension_array_dtype (dtype )
@@ -82,7 +82,7 @@ def __extension_duck_array__astype(
82
82
@implements (np .asarray )
83
83
def __extension_duck_array__asarray (
84
84
array_or_scalar : np .typing .ArrayLike , dtype : DTypeLikeSave = None
85
- ) -> T_ExtensionArray :
85
+ ) -> ExtensionArray :
86
86
if not is_extension_array_dtype (dtype ):
87
87
return NotImplemented
88
88
@@ -91,9 +91,9 @@ def __extension_duck_array__asarray(
91
91
92
92
def as_extension_array (
93
93
array_or_scalar : np .typing .ArrayLike , dtype : ExtensionDtype , copy : bool = False
94
- ) -> T_ExtensionArray :
94
+ ) -> ExtensionArray :
95
95
if is_scalar (array_or_scalar ):
96
- return dtype .construct_array_type ()._from_sequence (
96
+ return dtype .construct_array_type ()._from_sequence ( # type: ignore[attr-defined]
97
97
[array_or_scalar ], dtype = dtype
98
98
)
99
99
else :
@@ -104,14 +104,17 @@ def as_extension_array(
104
104
def __extension_duck_array__result_type (
105
105
* arrays_and_dtypes : np .typing .ArrayLike | np .typing .DTypeLike ,
106
106
) -> DtypeObj :
107
- extension_arrays_and_dtypes = [
108
- x for x in arrays_and_dtypes if is_extension_array_dtype (x )
107
+ extension_arrays_and_dtypes : list [ExtensionDtype | ExtensionArray ] = [
108
+ x
109
+ for x in arrays_and_dtypes
110
+ if is_extension_array_dtype (x ) # type: ignore[arg-type, misc]
109
111
]
110
112
if not extension_arrays_and_dtypes :
111
113
return NotImplemented
112
114
113
115
ea_dtypes : list [ExtensionDtype ] = [
114
- getattr (x , "dtype" , x ) for x in extension_arrays_and_dtypes
116
+ getattr (x , "dtype" , cast (ExtensionDtype , x ))
117
+ for x in extension_arrays_and_dtypes
115
118
]
116
119
scalars : list [Scalar ] = [
117
120
x for x in arrays_and_dtypes if is_scalar (x ) and x not in {pd .NA , np .nan }
@@ -122,15 +125,17 @@ def __extension_duck_array__result_type(
122
125
other_stuff = [
123
126
x
124
127
for x in arrays_and_dtypes
125
- if not is_extension_array_dtype (x ) and not is_scalar (x )
128
+ if not is_extension_array_dtype (x ) and not is_scalar (x ) # type: ignore[arg-type, misc]
126
129
]
127
130
# We implement one special case: when possible, preserve Categoricals (avoid promoting
128
131
# to object) by merging the categories of all given Categoricals + scalars + NA.
129
132
# Ideally this could be upstreamed into pandas find_result_type / find_common_type.
130
133
if not other_stuff and all (
131
134
isinstance (x , pd .CategoricalDtype ) and not x .ordered for x in ea_dtypes
132
135
):
133
- return union_unordered_categorical_and_scalar (ea_dtypes , scalars )
136
+ return union_unordered_categorical_and_scalar (
137
+ cast (list [pd .CategoricalDtype ], ea_dtypes ), scalars
138
+ )
134
139
if not other_stuff and all (
135
140
isinstance (x , type (ea_type := ea_dtypes [0 ])) for x in ea_dtypes
136
141
):
@@ -146,7 +151,7 @@ def union_unordered_categorical_and_scalar(
146
151
scalars = [x for x in scalars if x is not pd .CategoricalDtype .na_value ]
147
152
all_categories = set ().union (* (x .categories for x in categorical_dtypes ))
148
153
all_categories = all_categories .union (scalars )
149
- return pd .CategoricalDtype (categories = all_categories )
154
+ return pd .CategoricalDtype (categories = list ( all_categories ) )
150
155
151
156
152
157
@implements (np .broadcast_to )
@@ -174,7 +179,7 @@ def __extension_duck_array__where(
174
179
x : T_ExtensionArray ,
175
180
y : T_ExtensionArray | np .ArrayLike ,
176
181
) -> T_ExtensionArray :
177
- return cast (T_ExtensionArray , pd .Series (x ).where (condition , y ).array )
182
+ return cast (T_ExtensionArray , pd .Series (x ).where (condition , y ).array ) # type: ignore[arg-type]
178
183
179
184
180
185
def _replace_duck (args , replacer : Callable [[PandasExtensionArray ]]) -> list :
0 commit comments