-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
TYP: pandas/core/dtypes/dtypes.py #31384
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
simonjayhawkins
merged 9 commits into
pandas-dev:master
from
simonjayhawkins:pandas/core/dtypes/dtypes.py
Apr 5, 2020
Merged
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e964b80
TYP: pandas/core/dtypes/dtypes.py
simonjayhawkins 41bcea4
Merge remote-tracking branch 'upstream/master' into pandas/core/dtype…
simonjayhawkins af0b634
Merge remote-tracking branch 'upstream/master' into pandas/core/dtype…
simonjayhawkins 60120bb
address comments
simonjayhawkins 5a35bf1
Merge remote-tracking branch 'upstream/master' into pandas/core/dtype…
simonjayhawkins f0d7827
address comment
simonjayhawkins 29b5c40
Merge remote-tracking branch 'upstream/master' into pandas/core/dtype…
simonjayhawkins 61de206
Merge remote-tracking branch 'upstream/master' into pandas/core/dtype…
simonjayhawkins f02010b
Merge remote-tracking branch 'upstream/master' into pandas/core/dtype…
simonjayhawkins 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 |
---|---|---|
@@ -1,6 +1,17 @@ | ||
""" define extension dtypes """ | ||
import re | ||
from typing import Any, Dict, List, MutableMapping, Optional, Tuple, Type, Union, cast | ||
from typing import ( | ||
TYPE_CHECKING, | ||
Any, | ||
Dict, | ||
List, | ||
MutableMapping, | ||
Optional, | ||
Tuple, | ||
Type, | ||
Union, | ||
cast, | ||
) | ||
|
||
import numpy as np | ||
import pytz | ||
|
@@ -13,6 +24,15 @@ | |
from pandas.core.dtypes.generic import ABCCategoricalIndex, ABCDateOffset, ABCIndexClass | ||
from pandas.core.dtypes.inference import is_bool, is_list_like | ||
|
||
if TYPE_CHECKING: | ||
import pyarrow # noqa: F401 | ||
from pandas.core.arrays import ( # noqa: F401 | ||
IntervalArray, | ||
PeriodArray, | ||
DatetimeArray, | ||
) | ||
from pandas import Categorical # noqa: F401 | ||
|
||
str_type = str | ||
|
||
|
||
|
@@ -65,7 +85,7 @@ def register(self, dtype: Type[ExtensionDtype]) -> None: | |
""" | ||
Parameters | ||
---------- | ||
dtype : ExtensionDtype | ||
dtype : Type[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. Can you put this in a non-typing way? Eg "ExtensionDtype class" |
||
""" | ||
if not issubclass(dtype, ExtensionDtype): | ||
raise ValueError("can only register pandas extension dtypes") | ||
|
@@ -119,7 +139,7 @@ class PandasExtensionDtype(ExtensionDtype): | |
# and ExtensionDtype's @properties in the subclasses below. The kind and | ||
# type variables in those subclasses are explicitly typed below. | ||
subdtype = None | ||
str: Optional[str_type] = None | ||
str: str_type | ||
num = 100 | ||
shape: Tuple[int, ...] = tuple() | ||
itemsize = 8 | ||
|
@@ -483,15 +503,15 @@ def _hash_categories(categories, ordered: Ordered = True) -> int: | |
return np.bitwise_xor.reduce(hashed) | ||
|
||
@classmethod | ||
def construct_array_type(cls): | ||
def construct_array_type(cls) -> Type["Categorical"]: | ||
""" | ||
Return the array type associated with this dtype. | ||
|
||
Returns | ||
------- | ||
type | ||
""" | ||
from pandas import Categorical | ||
from pandas import Categorical # noqa: F811 | ||
|
||
return Categorical | ||
|
||
|
@@ -655,9 +675,9 @@ class DatetimeTZDtype(PandasExtensionDtype): | |
_match = re.compile(r"(datetime64|M8)\[(?P<unit>.+), (?P<tz>.+)\]") | ||
_cache: Dict[str_type, PandasExtensionDtype] = {} | ||
|
||
def __init__(self, unit="ns", tz=None): | ||
def __init__(self, unit: Union[str_type, "DatetimeTZDtype"] = "ns", tz=None): | ||
jreback marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if isinstance(unit, DatetimeTZDtype): | ||
unit, tz = unit.unit, unit.tz | ||
unit, tz = unit.unit, unit.tz # type: ignore | ||
jreback marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if unit != "ns": | ||
if isinstance(unit, str) and tz is None: | ||
|
@@ -687,7 +707,7 @@ def __init__(self, unit="ns", tz=None): | |
self._tz = tz | ||
|
||
@property | ||
def unit(self): | ||
def unit(self) -> str_type: | ||
""" | ||
The precision of the datetime data. | ||
""" | ||
|
@@ -701,20 +721,20 @@ def tz(self): | |
return self._tz | ||
|
||
@classmethod | ||
def construct_array_type(cls): | ||
def construct_array_type(cls) -> Type["DatetimeArray"]: | ||
""" | ||
Return the array type associated with this dtype. | ||
|
||
Returns | ||
------- | ||
type | ||
""" | ||
from pandas.core.arrays import DatetimeArray | ||
from pandas.core.arrays import DatetimeArray # noqa: F811 | ||
jreback marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return DatetimeArray | ||
|
||
@classmethod | ||
def construct_from_string(cls, string: str_type): | ||
def construct_from_string(cls, string: str_type) -> "DatetimeTZDtype": | ||
""" | ||
Construct a DatetimeTZDtype from a string. | ||
|
||
|
@@ -772,7 +792,7 @@ def __eq__(self, other: Any) -> bool: | |
and str(self.tz) == str(other.tz) | ||
) | ||
|
||
def __setstate__(self, state): | ||
def __setstate__(self, state) -> None: | ||
# for pickle compat. __get_state__ is defined in the | ||
# PandasExtensionDtype superclass and uses the public properties to | ||
# pickle -> need to set the settable private ones here (see GH26067) | ||
|
@@ -868,7 +888,7 @@ def _parse_dtype_strict(cls, freq): | |
raise ValueError("could not construct PeriodDtype") | ||
|
||
@classmethod | ||
def construct_from_string(cls, string): | ||
def construct_from_string(cls, string: str_type) -> "PeriodDtype": | ||
""" | ||
Strict construction from a string, raise a TypeError if not | ||
possible | ||
|
@@ -918,7 +938,7 @@ def __setstate__(self, state): | |
self._freq = state["freq"] | ||
|
||
@classmethod | ||
def is_dtype(cls, dtype) -> bool: | ||
def is_dtype(cls, dtype: object) -> bool: | ||
""" | ||
Return a boolean if we if the passed type is an actual dtype that we | ||
can match (via string or type) | ||
|
@@ -940,7 +960,7 @@ def is_dtype(cls, dtype) -> bool: | |
return super().is_dtype(dtype) | ||
|
||
@classmethod | ||
def construct_array_type(cls): | ||
def construct_array_type(cls) -> Type["PeriodArray"]: | ||
""" | ||
Return the array type associated with this dtype. | ||
|
||
|
@@ -952,9 +972,13 @@ def construct_array_type(cls): | |
|
||
return PeriodArray | ||
|
||
def __from_arrow__(self, array): | ||
"""Construct PeriodArray from pyarrow Array/ChunkedArray.""" | ||
import pyarrow | ||
def __from_arrow__( | ||
self, array: Union["pyarrow.Array", "pyarrow.ChunkedArray"] | ||
) -> "PeriodArray": | ||
""" | ||
Construct PeriodArray from pyarrow Array/ChunkedArray. | ||
""" | ||
import pyarrow # noqa: F811 | ||
from pandas.core.arrays import PeriodArray | ||
from pandas.core.arrays._arrow_utils import pyarrow_array_to_numpy_and_mask | ||
|
||
|
@@ -1060,7 +1084,7 @@ def subtype(self): | |
return self._subtype | ||
|
||
@classmethod | ||
def construct_array_type(cls): | ||
def construct_array_type(cls) -> Type["IntervalArray"]: | ||
""" | ||
Return the array type associated with this dtype. | ||
|
||
|
@@ -1127,7 +1151,7 @@ def __setstate__(self, state): | |
self._subtype = state["subtype"] | ||
|
||
@classmethod | ||
def is_dtype(cls, dtype) -> bool: | ||
def is_dtype(cls, dtype: object) -> bool: | ||
""" | ||
Return a boolean if we if the passed type is an actual dtype that we | ||
can match (via string or type) | ||
|
@@ -1146,9 +1170,13 @@ def is_dtype(cls, dtype) -> bool: | |
return False | ||
return super().is_dtype(dtype) | ||
|
||
def __from_arrow__(self, array): | ||
"""Construct IntervalArray from pyarrow Array/ChunkedArray.""" | ||
import pyarrow | ||
def __from_arrow__( | ||
self, array: Union["pyarrow.Array", "pyarrow.ChunkedArray"] | ||
) -> "IntervalArray": | ||
""" | ||
Construct IntervalArray from pyarrow Array/ChunkedArray. | ||
""" | ||
import pyarrow # noqa: F811 | ||
from pandas.core.arrays import IntervalArray | ||
|
||
if isinstance(array, pyarrow.Array): | ||
|
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.
This means that running mypy (eg in a pre-commit hook) requires pyarrow to be installed? (which is not a required dependency?)
Are we fine with that? (do we already do that for other deps?)
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. mypy is a static checker.
The behavior if pyarrow is not installed depends on the strictness of the type checking.(ignore_missing_imports config option)
in general,"mypy will assume the type of that module is Any, the dynamic type. This means attempting to access any attribute of the module will automatically succeed"
https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports