-
-
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
Merged
Merged
Changes from 11 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
7d4531d
TYP: Fix typing in ExtensionDtype registry
Dr-Irv 41c5355
fix construct_from_string return type
Dr-Irv 03d4e66
overloads for find. typing cls in ExtensionDtype
Dr-Irv 7f51bf0
Merge remote-tracking branch 'upstream/master' into extensionv1
Dr-Irv 1a71e71
fixes to use more of ExtensionDtypeT
Dr-Irv bd21368
Merge branch 'master' into extensionv1
Dr-Irv d7803f0
Merge remote-tracking branch 'upstream/master' into extensionv1
Dr-Irv 246ee33
Merge remote-tracking branch 'upstream/master' into extensionv1
Dr-Irv 37affba
use type_t, reduce overloads, use npt.DTypeLike
Dr-Irv 3174b0f
undo use of npt and put back type_t[ExtensionDtype]
Dr-Irv 4d6dbdb
cast to the union
Dr-Irv eaefa6c
remove Union in cast
Dr-Irv 9b295a2
Merge remote-tracking branch 'upstream/master' into extensionv1
Dr-Irv d9651a4
add 4th overload
Dr-Irv b72f9dd
Merge remote-tracking branch 'upstream/master' into extensionv1
Dr-Irv 00092c0
Merge remote-tracking branch 'upstream/master' into extensionv1
Dr-Irv c4deaa0
Merge remote-tracking branch 'upstream/master' into extensionv1
Dr-Irv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -8,13 +8,17 @@ | |||||
TYPE_CHECKING, | ||||||
Any, | ||||||
TypeVar, | ||||||
cast, | ||||||
overload, | ||||||
) | ||||||
|
||||||
import numpy as np | ||||||
|
||||||
from pandas._libs.hashtable import object_hash | ||||||
from pandas._typing import ( | ||||||
DtypeObj, | ||||||
NpDtype, | ||||||
Union, | ||||||
type_t, | ||||||
) | ||||||
from pandas.errors import AbstractMethodError | ||||||
|
@@ -29,7 +33,7 @@ | |||||
from pandas.core.arrays import ExtensionArray | ||||||
|
||||||
# To parameterize on same ExtensionDtype | ||||||
E = TypeVar("E", bound="ExtensionDtype") | ||||||
ExtensionDtypeT = TypeVar("ExtensionDtypeT", bound="ExtensionDtype") | ||||||
|
||||||
|
||||||
class ExtensionDtype: | ||||||
|
@@ -206,7 +210,9 @@ def construct_array_type(cls) -> type_t[ExtensionArray]: | |||||
raise AbstractMethodError(cls) | ||||||
|
||||||
@classmethod | ||||||
def construct_from_string(cls, string: str): | ||||||
def construct_from_string( | ||||||
cls: type_t[ExtensionDtypeT], string: str | ||||||
) -> ExtensionDtypeT: | ||||||
r""" | ||||||
Construct this type from a string. | ||||||
|
||||||
|
@@ -368,7 +374,7 @@ def _can_hold_na(self) -> bool: | |||||
return True | ||||||
|
||||||
|
||||||
def register_extension_dtype(cls: type[E]) -> type[E]: | ||||||
def register_extension_dtype(cls: type_t[ExtensionDtypeT]) -> type_t[ExtensionDtypeT]: | ||||||
""" | ||||||
Register an ExtensionType with pandas as class decorator. | ||||||
|
||||||
|
@@ -409,9 +415,9 @@ class Registry: | |||||
""" | ||||||
|
||||||
def __init__(self): | ||||||
self.dtypes: list[type[ExtensionDtype]] = [] | ||||||
self.dtypes: list[type_t[ExtensionDtype]] = [] | ||||||
|
||||||
def register(self, dtype: type[ExtensionDtype]) -> None: | ||||||
def register(self, dtype: type_t[ExtensionDtype]) -> None: | ||||||
""" | ||||||
Parameters | ||||||
---------- | ||||||
|
@@ -422,22 +428,46 @@ 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_t[ExtensionDtypeT]) -> type_t[ExtensionDtypeT]: | ||||||
... | ||||||
|
||||||
@overload | ||||||
def find(self, dtype: ExtensionDtypeT) -> ExtensionDtypeT: | ||||||
... | ||||||
|
||||||
@overload | ||||||
def find( | ||||||
self, dtype: NpDtype | type_t[str | float | int | complex | bool | object] | ||||||
) -> type_t[ExtensionDtypeT] | ExtensionDtype | None: | ||||||
... | ||||||
|
||||||
def find( | ||||||
self, | ||||||
dtype: type_t[ExtensionDtype] | ||||||
| ExtensionDtype | ||||||
| NpDtype | ||||||
| type_t[str | float | int | complex | bool | object], | ||||||
) -> type_t[ExtensionDtype] | ExtensionDtype | None: | ||||||
""" | ||||||
Parameters | ||||||
---------- | ||||||
dtype : Type[ExtensionDtype] or str | ||||||
dtype : ExtensionDtype class or instance or str or numpy dtype or python type | ||||||
|
||||||
Returns | ||||||
------- | ||||||
return the first matching dtype, otherwise return None | ||||||
""" | ||||||
if not isinstance(dtype, str): | ||||||
dtype_type = dtype | ||||||
dtype_type: type_t | ||||||
if not isinstance(dtype, type): | ||||||
dtype_type = type(dtype) | ||||||
else: | ||||||
dtype_type = dtype | ||||||
if issubclass(dtype_type, ExtensionDtype): | ||||||
return dtype | ||||||
# cast needed here as mypy doesn't know we have figured | ||||||
# out it is an ExtensionDtype or type_t[ExtensionDtype] | ||||||
return cast(Union[ExtensionDtype, type_t[ExtensionDtype]], dtype) | ||||||
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. nit, to avoid adding the Union import, i have a preference for,
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. fixed in next commit |
||||||
|
||||||
return None | ||||||
|
||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
arent these subsumed by NpDtype?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No,
NpDtype
includesstr
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does #41945 help simplify
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, in next commit
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Had to undo this