-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
TYP: narrow type bounds on extract_array #46942
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,20 +9,25 @@ | |
from typing import ( | ||
TYPE_CHECKING, | ||
Any, | ||
Optional, | ||
Sequence, | ||
Union, | ||
cast, | ||
overload, | ||
) | ||
import warnings | ||
|
||
import numpy as np | ||
import numpy.ma as ma | ||
|
||
from pandas._libs import lib | ||
from pandas._libs.tslibs.period import Period | ||
from pandas._typing import ( | ||
AnyArrayLike, | ||
ArrayLike, | ||
Dtype, | ||
DtypeObj, | ||
T, | ||
) | ||
from pandas.errors import IntCastingNaNError | ||
from pandas.util._exceptions import find_stack_level | ||
|
@@ -329,7 +334,8 @@ def array( | |
if dtype is None: | ||
inferred_dtype = lib.infer_dtype(data, skipna=True) | ||
if inferred_dtype == "period": | ||
return PeriodArray._from_sequence(data, copy=copy) | ||
period_data = cast(Union[Sequence[Optional[Period]], AnyArrayLike], data) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wouldn't mind a mypy ignore instead of the cast There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you prefer the ignore? I personally think the cast is more semantically correct, since we checked the type using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have no strong opinion (slightly in favor of ignore, unless it creates many follow up ignores) @simonjayhawkins has a better overview when to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we have checks for redundant casts and also unused ignores so if, in general, the typing issue is likely to be fixed in the future then either are OK (but I personally prefer an ignore since we traditionally add the mypy error as a comment, so when looking at the code, the error message is perhaps more helpful than a cast) In this case, where the issue is that a type if not correctly narrowed by a condition then I am equally happy with a cast adjacent to the condition. |
||
return PeriodArray._from_sequence(period_data, copy=copy) | ||
|
||
elif inferred_dtype == "interval": | ||
return IntervalArray(data, copy=copy) | ||
|
@@ -376,9 +382,23 @@ def array( | |
return PandasArray._from_sequence(data, dtype=dtype, copy=copy) | ||
|
||
|
||
@overload | ||
def extract_array( | ||
obj: object, extract_numpy: bool = False, extract_range: bool = False | ||
) -> Any | ArrayLike: | ||
obj: Series | Index, extract_numpy: bool = ..., extract_range: bool = ... | ||
) -> ArrayLike: | ||
... | ||
|
||
|
||
@overload | ||
def extract_array( | ||
obj: T, extract_numpy: bool = ..., extract_range: bool = ... | ||
) -> T | ArrayLike: | ||
... | ||
|
||
|
||
def extract_array( | ||
obj: T, extract_numpy: bool = False, extract_range: bool = False | ||
) -> T | ArrayLike: | ||
""" | ||
Extract the ndarray or ExtensionArray from a Series or Index. | ||
|
||
|
@@ -425,12 +445,15 @@ def extract_array( | |
if isinstance(obj, ABCRangeIndex): | ||
if extract_range: | ||
return obj._values | ||
return obj | ||
# https://github.com/python/mypy/issues/1081 | ||
# error: Incompatible return value type (got "RangeIndex", expected | ||
# "Union[T, Union[ExtensionArray, ndarray[Any, Any]]]") | ||
return obj # type: ignore[return-value] | ||
|
||
obj = obj._values | ||
return obj._values | ||
|
||
elif extract_numpy and isinstance(obj, ABCPandasArray): | ||
obj = obj.to_numpy() | ||
return obj.to_numpy() | ||
|
||
return obj | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.