-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
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
Changes from 3 commits
7d4531d
41c5355
03d4e66
7f51bf0
1a71e71
bd21368
d7803f0
246ee33
37affba
3174b0f
4d6dbdb
eaefa6c
9b295a2
d9651a4
b72f9dd
00092c0
c4deaa0
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 | ||||
---|---|---|---|---|---|---|
|
@@ -8,6 +8,7 @@ | |||||
TYPE_CHECKING, | ||||||
Any, | ||||||
TypeVar, | ||||||
overload, | ||||||
) | ||||||
|
||||||
import numpy as np | ||||||
|
@@ -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: | ||||||
|
@@ -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. | ||||||
|
||||||
|
@@ -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: | ||||||
Dr-Irv marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
r""" | ||||||
Construct this type from a string. | ||||||
|
||||||
|
@@ -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: | ||||||
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. not needed, revert 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. done |
||||||
""" | ||||||
Check if we match 'dtype'. | ||||||
|
||||||
|
@@ -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. | ||||||
|
||||||
|
@@ -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: | ||||||
Dr-Irv marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
... | ||||||
|
||||||
@overload | ||||||
def find(self, dtype: ExtensionDtype) -> ExtensionDtype: | ||||||
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.
Suggested change
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. done |
||||||
... | ||||||
|
||||||
@overload | ||||||
def find(self, dtype: str) -> ExtensionDtype | None: | ||||||
... | ||||||
|
||||||
def find( | ||||||
self, dtype: type[ExtensionDtype] | ExtensionDtype | str | ||||||
Dr-Irv marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
) -> 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 | ||||||
|
||||||
|
Uh oh!
There was an error while loading. Please reload this page.