Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #83612

Re: class-based class decorator

Date 2015-01-12 16:20 +0100
From Jean-Michel Pichavant <jeanmichel@sequans.com>
Subject Re: class-based class decorator
Newsgroups comp.lang.python
Message-ID <mailman.17625.1421076121.18130.python-list@python.org> (permalink)

Show all headers | View raw


----- Original Message -----
> From: "Albert-Jan Roskam" <fomcl@yahoo.com.dmarc.invalid>
> import functools
> import inspect
> import warnings
> 
> warnings.simplefilter("always")
> 
> class check_deprecated_args(object):
> 
>     def __init__(self, deprecated_params, msg=None):
>         self.deprecated_params = deprecated_params
>         self.msg = msg
> 
>     def __call__(self, func):
>         @functools.wraps(func)
>         def inner(*args, **kwargs):
>             argspec = inspect.getargspec(func)
>             default_signature = dict(zip(argspec.args[1:],
>             argspec.defaults))
>             callargs = inspect.getcallargs(func, *args, **kwargs)
>             deprecated_calls = [(p, a) for p, a in callargs.items()
>             if
>                                  p in self.deprecated_params and
>                                  a != default_signature[p]]
>             for (param, arg) in deprecated_calls:
>                 msg = "you're using obsolete parameters in %s:
>                 [%s:%s]"
>                 msg = msg % (func.__name__, param, arg)
>                 msg = msg + " " + self.msg if self.msg else msg
>                 warnings.warn(msg, DeprecationWarning, stacklevel=2)
>             return func(*args, **kwargs)
>         functools.update_wrapper(inner, func)
>         return inner
> 
> if __name__ == "__main__":
>     class Foo(object):
> 
>         @check_deprecated_args(["old", "older"], "use 'brand_new'
>         param instead")
>         def __init__(self, old="old", older="ancient"):
>             print "hello"
> 
>         @check_deprecated_args(deprecated_params=["old", "older"])
>         def bar(self, old="default"):
>             print "world"
> 
>     f = Foo(old="old", older="dino era")
>     f.bar("gnarly")
> 
>     help(f)  # now the signature is *args, **kwargs, which makes my
>     Sphinx documentation less readable!
> 
> Best wishes,
> Albert-Jan

I don't really understand how you successfuly manage positional parameters, since the caller may not name them.
I'm asking because if your intend to check only the keyword parameters, there's a much simplier solution.

JM


-- IMPORTANT NOTICE: 

The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: class-based class decorator Jean-Michel Pichavant <jeanmichel@sequans.com> - 2015-01-12 16:20 +0100

csiph-web