-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
CLN: Add/refine type hints to some functions in core.dtypes.cast #33286
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 1 commit
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 |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
""" | ||
|
||
from datetime import date, datetime, timedelta | ||
from typing import TYPE_CHECKING, Type | ||
|
||
import numpy as np | ||
|
||
|
@@ -63,13 +64,18 @@ | |
ABCDataFrame, | ||
ABCDatetimeArray, | ||
ABCDatetimeIndex, | ||
ABCExtensionArray, | ||
ABCPeriodArray, | ||
ABCPeriodIndex, | ||
ABCSeries, | ||
) | ||
from pandas.core.dtypes.inference import is_list_like | ||
from pandas.core.dtypes.missing import isna, notna | ||
|
||
if TYPE_CHECKING: | ||
from pandas import Series | ||
from pandas.api.extensions import ExtensionArray | ||
|
||
_int8_max = np.iinfo(np.int8).max | ||
_int16_max = np.iinfo(np.int16).max | ||
_int32_max = np.iinfo(np.int32).max | ||
|
@@ -246,18 +252,16 @@ def trans(x): | |
return result | ||
|
||
|
||
def maybe_cast_result( | ||
result, obj: ABCSeries, numeric_only: bool = False, how: str = "" | ||
): | ||
def maybe_cast_result(result, obj: "Series", numeric_only: bool = False, how: str = ""): | ||
""" | ||
Try casting result to a different type if appropriate | ||
|
||
Parameters | ||
---------- | ||
result : array-like | ||
Result to cast. | ||
obj : ABCSeries | ||
Input series from which result was calculated. | ||
obj : Series | ||
Input Series from which result was calculated. | ||
numeric_only : bool, default False | ||
Whether to cast only numerics or datetimes as well. | ||
how : str, default "" | ||
|
@@ -313,13 +317,13 @@ def maybe_cast_result_dtype(dtype: DtypeObj, how: str) -> DtypeObj: | |
return d.get((dtype, how), dtype) | ||
|
||
|
||
def maybe_cast_to_extension_array(cls, obj, dtype=None): | ||
def maybe_cast_to_extension_array(cls: Type["ExtensionArray"], obj, dtype=None): | ||
""" | ||
Call to `_from_sequence` that returns the object unchanged on Exception. | ||
|
||
Parameters | ||
---------- | ||
cls : ExtensionArray subclass | ||
cls : class, subclass of ExtensionArray | ||
obj : arraylike | ||
Values to pass to cls._from_sequence | ||
dtype : ExtensionDtype, optional | ||
|
@@ -329,6 +333,8 @@ def maybe_cast_to_extension_array(cls, obj, dtype=None): | |
ExtensionArray or obj | ||
""" | ||
assert isinstance(cls, type), f"must pass a type: {cls}" | ||
assertion_msg = f"must pass a subclass of ExtensionArray: {cls}" | ||
assert issubclass(cls, ABCExtensionArray), assertion_msg | ||
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. Is the assert here still necessary, or does the type hint now make this redundant? 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. @simonjayhawkins Would like your thoughts on this. 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. the type hint is sufficient for the static checking (so that mypy doesn't report any errors). For the assert, it depends on the desired runtime behaviour. |
||
try: | ||
result = cls._from_sequence(obj, dtype=dtype) | ||
except Exception: | ||
|
Uh oh!
There was an error while loading. Please reload this page.