Named arguments confer many benefits by promoting explicit is better than implicit, thus increasing readability and minimising the risk of inadvertent transposition. However, the syntax can become needlessly repetitive and verbose.
The verbosity and redundancy discourages use of named arguments (and therefore, their benefits) and reduces readability by increasing noise. I have personally encountered this many times.
Proposition
I would like to propose a simple syntactic sugar in the case that the name of the variable provided as the argument value is the same as the name of the argument itself.
Encourages use of named variables, thereby increasing readability (explicit is better than implicit) and reducing bugs from argument transposition
Reduces verbosity (readability counts)
Encourages authors to use the same variable name when calling a function as the argument (increases consistency of variable names used, thereby increasing readability)
Reminiscent of python’s long-standing *args and **kwargs syntax (established pythonic style)
Reminiscent of python’s existing f-string debug f'{var=}' syntax (established pythonic style) and with a very similar function
This should be relatively easy to implement as it is simple syntactic sugar (if the implementation is easy to explain, it may be a good idea)
Backwards compatibility: this change is fully backwards compatible since the proposed syntax would raise a syntax error in current python versions
I think that having the equal sign at the end of the variable func(a=) may ease the implementation and be more intuitive because you already have a similar syntax for f strings: f"{x=}"
I don’t know what the SC will think of this proposal, and I don’t have time to do a lot of mentoring in this area, but if a few folks here will come together and submit a PEP (following the process from PEP 1) and you need a sponsor you can put my name in. I personally prefer the syntax foo(x=, y=).
I think the arguments presented this time around are the most motivating (at least to me). In particular, I like this one: “Encourages authors to use the same variable name when calling a function as the argument”. That’s something I will intentionally fix if argument names drift apart. In my opinion, it is beneficial to have syntax that induces coders to do this.
I don’t think this argument was suggested the last couple times this was proposed.
Can the PEP have a recommendation for static analysis tools (like mypy) catch if this is used but the corresponding variable of the same name doesn’t exist?
… though I suspect that this is nothing more than a plain vanilla NameError. Am I understanding you correctly, and you mean code like this?
def spam():
x = 1234 # no y
func(x=x, y=y)
func(x=, y=) # or func(=x, =y) if that's the chosen syntax
If so, it should be no different from the longhand line above it, or any other attempt to reference a variable that doesn’t exist. Obviously static analysis tools will have to be taught about the new syntax, but that’s the case for all syntax.
Or were there other recommendations you had in mind?
Yeah, there’s definitely value in catching this sort of thing statically. I’ve no idea which checkers report on this (I gave MyPy a quick whirl and it didn’t report it, but that’s with default settings so maybe that can be turned on), but regardless, it should be able to be reported on regardless of this particular piece of syntax.
Mypy can do that check, but it’s disabled by default. Probably because it’s rather tricky to catch all the situations correctly, so it’s likely that you’ll get false positives.
I think I prefer param= over the equals sign on the other side. My editor (vscode) already would be happy to autocomplete this since it lets me tab complete var=
Like in a day at work, I already saw multiple times that I would use this functionality.