Skip to content

Commit c79801c

Browse files
twoertweinpmhatre1
authored andcommitted
TYP: register_*_accessor decorators (pandas-dev#58339)
1 parent 3649db0 commit c79801c

File tree

6 files changed

+20
-8
lines changed

6 files changed

+20
-8
lines changed

.pre-commit-config.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ repos:
108108
types: [python]
109109
stages: [manual]
110110
additional_dependencies: &pyright_dependencies
111-
111+
112112
- id: pyright
113113
# note: assumes python env is setup and activated
114114
name: pyright reportGeneralTypeIssues

doc/source/whatsnew/v3.0.0.rst

+2
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ Optional libraries below the lowest tested version may still work, but are not c
142142
+------------------------+---------------------+
143143
| adbc-driver-postgresql | 0.10.0 |
144144
+------------------------+---------------------+
145+
| mypy (dev) | 1.9.0 |
146+
+------------------------+---------------------+
145147

146148
See :ref:`install.dependencies` and :ref:`install.optional_dependencies` for more.
147149

environment.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ dependencies:
7777

7878
# code checks
7979
- flake8=6.1.0 # run in subprocess over docstring examples
80-
- mypy=1.8.0 # pre-commit uses locally installed mypy
80+
- mypy=1.9.0 # pre-commit uses locally installed mypy
8181
- tokenize-rt # scripts/check_for_inconsistent_pandas_namespace.py
8282
- pre-commit>=3.6.0
8383

pandas/_typing.py

+1
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ def __reversed__(self) -> Iterator[_T_co]: ...
239239
# see https://mypy.readthedocs.io/en/stable/generics.html#declaring-decorators
240240
FuncType = Callable[..., Any]
241241
F = TypeVar("F", bound=FuncType)
242+
TypeT = TypeVar("TypeT", bound=type)
242243

243244
# types of vectorized key functions for DataFrame::sort_values and
244245
# DataFrame::sort_index, among others

pandas/core/accessor.py

+14-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from __future__ import annotations
99

1010
from typing import (
11+
TYPE_CHECKING,
1112
Callable,
1213
final,
1314
)
@@ -16,6 +17,12 @@
1617
from pandas.util._decorators import doc
1718
from pandas.util._exceptions import find_stack_level
1819

20+
if TYPE_CHECKING:
21+
from pandas._typing import TypeT
22+
23+
from pandas import Index
24+
from pandas.core.generic import NDFrame
25+
1926

2027
class DirNamesMixin:
2128
_accessors: set[str] = set()
@@ -232,7 +239,9 @@ def __get__(self, obj, cls):
232239

233240

234241
@doc(klass="", examples="", others="")
235-
def _register_accessor(name: str, cls):
242+
def _register_accessor(
243+
name: str, cls: type[NDFrame | Index]
244+
) -> Callable[[TypeT], TypeT]:
236245
"""
237246
Register a custom accessor on {klass} objects.
238247
@@ -277,7 +286,7 @@ def _register_accessor(name: str, cls):
277286
{examples}
278287
"""
279288

280-
def decorator(accessor):
289+
def decorator(accessor: TypeT) -> TypeT:
281290
if hasattr(cls, name):
282291
warnings.warn(
283292
f"registration of accessor {accessor!r} under name "
@@ -320,7 +329,7 @@ def decorator(accessor):
320329

321330

322331
@doc(_register_accessor, klass="DataFrame", examples=_register_df_examples)
323-
def register_dataframe_accessor(name: str):
332+
def register_dataframe_accessor(name: str) -> Callable[[TypeT], TypeT]:
324333
from pandas import DataFrame
325334

326335
return _register_accessor(name, DataFrame)
@@ -351,7 +360,7 @@ def register_dataframe_accessor(name: str):
351360

352361

353362
@doc(_register_accessor, klass="Series", examples=_register_series_examples)
354-
def register_series_accessor(name: str):
363+
def register_series_accessor(name: str) -> Callable[[TypeT], TypeT]:
355364
from pandas import Series
356365

357366
return _register_accessor(name, Series)
@@ -385,7 +394,7 @@ def register_series_accessor(name: str):
385394

386395

387396
@doc(_register_accessor, klass="Index", examples=_register_index_examples)
388-
def register_index_accessor(name: str):
397+
def register_index_accessor(name: str) -> Callable[[TypeT], TypeT]:
389398
from pandas import Index
390399

391400
return _register_accessor(name, Index)

requirements-dev.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ moto
5454
flask
5555
asv>=0.6.1
5656
flake8==6.1.0
57-
mypy==1.8.0
57+
mypy==1.9.0
5858
tokenize-rt
5959
pre-commit>=3.6.0
6060
gitpython

0 commit comments

Comments
 (0)