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
Changes from 2 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
15 changes: 9 additions & 6 deletions pandas/core/dtypes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,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 @@ -213,7 +213,7 @@ def construct_array_type(cls) -> type_t[ExtensionArray]:
raise NotImplementedError

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

Expand Down Expand Up @@ -368,7 +368,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 +424,23 @@ def register(self, dtype: type[ExtensionDtype]) -> None:

self.dtypes.append(dtype)

def find(self, dtype: type[ExtensionDtype] | str) -> type[ExtensionDtype] | None:
def find(
self, dtype: type[ExtensionDtype] | ExtensionDtype | str
) -> type[ExtensionDtype] | ExtensionDtype | None:
"""
Parameters
----------
dtype : Type[ExtensionDtype] or str
dtype : Type[ExtensionDtype] or ExtensionDtype 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