From 48ee962bb09a55484a8f1fa2b27ef478bd194956 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Mon, 7 Aug 2023 22:08:26 -0400 Subject: [PATCH 1/2] TYP:ExtensionDtype.name/type as ClassVar --- pandas/core/arrays/boolean.py | 3 ++- pandas/core/arrays/floating.py | 10 ++++++---- pandas/core/arrays/integer.py | 34 ++++++++++++++++++---------------- pandas/core/arrays/masked.py | 1 + pandas/core/arrays/string_.py | 5 ++--- pandas/core/dtypes/base.py | 32 ++++++++++++++------------------ pandas/core/dtypes/dtypes.py | 14 +++++--------- 7 files changed, 48 insertions(+), 51 deletions(-) diff --git a/pandas/core/arrays/boolean.py b/pandas/core/arrays/boolean.py index 43344f04085ae..04e6f0a0bcdde 100644 --- a/pandas/core/arrays/boolean.py +++ b/pandas/core/arrays/boolean.py @@ -3,6 +3,7 @@ import numbers from typing import ( TYPE_CHECKING, + ClassVar, cast, ) @@ -60,7 +61,7 @@ class BooleanDtype(BaseMaskedDtype): BooleanDtype """ - name = "boolean" + name: ClassVar[str] = "boolean" # https://github.com/python/mypy/issues/4125 # error: Signature of "type" incompatible with supertype "BaseMaskedDtype" diff --git a/pandas/core/arrays/floating.py b/pandas/core/arrays/floating.py index bd3c03f9218d4..c85a5479f3e58 100644 --- a/pandas/core/arrays/floating.py +++ b/pandas/core/arrays/floating.py @@ -1,5 +1,7 @@ from __future__ import annotations +from typing import ClassVar + import numpy as np from pandas.core.dtypes.base import register_extension_dtype @@ -155,15 +157,15 @@ class FloatingArray(NumericArray): @register_extension_dtype class Float32Dtype(FloatingDtype): - type = np.float32 - name = "Float32" + type: ClassVar[type[np.float32]] = np.float32 + name: ClassVar[str] = "Float32" __doc__ = _dtype_docstring.format(dtype="float32") @register_extension_dtype class Float64Dtype(FloatingDtype): - type = np.float64 - name = "Float64" + type: ClassVar[type[np.float64]] = np.float64 + name: ClassVar[str] = "Float64" __doc__ = _dtype_docstring.format(dtype="float64") diff --git a/pandas/core/arrays/integer.py b/pandas/core/arrays/integer.py index 0e6e7a484bbb7..55a84214be532 100644 --- a/pandas/core/arrays/integer.py +++ b/pandas/core/arrays/integer.py @@ -1,5 +1,7 @@ from __future__ import annotations +from typing import ClassVar + import numpy as np from pandas.core.dtypes.base import register_extension_dtype @@ -204,57 +206,57 @@ class IntegerArray(NumericArray): @register_extension_dtype class Int8Dtype(IntegerDtype): - type = np.int8 - name = "Int8" + type: ClassVar[type[np.int8]] = np.int8 + name: ClassVar[str] = "Int8" __doc__ = _dtype_docstring.format(dtype="int8") @register_extension_dtype class Int16Dtype(IntegerDtype): - type = np.int16 - name = "Int16" + type: ClassVar[type[np.int16]] = np.int16 + name: ClassVar[str] = "Int16" __doc__ = _dtype_docstring.format(dtype="int16") @register_extension_dtype class Int32Dtype(IntegerDtype): - type = np.int32 - name = "Int32" + type: ClassVar[type[np.int32]] = np.int32 + name: ClassVar[str] = "Int32" __doc__ = _dtype_docstring.format(dtype="int32") @register_extension_dtype class Int64Dtype(IntegerDtype): - type = np.int64 - name = "Int64" + type: ClassVar[type[np.int64]] = np.int64 + name: ClassVar[str] = "Int64" __doc__ = _dtype_docstring.format(dtype="int64") @register_extension_dtype class UInt8Dtype(IntegerDtype): - type = np.uint8 - name = "UInt8" + type: ClassVar[type[np.uint8]] = np.uint8 + name: ClassVar[str] = "UInt8" __doc__ = _dtype_docstring.format(dtype="uint8") @register_extension_dtype class UInt16Dtype(IntegerDtype): - type = np.uint16 - name = "UInt16" + type: ClassVar[type[np.uint16]] = np.uint16 + name: ClassVar[str] = "UInt16" __doc__ = _dtype_docstring.format(dtype="uint16") @register_extension_dtype class UInt32Dtype(IntegerDtype): - type = np.uint32 - name = "UInt32" + type: ClassVar[type[np.uint32]] = np.uint32 + name: ClassVar[str] = "UInt32" __doc__ = _dtype_docstring.format(dtype="uint32") @register_extension_dtype class UInt64Dtype(IntegerDtype): - type = np.uint64 - name = "UInt64" + type: ClassVar[type[np.uint64]] = np.uint64 + name: ClassVar[str] = "UInt64" __doc__ = _dtype_docstring.format(dtype="uint64") diff --git a/pandas/core/arrays/masked.py b/pandas/core/arrays/masked.py index bec875f2bbfa1..a14de26e1ff6c 100644 --- a/pandas/core/arrays/masked.py +++ b/pandas/core/arrays/masked.py @@ -4,6 +4,7 @@ TYPE_CHECKING, Any, Callable, + ClassVar, Literal, overload, ) diff --git a/pandas/core/arrays/string_.py b/pandas/core/arrays/string_.py index 25f1c2ec6ce4f..131222dff5765 100644 --- a/pandas/core/arrays/string_.py +++ b/pandas/core/arrays/string_.py @@ -2,6 +2,7 @@ from typing import ( TYPE_CHECKING, + ClassVar, Literal, ) @@ -118,9 +119,7 @@ def __init__(self, storage=None) -> None: ) self.storage = storage - @property - def type(self) -> type[str]: - return str + type: ClassVar[type_t[str]] = str @classmethod def construct_from_string(cls, string): diff --git a/pandas/core/dtypes/base.py b/pandas/core/dtypes/base.py index bc776434b2e6e..db4ee4c7e13ec 100644 --- a/pandas/core/dtypes/base.py +++ b/pandas/core/dtypes/base.py @@ -6,6 +6,7 @@ from typing import ( TYPE_CHECKING, Any, + ClassVar, TypeVar, cast, overload, @@ -158,17 +159,15 @@ def na_value(self) -> object: """ return np.nan - @property - def type(self) -> type_t[Any]: - """ - The scalar type for the array, e.g. ``int`` + """ + The scalar type for the array, e.g. ``int`` - It's expected ``ExtensionArray[item]`` returns an instance - of ``ExtensionDtype.type`` for scalar ``item``, assuming - that value is valid (not NA). NA values do not need to be - instances of `type`. - """ - raise AbstractMethodError(self) + It's expected ``ExtensionArray[item]`` returns an instance + of ``ExtensionDtype.type`` for scalar ``item``, assuming + that value is valid (not NA). NA values do not need to be + instances of `type`. + """ + type: ClassVar[type] = NotImplementedError @property def kind(self) -> str: @@ -186,14 +185,12 @@ def kind(self) -> str: """ return "O" - @property - def name(self) -> str: - """ - A string identifying the data type. + """ + A string identifying the data type. - Will be used for display in, e.g. ``Series.dtype`` - """ - raise AbstractMethodError(self) + Will be used for display in, e.g. ``Series.dtype`` + """ + name: ClassVar[str] = "NotImplementedError" @property def names(self) -> list[str] | None: @@ -410,7 +407,6 @@ def _is_immutable(self) -> bool: class StorageExtensionDtype(ExtensionDtype): """ExtensionDtype that may be backed by more than one implementation.""" - name: str _metadata = ("storage",) def __init__(self, storage: str | None = None) -> None: diff --git a/pandas/core/dtypes/dtypes.py b/pandas/core/dtypes/dtypes.py index 53f0fb2843653..accbfbe5e6b2a 100644 --- a/pandas/core/dtypes/dtypes.py +++ b/pandas/core/dtypes/dtypes.py @@ -14,6 +14,7 @@ from typing import ( TYPE_CHECKING, Any, + ClassVar, cast, ) import warnings @@ -103,7 +104,6 @@ class PandasExtensionDtype(ExtensionDtype): THIS IS NOT A REAL NUMPY DTYPE """ - type: Any kind: Any # The Any type annotations above are here only because mypy seems to have a # problem dealing with multiple inheritance from PandasExtensionDtype @@ -200,7 +200,7 @@ class CategoricalDtype(PandasExtensionDtype, ExtensionDtype): # TODO: Document public vs. private API name = "category" - type: type[CategoricalDtypeType] = CategoricalDtypeType + type: ClassVar[type[CategoricalDtypeType]] = CategoricalDtypeType kind: str_type = "O" str = "|O08" base = np.dtype("O") @@ -711,7 +711,7 @@ class DatetimeTZDtype(PandasExtensionDtype): datetime64[ns, Europe/Paris] """ - type: type[Timestamp] = Timestamp + type: ClassVar[type[Timestamp]] = Timestamp kind: str_type = "M" num = 101 _metadata = ("unit", "tz") @@ -941,7 +941,7 @@ class PeriodDtype(PeriodDtypeBase, PandasExtensionDtype): period[M] """ - type: type[Period] = Period + type: ClassVar[type[Period]] = Period kind: str_type = "O" str = "|O08" base = np.dtype("O") @@ -1285,9 +1285,7 @@ def construct_from_string(cls, string: str_type) -> IntervalDtype: ) raise TypeError(msg) - @property - def type(self) -> type[Interval]: - return Interval + type: ClassVar[type[Interval]] = Interval def __str__(self) -> str_type: if self.subtype is None: @@ -1488,9 +1486,7 @@ class BaseMaskedDtype(ExtensionDtype): Base class for dtypes for BaseMaskedArray subclasses. """ - name: str base = None - type: type @property def na_value(self) -> libmissing.NAType: From 50141835d2e2f78bed5271a078bd8543a7bb3fef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Wed, 9 Aug 2023 10:30:29 -0400 Subject: [PATCH 2/2] no code changes; only .name --- pandas/core/arrays/floating.py | 4 ++-- pandas/core/arrays/integer.py | 16 ++++++++-------- pandas/core/arrays/masked.py | 1 - pandas/core/arrays/string_.py | 8 ++++++-- pandas/core/dtypes/base.py | 32 ++++++++++++++++++-------------- pandas/core/dtypes/dtypes.py | 13 ++++++++----- 6 files changed, 42 insertions(+), 32 deletions(-) diff --git a/pandas/core/arrays/floating.py b/pandas/core/arrays/floating.py index c85a5479f3e58..40c8347c6f1a3 100644 --- a/pandas/core/arrays/floating.py +++ b/pandas/core/arrays/floating.py @@ -157,14 +157,14 @@ class FloatingArray(NumericArray): @register_extension_dtype class Float32Dtype(FloatingDtype): - type: ClassVar[type[np.float32]] = np.float32 + type = np.float32 name: ClassVar[str] = "Float32" __doc__ = _dtype_docstring.format(dtype="float32") @register_extension_dtype class Float64Dtype(FloatingDtype): - type: ClassVar[type[np.float64]] = np.float64 + type = np.float64 name: ClassVar[str] = "Float64" __doc__ = _dtype_docstring.format(dtype="float64") diff --git a/pandas/core/arrays/integer.py b/pandas/core/arrays/integer.py index 55a84214be532..f9384e25ba9d9 100644 --- a/pandas/core/arrays/integer.py +++ b/pandas/core/arrays/integer.py @@ -206,56 +206,56 @@ class IntegerArray(NumericArray): @register_extension_dtype class Int8Dtype(IntegerDtype): - type: ClassVar[type[np.int8]] = np.int8 + type = np.int8 name: ClassVar[str] = "Int8" __doc__ = _dtype_docstring.format(dtype="int8") @register_extension_dtype class Int16Dtype(IntegerDtype): - type: ClassVar[type[np.int16]] = np.int16 + type = np.int16 name: ClassVar[str] = "Int16" __doc__ = _dtype_docstring.format(dtype="int16") @register_extension_dtype class Int32Dtype(IntegerDtype): - type: ClassVar[type[np.int32]] = np.int32 + type = np.int32 name: ClassVar[str] = "Int32" __doc__ = _dtype_docstring.format(dtype="int32") @register_extension_dtype class Int64Dtype(IntegerDtype): - type: ClassVar[type[np.int64]] = np.int64 + type = np.int64 name: ClassVar[str] = "Int64" __doc__ = _dtype_docstring.format(dtype="int64") @register_extension_dtype class UInt8Dtype(IntegerDtype): - type: ClassVar[type[np.uint8]] = np.uint8 + type = np.uint8 name: ClassVar[str] = "UInt8" __doc__ = _dtype_docstring.format(dtype="uint8") @register_extension_dtype class UInt16Dtype(IntegerDtype): - type: ClassVar[type[np.uint16]] = np.uint16 + type = np.uint16 name: ClassVar[str] = "UInt16" __doc__ = _dtype_docstring.format(dtype="uint16") @register_extension_dtype class UInt32Dtype(IntegerDtype): - type: ClassVar[type[np.uint32]] = np.uint32 + type = np.uint32 name: ClassVar[str] = "UInt32" __doc__ = _dtype_docstring.format(dtype="uint32") @register_extension_dtype class UInt64Dtype(IntegerDtype): - type: ClassVar[type[np.uint64]] = np.uint64 + type = np.uint64 name: ClassVar[str] = "UInt64" __doc__ = _dtype_docstring.format(dtype="uint64") diff --git a/pandas/core/arrays/masked.py b/pandas/core/arrays/masked.py index a14de26e1ff6c..bec875f2bbfa1 100644 --- a/pandas/core/arrays/masked.py +++ b/pandas/core/arrays/masked.py @@ -4,7 +4,6 @@ TYPE_CHECKING, Any, Callable, - ClassVar, Literal, overload, ) diff --git a/pandas/core/arrays/string_.py b/pandas/core/arrays/string_.py index 131222dff5765..e4305abc97cbb 100644 --- a/pandas/core/arrays/string_.py +++ b/pandas/core/arrays/string_.py @@ -97,7 +97,9 @@ class StringDtype(StorageExtensionDtype): string[pyarrow] """ - name = "string" + # error: Cannot override instance variable (previously declared on + # base class "StorageExtensionDtype") with class variable + name: ClassVar[str] = "string" # type: ignore[misc] #: StringDtype().na_value uses pandas.NA @property @@ -119,7 +121,9 @@ def __init__(self, storage=None) -> None: ) self.storage = storage - type: ClassVar[type_t[str]] = str + @property + def type(self) -> type[str]: + return str @classmethod def construct_from_string(cls, string): diff --git a/pandas/core/dtypes/base.py b/pandas/core/dtypes/base.py index db4ee4c7e13ec..bc776434b2e6e 100644 --- a/pandas/core/dtypes/base.py +++ b/pandas/core/dtypes/base.py @@ -6,7 +6,6 @@ from typing import ( TYPE_CHECKING, Any, - ClassVar, TypeVar, cast, overload, @@ -159,15 +158,17 @@ def na_value(self) -> object: """ return np.nan - """ - The scalar type for the array, e.g. ``int`` + @property + def type(self) -> type_t[Any]: + """ + The scalar type for the array, e.g. ``int`` - It's expected ``ExtensionArray[item]`` returns an instance - of ``ExtensionDtype.type`` for scalar ``item``, assuming - that value is valid (not NA). NA values do not need to be - instances of `type`. - """ - type: ClassVar[type] = NotImplementedError + It's expected ``ExtensionArray[item]`` returns an instance + of ``ExtensionDtype.type`` for scalar ``item``, assuming + that value is valid (not NA). NA values do not need to be + instances of `type`. + """ + raise AbstractMethodError(self) @property def kind(self) -> str: @@ -185,12 +186,14 @@ def kind(self) -> str: """ return "O" - """ - A string identifying the data type. + @property + def name(self) -> str: + """ + A string identifying the data type. - Will be used for display in, e.g. ``Series.dtype`` - """ - name: ClassVar[str] = "NotImplementedError" + Will be used for display in, e.g. ``Series.dtype`` + """ + raise AbstractMethodError(self) @property def names(self) -> list[str] | None: @@ -407,6 +410,7 @@ def _is_immutable(self) -> bool: class StorageExtensionDtype(ExtensionDtype): """ExtensionDtype that may be backed by more than one implementation.""" + name: str _metadata = ("storage",) def __init__(self, storage: str | None = None) -> None: diff --git a/pandas/core/dtypes/dtypes.py b/pandas/core/dtypes/dtypes.py index accbfbe5e6b2a..368e499d3da33 100644 --- a/pandas/core/dtypes/dtypes.py +++ b/pandas/core/dtypes/dtypes.py @@ -14,7 +14,6 @@ from typing import ( TYPE_CHECKING, Any, - ClassVar, cast, ) import warnings @@ -104,6 +103,7 @@ class PandasExtensionDtype(ExtensionDtype): THIS IS NOT A REAL NUMPY DTYPE """ + type: Any kind: Any # The Any type annotations above are here only because mypy seems to have a # problem dealing with multiple inheritance from PandasExtensionDtype @@ -200,7 +200,7 @@ class CategoricalDtype(PandasExtensionDtype, ExtensionDtype): # TODO: Document public vs. private API name = "category" - type: ClassVar[type[CategoricalDtypeType]] = CategoricalDtypeType + type: type[CategoricalDtypeType] = CategoricalDtypeType kind: str_type = "O" str = "|O08" base = np.dtype("O") @@ -711,7 +711,7 @@ class DatetimeTZDtype(PandasExtensionDtype): datetime64[ns, Europe/Paris] """ - type: ClassVar[type[Timestamp]] = Timestamp + type: type[Timestamp] = Timestamp kind: str_type = "M" num = 101 _metadata = ("unit", "tz") @@ -941,7 +941,7 @@ class PeriodDtype(PeriodDtypeBase, PandasExtensionDtype): period[M] """ - type: ClassVar[type[Period]] = Period + type: type[Period] = Period kind: str_type = "O" str = "|O08" base = np.dtype("O") @@ -1285,7 +1285,9 @@ def construct_from_string(cls, string: str_type) -> IntervalDtype: ) raise TypeError(msg) - type: ClassVar[type[Interval]] = Interval + @property + def type(self) -> type[Interval]: + return Interval def __str__(self) -> str_type: if self.subtype is None: @@ -1487,6 +1489,7 @@ class BaseMaskedDtype(ExtensionDtype): """ base = None + type: type @property def na_value(self) -> libmissing.NAType: