Skip to content

TYP: NumericDtype._standardize_dtype #47298

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

Merged
merged 4 commits into from
Jun 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions pandas/core/arrays/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
TYPE_CHECKING,
Any,
Callable,
Mapping,
TypeVar,
)

Expand Down Expand Up @@ -113,11 +114,11 @@ def __from_arrow__(
return array_class._concat_same_type(results)

@classmethod
def _str_to_dtype_mapping(cls):
def _str_to_dtype_mapping(cls) -> Mapping[str, NumericDtype]:
raise AbstractMethodError(cls)

@classmethod
def _standardize_dtype(cls, dtype) -> NumericDtype:
def _standardize_dtype(cls, dtype: NumericDtype | str | np.dtype) -> NumericDtype:
"""
Convert a string representation or a numpy dtype to NumericDtype.
"""
Expand All @@ -126,7 +127,7 @@ def _standardize_dtype(cls, dtype) -> NumericDtype:
# https://github.com/numpy/numpy/pull/7476
dtype = dtype.lower()

if not issubclass(type(dtype), cls):
if not isinstance(dtype, NumericDtype):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is safe to check against NumericDtype and not cls: if it were a more specific sub-class, np.dtype doesn't work with instances of NumericDtype.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(mypy and pyright do not support issubclass(type(some_object), some_class) for type narrowing)

mapping = cls._str_to_dtype_mapping()
try:
dtype = mapping[str(np.dtype(dtype))]
Expand Down
1 change: 0 additions & 1 deletion pyright_reportGeneralTypeIssues.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"pandas/core/arrays/datetimes.py",
"pandas/core/arrays/interval.py",
"pandas/core/arrays/masked.py",
"pandas/core/arrays/numeric.py",
"pandas/core/arrays/period.py",
"pandas/core/arrays/sparse/array.py",
"pandas/core/arrays/sparse/dtype.py",
Expand Down