Skip to content

TYP: F used in decorators to _typing #33456

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 2 commits into from
Apr 10, 2020
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
18 changes: 3 additions & 15 deletions pandas/_config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -704,9 +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):
Expand Down
4 changes: 4 additions & 0 deletions pandas/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
4 changes: 3 additions & 1 deletion pandas/compat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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.
"""
Expand Down
21 changes: 4 additions & 17 deletions pandas/util/_decorators.py
Original file line number Diff line number Diff line change
@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

side issue i think we should isort so _typing imports right after typing



def deprecate(
Expand All @@ -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.

Expand Down Expand Up @@ -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.

Expand Down