diff --git a/pandas/core/construction.py b/pandas/core/construction.py index 92f94f4424ee8..05e267bf83dd6 100644 --- a/pandas/core/construction.py +++ b/pandas/core/construction.py @@ -554,9 +554,8 @@ def sanitize_array( # TODO: copy? subarr = maybe_convert_platform(data) if subarr.dtype == object: - # Argument 1 to "maybe_infer_to_datetimelike" has incompatible - # type "Union[ExtensionArray, ndarray]"; expected "ndarray" - subarr = maybe_infer_to_datetimelike(subarr) # type: ignore[arg-type] + subarr = cast(np.ndarray, subarr) + subarr = maybe_infer_to_datetimelike(subarr) subarr = _sanitize_ndim(subarr, data, dtype, index, allow_2d=allow_2d) @@ -620,9 +619,7 @@ def _sanitize_ndim( if is_object_dtype(dtype) and isinstance(dtype, ExtensionDtype): # i.e. PandasDtype("O") - # error: Argument "dtype" to "asarray_tuplesafe" has incompatible type - # "Type[object]"; expected "Union[str, dtype[Any], None]" - result = com.asarray_tuplesafe(data, dtype=object) # type: ignore[arg-type] + result = com.asarray_tuplesafe(data, dtype=np.dtype("object")) cls = dtype.construct_array_type() result = cls._from_sequence(result, dtype=dtype) else: diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index 40883dd8f747b..03554e67d7931 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -123,9 +123,8 @@ def maybe_convert_platform( arr = values if arr.dtype == object: - # error: Argument 1 to "maybe_convert_objects" has incompatible type - # "Union[ExtensionArray, ndarray]"; expected "ndarray" - arr = lib.maybe_convert_objects(arr) # type: ignore[arg-type] + arr = cast(np.ndarray, arr) + arr = lib.maybe_convert_objects(arr) return arr @@ -1249,13 +1248,12 @@ def astype_array(values: ArrayLike, dtype: DtypeObj, copy: bool = False) -> Arra return values.copy() return values - if isinstance(values, ABCExtensionArray): + if not isinstance(values, np.ndarray): + # i.e. ExtensionArray values = values.astype(dtype, copy=copy) else: - # error: Argument 1 to "astype_nansafe" has incompatible type "ExtensionArray"; - # expected "ndarray" - values = astype_nansafe(values, dtype, copy=copy) # type: ignore[arg-type] + values = astype_nansafe(values, dtype, copy=copy) # in pandas we don't store numpy str dtypes, so convert to object if isinstance(dtype, np.dtype) and issubclass(values.dtype.type, str): @@ -1958,7 +1956,7 @@ def construct_1d_object_array_from_listlike(values: Sized) -> np.ndarray: def construct_1d_ndarray_preserving_na( - values: Sequence, dtype: DtypeObj | None = None, copy: bool = False + values: Sequence, dtype: np.dtype | None = None, copy: bool = False ) -> np.ndarray: """ Construct a new ndarray, coercing `values` to `dtype`, preserving NA. @@ -2003,17 +2001,9 @@ def construct_1d_ndarray_preserving_na( and isinstance(values, np.ndarray) and values.dtype.kind == "f" ): - # Argument 2 to "astype_float_to_int_nansafe" has incompatible - # type "Union[dtype[Any], ExtensionDtype]"; expected "dtype[Any]" - return astype_float_to_int_nansafe( - values, dtype, copy=copy # type: ignore[arg-type] - ) + return astype_float_to_int_nansafe(values, dtype, copy=copy) else: - # error: Argument "dtype" to "array" has incompatible type - # "Union[dtype[Any], ExtensionDtype, None]"; expected "Union[dtype[Any], - # None, type, _SupportsDType, str, Union[Tuple[Any, int], Tuple[Any, - # Union[int, Sequence[int]]], List[Any], _DTypeDict, Tuple[Any, Any]]]" - subarr = np.array(values, dtype=dtype, copy=copy) # type: ignore[arg-type] + subarr = np.array(values, dtype=dtype, copy=copy) return subarr diff --git a/pandas/core/indexes/interval.py b/pandas/core/indexes/interval.py index 894abb0fb1776..1ae0be5e5f5bf 100644 --- a/pandas/core/indexes/interval.py +++ b/pandas/core/indexes/interval.py @@ -1165,6 +1165,8 @@ def interval_range( if periods is not None: periods += 1 + breaks: np.ndarray | TimedeltaIndex | DatetimeIndex + if is_number(endpoint): # force consistency between start/end/freq (lower end if freq skips it) if com.all_not_none(start, end, freq): @@ -1190,16 +1192,8 @@ def interval_range( else: # delegate to the appropriate range function if isinstance(endpoint, Timestamp): - # error: Incompatible types in assignment (expression has type - # "DatetimeIndex", variable has type "ndarray") - breaks = date_range( # type: ignore[assignment] - start=start, end=end, periods=periods, freq=freq - ) + breaks = date_range(start=start, end=end, periods=periods, freq=freq) else: - # error: Incompatible types in assignment (expression has type - # "TimedeltaIndex", variable has type "ndarray") - breaks = timedelta_range( # type: ignore[assignment] - start=start, end=end, periods=periods, freq=freq - ) + breaks = timedelta_range(start=start, end=end, periods=periods, freq=freq) return IntervalIndex.from_breaks(breaks, name=name, closed=closed)