Skip to content

TYP: register_*_accessor decorators #58339

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

Merged
merged 1 commit into from
Apr 20, 2024
Merged
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
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ repos:
types: [python]
stages: [manual]
additional_dependencies: &pyright_dependencies
- [email protected].351
- [email protected].352
- id: pyright
# note: assumes python env is setup and activated
name: pyright reportGeneralTypeIssues
Expand Down
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ Optional libraries below the lowest tested version may still work, but are not c
+------------------------+---------------------+
| adbc-driver-postgresql | 0.10.0 |
+------------------------+---------------------+
| mypy (dev) | 1.9.0 |
+------------------------+---------------------+

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

Expand Down
2 changes: 1 addition & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ dependencies:

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

Expand Down
1 change: 1 addition & 0 deletions pandas/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ def __reversed__(self) -> Iterator[_T_co]: ...
# see https://mypy.readthedocs.io/en/stable/generics.html#declaring-decorators
FuncType = Callable[..., Any]
F = TypeVar("F", bound=FuncType)
TypeT = TypeVar("TypeT", bound=type)

# types of vectorized key functions for DataFrame::sort_values and
# DataFrame::sort_index, among others
Expand Down
19 changes: 14 additions & 5 deletions pandas/core/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from __future__ import annotations

from typing import (
TYPE_CHECKING,
Callable,
final,
)
Expand All @@ -16,6 +17,12 @@
from pandas.util._decorators import doc
from pandas.util._exceptions import find_stack_level

if TYPE_CHECKING:
from pandas._typing import TypeT

from pandas import Index
from pandas.core.generic import NDFrame


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


@doc(klass="", examples="", others="")
def _register_accessor(name: str, cls):
def _register_accessor(
name: str, cls: type[NDFrame | Index]
) -> Callable[[TypeT], TypeT]:
"""
Register a custom accessor on {klass} objects.

Expand Down Expand Up @@ -277,7 +286,7 @@ def _register_accessor(name: str, cls):
{examples}
"""

def decorator(accessor):
def decorator(accessor: TypeT) -> TypeT:
if hasattr(cls, name):
warnings.warn(
f"registration of accessor {accessor!r} under name "
Expand Down Expand Up @@ -320,7 +329,7 @@ def decorator(accessor):


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

return _register_accessor(name, DataFrame)
Expand Down Expand Up @@ -351,7 +360,7 @@ def register_dataframe_accessor(name: str):


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

return _register_accessor(name, Series)
Expand Down Expand Up @@ -385,7 +394,7 @@ def register_series_accessor(name: str):


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

return _register_accessor(name, Index)
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ moto
flask
asv>=0.6.1
flake8==6.1.0
mypy==1.8.0
mypy==1.9.0
tokenize-rt
pre-commit>=3.6.0
gitpython
Expand Down
Loading