diff --git a/pandas/core/arrays/sparse/dtype.py b/pandas/core/arrays/sparse/dtype.py index 9cdc0d56d0061..1ce735421e7d6 100644 --- a/pandas/core/arrays/sparse/dtype.py +++ b/pandas/core/arrays/sparse/dtype.py @@ -1,7 +1,7 @@ """Sparse Dtype""" import re -from typing import Any, Tuple +from typing import TYPE_CHECKING, Any, Tuple, Type import numpy as np @@ -19,6 +19,9 @@ from pandas.core.dtypes.dtypes import register_extension_dtype from pandas.core.dtypes.missing import isna, na_value_for_dtype +if TYPE_CHECKING: + from pandas.core.arrays.sparse.array import SparseArray # noqa: F401 + @register_extension_dtype class SparseDtype(ExtensionDtype): @@ -137,11 +140,11 @@ def _is_na_fill_value(self): return isna(self.fill_value) @property - def _is_numeric(self): + def _is_numeric(self) -> bool: return not is_object_dtype(self.subtype) @property - def _is_boolean(self): + def _is_boolean(self) -> bool: return is_bool_dtype(self.subtype) @property @@ -167,7 +170,7 @@ def __repr__(self) -> str: return self.name @classmethod - def construct_array_type(cls): + def construct_array_type(cls) -> Type["SparseArray"]: """ Return the array type associated with this dtype. @@ -175,12 +178,12 @@ def construct_array_type(cls): ------- type """ - from pandas.core.arrays.sparse.array import SparseArray + from pandas.core.arrays.sparse.array import SparseArray # noqa: F811 return SparseArray @classmethod - def construct_from_string(cls, string): + def construct_from_string(cls, string: str) -> "SparseDtype": """ Construct a SparseDtype from a string form. @@ -266,7 +269,7 @@ def _parse_subtype(dtype: str) -> Tuple[str, bool]: return subtype, has_fill_value @classmethod - def is_dtype(cls, dtype): + def is_dtype(cls, dtype: object) -> bool: dtype = getattr(dtype, "dtype", dtype) if isinstance(dtype, str) and dtype.startswith("Sparse"): sub_type, _ = cls._parse_subtype(dtype)