From 1be9f585a928006e6397fde17d6dce7de8011daf Mon Sep 17 00:00:00 2001 From: Omer Ozarslan Date: Thu, 22 Apr 2021 06:51:57 -0500 Subject: [PATCH 1/2] TYP: Fix some typehints for ExtensionDtype --- pandas/core/dtypes/base.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/pandas/core/dtypes/base.py b/pandas/core/dtypes/base.py index 8104b0170fbe2..81b81b0893c24 100644 --- a/pandas/core/dtypes/base.py +++ b/pandas/core/dtypes/base.py @@ -7,6 +7,7 @@ from typing import ( TYPE_CHECKING, Any, + TypeVar, ) import numpy as np @@ -26,6 +27,9 @@ if TYPE_CHECKING: from pandas.core.arrays import ExtensionArray + # To parameterize on same ExtensionDtype + E = TypeVar("E", bound="ExtensionDtype") + class ExtensionDtype: """ @@ -151,7 +155,7 @@ def na_value(self) -> object: return np.nan @property - def type(self) -> type[Any]: + def type(self) -> type_t[Any]: """ The scalar type for the array, e.g. ``int`` @@ -209,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. @@ -364,7 +368,7 @@ def _get_common_dtype(self, dtypes: list[DtypeObj]) -> DtypeObj | None: return None -def register_extension_dtype(cls: type[ExtensionDtype]) -> type[ExtensionDtype]: +def register_extension_dtype(cls: type[E]) -> type[E]: """ Register an ExtensionType with pandas as class decorator. @@ -420,7 +424,7 @@ 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[E] | E | str) -> type[E] | E | ExtensionDtype | None: """ Parameters ---------- @@ -431,8 +435,9 @@ def find(self, dtype: type[ExtensionDtype] | str) -> type[ExtensionDtype] | None return the first matching dtype, otherwise return None """ if not isinstance(dtype, str): - dtype_type = dtype - if not isinstance(dtype, type): + if isinstance(dtype, type): + dtype_type = dtype + else: dtype_type = type(dtype) if issubclass(dtype_type, ExtensionDtype): return dtype From ac2e3e6570d19c31041b75244f6b3a9853d15f9e Mon Sep 17 00:00:00 2001 From: Omer Ozarslan Date: Thu, 22 Apr 2021 08:57:58 -0500 Subject: [PATCH 2/2] Revert minor changes causing mypy issues --- pandas/core/dtypes/base.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/pandas/core/dtypes/base.py b/pandas/core/dtypes/base.py index 81b81b0893c24..9671c340a0a92 100644 --- a/pandas/core/dtypes/base.py +++ b/pandas/core/dtypes/base.py @@ -213,7 +213,7 @@ def construct_array_type(cls) -> type_t[ExtensionArray]: raise NotImplementedError @classmethod - def construct_from_string(cls, string: str) -> ExtensionDtype: + def construct_from_string(cls, string: str): r""" Construct this type from a string. @@ -424,7 +424,7 @@ def register(self, dtype: type[ExtensionDtype]) -> None: self.dtypes.append(dtype) - def find(self, dtype: type[E] | E | str) -> type[E] | E | ExtensionDtype | None: + def find(self, dtype: type[ExtensionDtype] | str) -> type[ExtensionDtype] | None: """ Parameters ---------- @@ -435,9 +435,8 @@ def find(self, dtype: type[E] | E | str) -> type[E] | E | ExtensionDtype | None: return the first matching dtype, otherwise return None """ if not isinstance(dtype, str): - if isinstance(dtype, type): - dtype_type = dtype - else: + dtype_type = dtype + if not isinstance(dtype, type): dtype_type = type(dtype) if issubclass(dtype_type, ExtensionDtype): return dtype