-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
API: consider better decorator option #13875
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
Comments
it should propagate I think it just needs wraps |
actually @chris-b1 where is this from? |
actually read that article. Ok that is nice. |
I'm assuming you're looking on python3? In python 2 the standard In [1]: import functools
In [2]: def dec_stdlib(f):
...: @functools.wraps(f)
...: def inner(*args, **kwargs):
...: f(*args, **kwargs)
...: return inner
In [6]: import boltons.funcutils
In [7]: def dec_boltons(f):
...: @boltons.funcutils.wraps(f)
...: def inner(*args, **kwargs):
...: f(*args, **kwargs)
...: return inner
In [4]: @dec_stdlib
...: def f(a, b):
...: return a + b
In [5]: f?
Signature: f(*args, **kwargs)
Docstring: <no docstring>
Type: function
In [8]: @dec_boltons
...: def f(a, b):
...: return a + b
In [9]: f?
Signature: f(a, b)
Type: function |
I attempted this using https://github.com/mahmoud/boltons/blob/master/boltons/funcutils.py - which has a |
I've looked into this a bit using python 3.7 and the For the record, giving the drawbacks to wrapt and decorators.py, I like vendoring boltons.functools.wraps the best (assuming it can be pulled out painlessly - haven't checked that). |
@chris-b1 since we're now py35+ is this closeable? |
I would think so! Although I am not following @topper-123 comment above on |
My point was that
OTOH, if mypy accepts the "lie", all the better. Does anyone know? (PyCharm does not). To demonstrate the difference between a decorated and non-decorated function: >>> from inspect import getfullargspec
>>> from functools import wraps
>>> def my_func(a:int, b: int = 1) -> int:
...: return a + b
>>> def deco(func):
... @functools.wraps(func)
... def inner_func(*args, **kwargs):
... return func(*args, **kwargs)
... return inner_func
>>> getfullargspec(my_func)
FullArgSpec(args=['a', 'b'], varargs=None, varkw=None, defaults=(1,), kwonlyargs=[], kwonlydefaults=None, annotations={'return': <class 'pandas.core.series.Series'>, 'a': <class 'pandas.core.series.Series'>, 'b': <class 'int'>})
>>> getfullargspec(deco(my_func))
FullArgSpec(args=[], varargs='args', varkw='kwargs', defaults=None, kwonlyargs=[], kwonlydefaults=None, annotations={'return': <class 'pandas.core.series.Series'>}) Notice the output is quite different. inspect.signature gives the same output for both decorared and non-decorated, but that's not sufficient for at least PyCharm to treat a decorated function as the same as the non-decorated. |
@topper-123 now that we're on the verge of dropping py35, does that affect whether this is closeable and/or actionable? |
@WillAyd there was another decorator-related issue that just got closed. can this one go too? |
I think this works on master? In [3]: inspect.getfullargspec(pd.to_datetime)
Out[3]: FullArgSpec(args=['arg', 'errors', 'dayfirst', 'yearfirst', 'utc', 'format', 'exact', 'unit', 'infer_datetime_format', 'origin', 'cache'], varargs=None, varkw=None, defaults=('raise', False, False, None, None, True, None, False, 'unix', True), kwonlyargs=[], kwonlydefaults=None, annotations={'return': typing.Union[pandas.core.indexes.datetimes.DatetimeIndex, ForwardRef('Series'), ~DatetimeScalar, ForwardRef('NaTType')], 'arg': typing.Union[~DatetimeScalar, typing.List, typing.Tuple, ~ArrayLike, ForwardRef('Series')], 'errors': <class 'str'>, 'dayfirst': <class 'bool'>, 'yearfirst': <class 'bool'>, 'utc': typing.Union[bool, NoneType], 'format': typing.Union[str, NoneType], 'exact': <class 'bool'>, 'unit': typing.Union[str, NoneType], 'infer_datetime_format': <class 'bool'>, 'cache': <class 'bool'>}) So sure let's close - can always reopen |
It's not an issue in py3.5, but in older versions this is annoying - because a decorator is used in the definition, the function signature on

to_datetime
is lost. This is the big one I've run into, but there may be some other places in the API it comes up.I saw this blog today - could use one of the strategies there. It may make sense to vendor rather than adding another dependency?
https://hynek.me/articles/decorators/
The text was updated successfully, but these errors were encountered: