[Python-Dev] TypeError: f() missing 1 required positional argument: 'x'

Ethan Furman ethan at stoneleaf.us
Thu Sep 20 21:45:25 CEST 2012


Steven D'Aprano wrote:
> On 20/09/12 22:59, Mark Dickinson wrote:
>> On Thu, Sep 20, 2012 at 1:21 PM, Nick Coghlan<ncoghlan at gmail.com>  wrote:
>>> +1 for using the unqualified "argument" in these error messages to
>>> mean "positional or keyword argument" (inspect.Parameter spells it out
>>> as POSITIONAL_OR_KEYWORD, but the full phrase is far too verbose for
>>> an error message).
>>
>> Ah yes;  I see that 'positional or keyword' is a more accurate term
>> (but agree it's unwieldy for an error message).  I also see that I was
>> naive to think that the 'fix' is as simple as dropping the word
>> 'positional':
>>
>>      >>>  def f(a, *, b):
>>      ...     pass
>>      ...
>>      >>>  f()
>>      Traceback (most recent call last):
>>        File "<stdin>", line 1, in<module>
>>      TypeError: f() missing 1 required positional argument: 'a'
>>
>> If the word 'positional' were dropped here, it would give the
>> incorrect impression that f only requires one argument.
> 
> I don't expect error messages to give a complete catalog of every
> problem with a specific function call. If f() reports that required
> argument 'a' is missing, that does not imply that no other required
> arguments are also missing. I think it is perfectly acceptable to
> not report the missing 'b' until the missing 'a' is resolved.

I disagree.  There is no reason (that I'm aware of ;) that the missing 
'b' cannot be noticed and reported at the same time as the missing 'a'.


> But I do expect error messages to be accurate. +1 to remove the
> word "positional" from the message.

And then it's still not accurate as 'b' is also a required argument that 
is missing.  Unless and until all error messages adopt your proposed 
'positional argument', 'argument', 'keyword argument' *and* describe 
_all_ the problems with the call confusion will reign supreme.

So, ideally, the above example would be:

      >>>  def f(a, *, b):
      ...     pass
      ...
      >>>  f()
      Traceback (most recent call last):
        File "<stdin>", line 1, in<module>
      TypeError: f() missing 2 required arguments: positional: 'a', 
keyword: 'b'

~Ethan~

P.S.
Also, a big thank-you -- the error messages *are* getting better all the 
time!


More information about the Python-Dev mailing list