Skip to content

RFC deprecate positional arguments #12805

@amueller

Description

@amueller

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions