Skip to content

Added annotations to util._decorators #27393

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
Jul 15, 2019
Merged
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
46 changes: 24 additions & 22 deletions pandas/util/_decorators.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
from functools import wraps
import inspect
from textwrap import dedent
from typing import Any, Callable, Dict, List, Optional, Tuple, Type, Union
import warnings

from pandas._libs.properties import cache_readonly # noqa


def deprecate(
name, alternative, version, alt_name=None, klass=None, stacklevel=2, msg=None
):
name: str,
alternative: Callable,
version: str,
alt_name: Optional[str] = None,
klass: Optional[Type[Warning]] = None,
stacklevel: int = 2,
msg: Optional[str] = None,
) -> Callable:
"""
Return a new function that emits a deprecation warning on use.

Expand Down Expand Up @@ -80,7 +87,12 @@ def wrapper(*args, **kwargs):
return wrapper


def deprecate_kwarg(old_arg_name, new_arg_name, mapping=None, stacklevel=2):
def deprecate_kwarg(
old_arg_name: str,
new_arg_name: Optional[str],
mapping: Optional[Union[Dict, Callable[[Any], Any]]] = None,
stacklevel: int = 2,
) -> Callable:
"""
Decorator to deprecate a keyword argument of a function.

Expand Down Expand Up @@ -200,7 +212,9 @@ def wrapper(*args, **kwargs):
return _deprecate_kwarg


def rewrite_axis_style_signature(name, extra_params):
def rewrite_axis_style_signature(
name: str, extra_params: List[Tuple[str, Any]]
) -> Callable:
def decorate(func):
@wraps(func)
def wrapper(*args, **kwargs):
Expand Down Expand Up @@ -265,11 +279,11 @@ def __init__(self, *args, **kwargs):

self.params = args or kwargs

def __call__(self, func):
def __call__(self, func: Callable) -> Callable:
func.__doc__ = func.__doc__ and func.__doc__ % self.params
return func

def update(self, *args, **kwargs):
def update(self, *args, **kwargs) -> None:
"""
Update self.params with supplied args.

Expand All @@ -278,18 +292,6 @@ def update(self, *args, **kwargs):

self.params.update(*args, **kwargs)

@classmethod
def from_params(cls, params):
"""
In the case where the params is a mutable sequence (list or dictionary)
and it may change before this class is called, one may explicitly use a
reference to the params rather than using *args or **kwargs which will
copy the values and not reference them.
"""
result = cls()
result.params = params
return result


class Appender:
"""
Expand All @@ -311,22 +313,22 @@ def my_dog(has='fleas'):
pass
"""

def __init__(self, addendum, join="", indents=0):
def __init__(self, addendum: Optional[str], join: str = "", indents: int = 0):
if indents > 0:
self.addendum = indent(addendum, indents=indents)
self.addendum = indent(addendum, indents=indents) # type: Optional[str]
else:
self.addendum = addendum
self.join = join

def __call__(self, func):
def __call__(self, func: Callable) -> Callable:
func.__doc__ = func.__doc__ if func.__doc__ else ""
self.addendum = self.addendum if self.addendum else ""
docitems = [func.__doc__, self.addendum]
func.__doc__ = dedent(self.join.join(docitems))
return func


def indent(text, indents=1):
def indent(text: Optional[str], indents: int = 1) -> str:
if not text or not isinstance(text, str):
return ""
jointext = "".join(["\n"] + [" "] * indents)
Expand Down