question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RFC deprecate positional arguments

See original GitHub issue

I had a quick discussion with @NicolasHug following the discussion of keyword only arguments on the mailing list.

That should be possible to deprecate the use of positional arguments via a decorator, so that

def myfunction(a=1, b=2, c=3):
    return a + b + c

will become

def myfunction(*, a=1, b=2, c=3):
    return a + b + c

but will not break, but raise a deprecation warning if used with positional arguments.

Might look like:

from warnings import warn
from functools import wraps

def warn_args_wrapper(f):
    @wraps(f)
    def new_f(*args, **kwargs):
        if args:
            warn(DeprecationWarning("got args {}, you should use keyword args!".format(args)))
        return f(*args, **kwargs)
    return new_f

@warn_args_wrapper
def f(a=1, b=2, c=3):
    return a + b + c

We would need to put that decorator around all __init__ definitions (at least). It’s a big magic and I’m not sure how well it plays with sphinx.

This RFC is to float the idea and discuss whether it’s worth writing a SLEP / doing a prototype.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
thomasjpfancommented, Feb 26, 2019

Here is a rough implementation:

import inspect

def warn_args_wrapper(f):
    argspec = inspect.getfullargspec(f)
    kwonlyargs = argspec.kwonlyargs
    orig_spec = argspec.args
    
    # For class methods
    has_self = 'self' in orig_spec
    
    @wraps(f)
    def inner_f(*args, **kwargs):
        extra_args = len(args) - len(orig_spec)
        if extra_args > 0:
            err_args = args[1:] if has_self else args
            warn("Got arguments, {}, should use keyword args for {}".format(
                 err_args, kwonlyargs[:extra_args]),
                 DeprecationWarning)
        kwargs.update({k: arg for k, arg in zip(orig_spec, args)})
        return f(**kwargs)
    return inner_f

For functions:

@warn_args_wrapper
def hello(a, b=3, *, h=4, j=6):
    return a + b + h + j

Calling hello(1, 3, 5) will result with a warning:

Got arguments, (1, 3, 5), should use keyword args for ['h']

For class methods:

class Hello:
    
    @warn_args_wrapper
    def __init__(self, penalty='l2', *, dual=False):

        self.penalty = penalty
        self.dual = dual

Running Hello('l2', True) will result in

DeprecationWarning: Got arguments, ('l2', True), should use keyword args for ['dual']

The warning can be adjusted.

0reactions
amuellercommented, Jun 15, 2020

closed with SLEP 009

Read more comments on GitHub >

github_iconTop Results From Across the Web

Deprecating Site Local Addresses RFC 3879 - IETF Datatracker
Huitema & Carpenter Standards Track [Page 1] RFC 3879 Deprecating Site Local ... The previous section reviewed the arguments against site-local addresses.
Read more >
[Pre-RFC] Keyword arguments - ideas (deprecated)
The design with keyword arguments doesn't have this problem: you can execute the function when the last positional argument is given and ...
Read more >
rfc:deprecations_php_7_2 - PHP
This is a draft RFC for multiple deprecations targeting PHP 7.2. The RFC proposes to deprecate the listed functionality in PHP 7.2 and ......
Read more >
EmberJS on Twitter: "Deprecate Array Observers Ember's Array ...
Today the Ember Framework team moved 5 RFCs into final comment period. ... Deprecate `` Component Positional Arguments by chancancode · Pull Request...
Read more >
tools — Miscellaneous Helper Functions — Pywikibot 7.7.0 ...
A wrapper for a module to deprecate classes or variables of it. Initialise the wrapper. ... The positional arguments are the dictionaries to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found