Skip to content

Is ExtensionDtype.type a property or a ClassVar? #51736

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions pandas/core/arrays/boolean.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,8 @@ class BooleanDtype(BaseMaskedDtype):

name = "boolean"

# https://github.com/python/mypy/issues/4125
# error: Signature of "type" incompatible with supertype "BaseMaskedDtype"
@property
def type(self) -> type: # type: ignore[override]
def type(self) -> type[np.bool_]:
return np.bool_

@property
Expand Down
21 changes: 16 additions & 5 deletions pandas/core/dtypes/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
npt,
type_t,
)
from pandas.util._decorators import doc

from pandas.core.dtypes.base import (
ExtensionDtype,
Expand Down Expand Up @@ -79,7 +80,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
Expand Down Expand Up @@ -176,13 +176,17 @@ class CategoricalDtype(PandasExtensionDtype, ExtensionDtype):

# TODO: Document public vs. private API
name = "category"
type: type[CategoricalDtypeType] = CategoricalDtypeType
kind: str_type = "O"
str = "|O08"
base = np.dtype("O")
_metadata = ("categories", "ordered")
_cache_dtypes: dict[str_type, PandasExtensionDtype] = {}

@property
@doc(ExtensionDtype.type)
def type(self) -> type[CategoricalDtypeType]:
return CategoricalDtypeType

def __init__(self, categories=None, ordered: Ordered = False) -> None:
self._finalize(categories, ordered, fastpath=False)

Expand Down Expand Up @@ -666,14 +670,18 @@ class DatetimeTZDtype(PandasExtensionDtype):
datetime64[ns, tzfile('/usr/share/zoneinfo/US/Central')]
"""

type: type[Timestamp] = Timestamp
kind: str_type = "M"
num = 101
base = np.dtype("M8[ns]") # TODO: depend on reso?
_metadata = ("unit", "tz")
_match = re.compile(r"(datetime64|M8)\[(?P<unit>.+), (?P<tz>.+)\]")
_cache_dtypes: dict[str_type, PandasExtensionDtype] = {}

@property
@doc(ExtensionDtype.type)
def type(self) -> type[Timestamp]:
return Timestamp

@property
def na_value(self) -> NaTType:
return NaT
Expand Down Expand Up @@ -847,7 +855,6 @@ class PeriodDtype(PeriodDtypeBase, PandasExtensionDtype):
period[M]
"""

type: type[Period] = Period
kind: str_type = "O"
str = "|O08"
base = np.dtype("O")
Expand All @@ -856,6 +863,11 @@ class PeriodDtype(PeriodDtypeBase, PandasExtensionDtype):
_match = re.compile(r"(P|p)eriod\[(?P<freq>.+)\]")
_cache_dtypes: dict[str_type, PandasExtensionDtype] = {}

@property
@doc(ExtensionDtype.type)
def type(self) -> type[Period]:
return Period

def __new__(cls, freq=None):
"""
Parameters
Expand Down Expand Up @@ -1407,7 +1419,6 @@ class BaseMaskedDtype(ExtensionDtype):

name: str
base = None
type: type

@property
def na_value(self) -> libmissing.NAType:
Expand Down
7 changes: 1 addition & 6 deletions pandas/core/reshape/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -2441,12 +2441,7 @@ def _convert_arrays_and_get_rizer_klass(
else:
lk = lk.astype(dtype)
rk = rk.astype(dtype)
if isinstance(lk, BaseMaskedArray):
# Invalid index type "type" for "Dict[Type[object], Type[Factorizer]]";
# expected type "Type[object]"
klass = _factorizers[lk.dtype.type] # type: ignore[index]
else:
klass = _factorizers[lk.dtype.type]
klass = _factorizers[lk.dtype.type]

else:
klass = libhashtable.ObjectFactorizer
Expand Down