15
15
TYPE_CHECKING ,
16
16
Any ,
17
17
Sized ,
18
+ TypeVar ,
18
19
cast ,
19
20
overload ,
20
21
)
107
108
_int32_max = np .iinfo (np .int32 ).max
108
109
_int64_max = np .iinfo (np .int64 ).max
109
110
111
+ NumpyArrayT = TypeVar ("NumpyArrayT" , bound = np .ndarray )
112
+
110
113
111
114
def maybe_convert_platform (
112
115
values : list | tuple | range | np .ndarray | ExtensionArray ,
@@ -659,7 +662,10 @@ def _ensure_dtype_type(value, dtype: np.dtype):
659
662
object
660
663
"""
661
664
# Start with exceptions in which we do _not_ cast to numpy types
662
- if dtype == np .object_ :
665
+
666
+ # error: Non-overlapping equality check (left operand type: "dtype[Any]", right
667
+ # operand type: "Type[object_]")
668
+ if dtype == np .object_ : # type: ignore[comparison-overlap]
663
669
return value
664
670
665
671
# Note: before we get here we have already excluded isna(value)
@@ -866,10 +872,10 @@ def maybe_infer_dtype_type(element):
866
872
867
873
868
874
def maybe_upcast (
869
- values : np . ndarray ,
875
+ values : NumpyArrayT ,
870
876
fill_value : Scalar = np .nan ,
871
877
copy : bool = False ,
872
- ) -> tuple [np . ndarray , Scalar ]:
878
+ ) -> tuple [NumpyArrayT , Scalar ]:
873
879
"""
874
880
Provide explicit type promotion and coercion.
875
881
@@ -1093,7 +1099,10 @@ def astype_nansafe(
1093
1099
raise ValueError ("dtype must be np.dtype or ExtensionDtype" )
1094
1100
1095
1101
if arr .dtype .kind in ["m" , "M" ] and (
1096
- issubclass (dtype .type , str ) or dtype == object
1102
+ issubclass (dtype .type , str )
1103
+ # error: Non-overlapping equality check (left operand type: "dtype[Any]", right
1104
+ # operand type: "Type[object]")
1105
+ or dtype == object # type: ignore[comparison-overlap]
1097
1106
):
1098
1107
from pandas .core .construction import ensure_wrapped_if_datetimelike
1099
1108
@@ -1104,7 +1113,9 @@ def astype_nansafe(
1104
1113
return lib .ensure_string_array (arr , skipna = skipna , convert_na_value = False )
1105
1114
1106
1115
elif is_datetime64_dtype (arr ):
1107
- if dtype == np .int64 :
1116
+ # Non-overlapping equality check (left operand type: "dtype[Any]", right
1117
+ # operand type: "Type[signedinteger[Any]]")
1118
+ if dtype == np .int64 : # type: ignore[comparison-overlap]
1108
1119
warnings .warn (
1109
1120
f"casting { arr .dtype } values to int64 with .astype(...) "
1110
1121
"is deprecated and will raise in a future version. "
@@ -1124,7 +1135,9 @@ def astype_nansafe(
1124
1135
raise TypeError (f"cannot astype a datetimelike from [{ arr .dtype } ] to [{ dtype } ]" )
1125
1136
1126
1137
elif is_timedelta64_dtype (arr ):
1127
- if dtype == np .int64 :
1138
+ # error: Non-overlapping equality check (left operand type: "dtype[Any]", right
1139
+ # operand type: "Type[signedinteger[Any]]")
1140
+ if dtype == np .int64 : # type: ignore[comparison-overlap]
1128
1141
warnings .warn (
1129
1142
f"casting { arr .dtype } values to int64 with .astype(...) "
1130
1143
"is deprecated and will raise in a future version. "
@@ -1398,10 +1411,9 @@ def convert_dtypes(
1398
1411
1399
1412
if is_string_dtype (inferred_dtype ):
1400
1413
if not convert_string :
1401
- inferred_dtype = input_array .dtype
1414
+ return input_array .dtype
1402
1415
else :
1403
- inferred_dtype = pandas_dtype ("string" )
1404
- return inferred_dtype
1416
+ return pandas_dtype ("string" )
1405
1417
1406
1418
if convert_integer :
1407
1419
target_int_dtype = pandas_dtype ("Int64" )
@@ -1454,7 +1466,9 @@ def convert_dtypes(
1454
1466
else :
1455
1467
return input_array .dtype
1456
1468
1457
- return inferred_dtype
1469
+ # error: Incompatible return value type (got "Union[str, Union[dtype[Any],
1470
+ # ExtensionDtype]]", expected "Union[dtype[Any], ExtensionDtype]")
1471
+ return inferred_dtype # type: ignore[return-value]
1458
1472
1459
1473
1460
1474
def maybe_infer_to_datetimelike (
@@ -1831,7 +1845,9 @@ def construct_2d_arraylike_from_scalar(
1831
1845
1832
1846
if dtype .kind in ["m" , "M" ]:
1833
1847
value = maybe_unbox_datetimelike_tz_deprecation (value , dtype , stacklevel = 4 )
1834
- elif dtype == object :
1848
+ # error: Non-overlapping equality check (left operand type: "dtype[Any]", right
1849
+ # operand type: "Type[object]")
1850
+ elif dtype == object : # type: ignore[comparison-overlap]
1835
1851
if isinstance (value , (np .timedelta64 , np .datetime64 )):
1836
1852
# calling np.array below would cast to pytimedelta/pydatetime
1837
1853
out = np .empty (shape , dtype = object )
@@ -2206,7 +2222,9 @@ def can_hold_element(arr: ArrayLike, element: Any) -> bool:
2206
2222
return tipo .kind == "b"
2207
2223
return lib .is_bool (element )
2208
2224
2209
- elif dtype == object :
2225
+ # error: Non-overlapping equality check (left operand type: "dtype[Any]", right
2226
+ # operand type: "Type[object]")
2227
+ elif dtype == object : # type: ignore[comparison-overlap]
2210
2228
return True
2211
2229
2212
2230
elif dtype .kind == "S" :
0 commit comments