Syntactic sugar to encourage use of named arguments

I have implemented a proof-of-concept library auto-kwargs inspired by PEP 736. Since it is somewhat relevant but not directly related to the PEP, I thought I’d share it in this thread.

The key realization behind the library was that a function can enable the following usage through reflection:

from auto_kwargs import auto


def foo(bar, baz):
    return bar + baz


def qux():
    bar = 5
    baz = 7
    return foo(bar=auto(), baz=auto())


# Prints 12.
print(qux())

This way, the shortcut doesn’t require you to modify the interpreter or perform source-to-source compilation.

Now, I am not sure this is more readable! There are also significant downsides:

  1. Performance is poor (see the benchmark).
  2. It interferes with type checking.

All of these factors combined mean I won’t continue working on it. I am sharing this experiment in case others are interested. A sufficiently interested person could perhaps address the performance with a native extension and type checking with a mypy plugin.

2 Likes