Skip to content

TYP: Fix typing in ExtensionDtype registry #41203

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 17 commits into from
Aug 3, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
34 changes: 26 additions & 8 deletions pandas/core/dtypes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
TYPE_CHECKING,
Any,
TypeVar,
overload,
)

import numpy as np
Expand All @@ -28,7 +29,7 @@
from pandas.core.arrays import ExtensionArray

# To parameterize on same ExtensionDtype
E = TypeVar("E", bound="ExtensionDtype")
ExtensionDtypeT = TypeVar("ExtensionDtypeT", bound="ExtensionDtype")


class ExtensionDtype:
Expand Down Expand Up @@ -202,7 +203,7 @@ def names(self) -> list[str] | None:
return None

@classmethod
def construct_array_type(cls) -> type_t[ExtensionArray]:
def construct_array_type(cls: type_t[ExtensionDtypeT]) -> type_t[ExtensionArray]:
"""
Return the array type associated with this dtype.

Expand All @@ -213,7 +214,9 @@ def construct_array_type(cls) -> type_t[ExtensionArray]:
raise NotImplementedError

@classmethod
def construct_from_string(cls, string: str):
def construct_from_string(
cls: type_t[ExtensionDtypeT], string: str
) -> ExtensionDtype:
r"""
Construct this type from a string.

Expand Down Expand Up @@ -268,7 +271,7 @@ def construct_from_string(cls, string: str):
return cls()

@classmethod
def is_dtype(cls, dtype: object) -> bool:
def is_dtype(cls: type_t[ExtensionDtypeT], dtype: object) -> bool:
Copy link
Member

Choose a reason for hiding this comment

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

not needed, revert

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

"""
Check if we match 'dtype'.

Expand Down Expand Up @@ -368,7 +371,7 @@ def _get_common_dtype(self, dtypes: list[DtypeObj]) -> DtypeObj | None:
return None


def register_extension_dtype(cls: type[E]) -> type[E]:
def register_extension_dtype(cls: type[ExtensionDtypeT]) -> type[ExtensionDtypeT]:
"""
Register an ExtensionType with pandas as class decorator.

Expand Down Expand Up @@ -424,20 +427,35 @@ def register(self, dtype: type[ExtensionDtype]) -> None:

self.dtypes.append(dtype)

def find(self, dtype: type[ExtensionDtype] | str) -> type[ExtensionDtype] | None:
@overload
def find(self, dtype: type[ExtensionDtype]) -> ExtensionDtype:
...

@overload
def find(self, dtype: ExtensionDtype) -> ExtensionDtype:
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
def find(self, dtype: ExtensionDtype) -> ExtensionDtype:
def find(self, dtype: ExtensionDtypeT) -> ExtensionDtypeT:

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

...

@overload
def find(self, dtype: str) -> ExtensionDtype | None:
...

def find(
self, dtype: type[ExtensionDtype] | ExtensionDtype | str
) -> type[ExtensionDtype] | ExtensionDtype | None:
"""
Parameters
----------
dtype : Type[ExtensionDtype] or str
dtype : ExtensionDtype class or instance or str

Returns
-------
return the first matching dtype, otherwise return None
"""
if not isinstance(dtype, str):
dtype_type = dtype
if not isinstance(dtype, type):
dtype_type = type(dtype)
else:
dtype_type = dtype
if issubclass(dtype_type, ExtensionDtype):
return dtype

Expand Down
4 changes: 1 addition & 3 deletions pandas/core/dtypes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1789,9 +1789,7 @@ def pandas_dtype(dtype) -> DtypeObj:
# registered extension types
result = registry.find(dtype)
if result is not None:
# error: Incompatible return value type (got "Type[ExtensionDtype]",
# expected "Union[dtype, ExtensionDtype]")
return result # type: ignore[return-value]
return result

# try a numpy dtype
# raise a consistent TypeError if failed
Expand Down