Skip to content

Commit 12b0d45

Browse files
TYP: F used in decorators to _typing (#33456)
1 parent d72116b commit 12b0d45

File tree

4 files changed

+14
-33
lines changed

4 files changed

+14
-33
lines changed

pandas/_config/config.py

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,11 @@
5151
from collections import namedtuple
5252
from contextlib import contextmanager
5353
import re
54-
from typing import (
55-
Any,
56-
Callable,
57-
Dict,
58-
Iterable,
59-
List,
60-
Optional,
61-
Tuple,
62-
Type,
63-
TypeVar,
64-
cast,
65-
)
54+
from typing import Any, Callable, Dict, Iterable, List, Optional, Tuple, Type, cast
6655
import warnings
6756

57+
from pandas._typing import F
58+
6859
DeprecatedOption = namedtuple("DeprecatedOption", "key msg rkey removal_ver")
6960
RegisteredOption = namedtuple("RegisteredOption", "key defval doc validator cb")
7061

@@ -704,9 +695,6 @@ def pp(name: str, ks: Iterable[str]) -> List[str]:
704695
#
705696
# helpers
706697

707-
FuncType = Callable[..., Any]
708-
F = TypeVar("F", bound=FuncType)
709-
710698

711699
@contextmanager
712700
def config_prefix(prefix):

pandas/_typing.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,7 @@
7575

7676
# to maintain type information across generic functions and parametrization
7777
T = TypeVar("T")
78+
# used in decorators to preserve the signature of the function it decorates
79+
# see https://mypy.readthedocs.io/en/stable/generics.html#declaring-decorators
80+
FuncType = Callable[..., Any]
81+
F = TypeVar("F", bound=FuncType)

pandas/compat/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import sys
1313
import warnings
1414

15+
from pandas._typing import F
16+
1517
PY37 = sys.version_info >= (3, 7)
1618
PY38 = sys.version_info >= (3, 8)
1719
PYPY = platform.python_implementation() == "PyPy"
@@ -25,7 +27,7 @@
2527
# found at https://bitbucket.org/gutworth/six
2628

2729

28-
def set_function_name(f, name, cls):
30+
def set_function_name(f: F, name: str, cls) -> F:
2931
"""
3032
Bind the name/qualname attributes of the function.
3133
"""

pandas/util/_decorators.py

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,11 @@
11
from functools import wraps
22
import inspect
33
from textwrap import dedent
4-
from typing import (
5-
Any,
6-
Callable,
7-
List,
8-
Mapping,
9-
Optional,
10-
Tuple,
11-
Type,
12-
TypeVar,
13-
Union,
14-
cast,
15-
)
4+
from typing import Any, Callable, List, Mapping, Optional, Tuple, Type, Union, cast
165
import warnings
176

187
from pandas._libs.properties import cache_readonly # noqa
19-
20-
FuncType = Callable[..., Any]
21-
F = TypeVar("F", bound=FuncType)
8+
from pandas._typing import F
229

2310

2411
def deprecate(
@@ -29,7 +16,7 @@ def deprecate(
2916
klass: Optional[Type[Warning]] = None,
3017
stacklevel: int = 2,
3118
msg: Optional[str] = None,
32-
) -> Callable[..., Any]:
19+
) -> Callable[[F], F]:
3320
"""
3421
Return a new function that emits a deprecation warning on use.
3522
@@ -100,7 +87,7 @@ def deprecate_kwarg(
10087
new_arg_name: Optional[str],
10188
mapping: Optional[Union[Mapping[Any, Any], Callable[[Any], Any]]] = None,
10289
stacklevel: int = 2,
103-
) -> Callable[..., Any]:
90+
) -> Callable[[F], F]:
10491
"""
10592
Decorator to deprecate a keyword argument of a function.
10693

0 commit comments

Comments
 (0)