From 9c7b79c5fe3f6908f675f2add8e83f81ebc98d20 Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Fri, 10 Apr 2020 15:10:16 +0100 Subject: [PATCH 1/2] TYP: F used in decorators to _typing --- pandas/_config/config.py | 19 +++---------------- pandas/_typing.py | 4 ++++ pandas/compat/__init__.py | 4 +++- pandas/util/_decorators.py | 21 ++++----------------- 4 files changed, 14 insertions(+), 34 deletions(-) diff --git a/pandas/_config/config.py b/pandas/_config/config.py index df706bf25097e..6503c55545c9c 100644 --- a/pandas/_config/config.py +++ b/pandas/_config/config.py @@ -51,20 +51,11 @@ from collections import namedtuple from contextlib import contextmanager import re -from typing import ( - Any, - Callable, - Dict, - Iterable, - List, - Optional, - Tuple, - Type, - TypeVar, - cast, -) +from typing import Any, Callable, Dict, Iterable, List, Optional, Tuple, Type, cast import warnings +from pandas._typing import F + DeprecatedOption = namedtuple("DeprecatedOption", "key msg rkey removal_ver") RegisteredOption = namedtuple("RegisteredOption", "key defval doc validator cb") @@ -704,10 +695,6 @@ def pp(name: str, ks: Iterable[str]) -> List[str]: # # helpers -FuncType = Callable[..., Any] -F = TypeVar("F", bound=FuncType) - - @contextmanager def config_prefix(prefix): """ diff --git a/pandas/_typing.py b/pandas/_typing.py index e1b6a5e2e6876..850f10bd7f811 100644 --- a/pandas/_typing.py +++ b/pandas/_typing.py @@ -75,3 +75,7 @@ # to maintain type information across generic functions and parametrization T = TypeVar("T") +# used in decorators to preserve the signature of the function it decorates +# see https://mypy.readthedocs.io/en/stable/generics.html#declaring-decorators +FuncType = Callable[..., Any] +F = TypeVar("F", bound=FuncType) diff --git a/pandas/compat/__init__.py b/pandas/compat/__init__.py index 3547a33ea357b..6570e0782a69a 100644 --- a/pandas/compat/__init__.py +++ b/pandas/compat/__init__.py @@ -12,6 +12,8 @@ import sys import warnings +from pandas._typing import F + PY37 = sys.version_info >= (3, 7) PY38 = sys.version_info >= (3, 8) PYPY = platform.python_implementation() == "PyPy" @@ -25,7 +27,7 @@ # found at https://bitbucket.org/gutworth/six -def set_function_name(f, name, cls): +def set_function_name(f: F, name: str, cls) -> F: """ Bind the name/qualname attributes of the function. """ diff --git a/pandas/util/_decorators.py b/pandas/util/_decorators.py index 71d02db10c7ba..17815c437249b 100644 --- a/pandas/util/_decorators.py +++ b/pandas/util/_decorators.py @@ -1,24 +1,11 @@ from functools import wraps import inspect from textwrap import dedent -from typing import ( - Any, - Callable, - List, - Mapping, - Optional, - Tuple, - Type, - TypeVar, - Union, - cast, -) +from typing import Any, Callable, List, Mapping, Optional, Tuple, Type, Union, cast import warnings from pandas._libs.properties import cache_readonly # noqa - -FuncType = Callable[..., Any] -F = TypeVar("F", bound=FuncType) +from pandas._typing import F def deprecate( @@ -29,7 +16,7 @@ def deprecate( klass: Optional[Type[Warning]] = None, stacklevel: int = 2, msg: Optional[str] = None, -) -> Callable[..., Any]: +) -> Callable[[F], F]: """ Return a new function that emits a deprecation warning on use. @@ -100,7 +87,7 @@ def deprecate_kwarg( new_arg_name: Optional[str], mapping: Optional[Union[Mapping[Any, Any], Callable[[Any], Any]]] = None, stacklevel: int = 2, -) -> Callable[..., Any]: +) -> Callable[[F], F]: """ Decorator to deprecate a keyword argument of a function. From 1ecaf9f0a382f151ca84f080e2d2e72e1720e097 Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Fri, 10 Apr 2020 15:51:32 +0100 Subject: [PATCH 2/2] black fixup --- pandas/_config/config.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/_config/config.py b/pandas/_config/config.py index 6503c55545c9c..8955a06187109 100644 --- a/pandas/_config/config.py +++ b/pandas/_config/config.py @@ -695,6 +695,7 @@ def pp(name: str, ks: Iterable[str]) -> List[str]: # # helpers + @contextmanager def config_prefix(prefix): """