This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Incorrect error message: "missing 1 required positional argument"
Type: enhancement Stage:
Components: Interpreter Core Versions: Python 3.11
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: AlexWaygood, Anders.Hovmöller, aroberge, eric.smith, terry.reedy
Priority: normal Keywords:

Created on 2022-02-04 15:54 by Anders.Hovmöller, last changed 2022-04-11 14:59 by admin.

Messages (6)
msg412510 - (view) Author: Anders Hovmöller (Anders.Hovmöller) * Date: 2022-02-04 15:54
>>> def foo(a):
...     pass
... 
>>> foo()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: foo() missing 1 required positional argument: 'a'

This error is incorrect. It says "positional argument", but it's just "argument". The proof is that if you call it with

foo(a=3)

it works fine.
msg412511 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2022-02-04 16:07
I guess the technically correct term is positional-or-keyword, but that seems like too much: https://docs.python.org/3/glossary.html

I think dropping "positional" doesn't increase the precision of the error message, but I'll admit I can't think of anything better. And lacking something better I don't think we should change this.
msg412514 - (view) Author: Anders Hovmöller (Anders.Hovmöller) * Date: 2022-02-04 16:23
Just dropping the word "positional" is very good. That word is a lie, and just removing it makes it true.
msg412519 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2022-02-04 17:05
Given this current behavior:

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

What would you suggest?

I agree the current messages aren't perfect.
msg412520 - (view) Author: Anders Hovmöller (Anders.Hovmöller) * Date: 2022-02-04 17:13
For `foo(a, /, b)`, it could be:

"TypeError: foo() missing 1 required argument 'a', and one required positional argument 'b'.

If we start on this road there are some more, like for `def foo(a, *, b)` you get the error "TypeError: foo() missing 1 required positional argument: 'a'" which leaves out that the keyword only argument is also required. 

Another solution would be something like:

TypeError: foo() missing 3 required arguments: 'a' (positional only), 'b', 'c' (keyword only)

This solution scales to the worst complex cases, and is a lot clearer imo. Could even be further improved with some nicer formatting:

TypeError: foo() missing 3 required arguments: 
    'a' (positional only)
    'b'
    'c' (keyword only)

But that might be a bit overkill :)
msg412530 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2022-02-04 20:15
I think the incorrect qualification should be dropped, if sanely feasible.

More misleading are hidden positional-only args.  Doc:
  min(iterable, *[, key, default])
  min(arg1, arg2, *args[, key])

>>> min(iterator=(1,3))
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    min(iterator=(1,3))
TypeError: min expected at least 1 argument, got 0

1 was passed, but 0 were gotten, as the 1 passed was silently ignored.
History
Date User Action Args
2022-04-11 14:59:55adminsetgithub: 90795
2022-02-05 00:40:55AlexWaygoodsetnosy: + AlexWaygood
2022-02-04 20:15:07terry.reedysetnosy: + terry.reedy

messages: + msg412530
versions: + Python 3.11, - Python 3.7, Python 3.8, Python 3.9
2022-02-04 19:17:54arobergesetnosy: + aroberge
2022-02-04 17:13:07Anders.Hovmöllersetmessages: + msg412520
2022-02-04 17:05:31eric.smithsetmessages: + msg412519
2022-02-04 16:23:34Anders.Hovmöllersetmessages: + msg412514
2022-02-04 16:07:49eric.smithsetnosy: + eric.smith
messages: + msg412511
2022-02-04 15:54:15Anders.Hovmöllercreate