16

There's close to 5000 questions containing "TypeError: missing required positional argument". These questions mainly concern Python.

I'm thinking that we need a canonical for these questions, to effectively handle them, similar to What is a NullPointerException, and how do I fix it?.

I began writing a community wiki question for use as a target duplicate, when I decided I'd be better off asking Meta for input beforehand.

What is a parameter TypeError, and how to debug it?

A common error in Python arises when a function is passed a set of arguments (or lack thereof), that are invalid with the function signature:

TypeError: () missing required positional argument(s): <>...

In which situations do these occur, and how are they best debugged and fixed?

I find this to be a very specific question that seems rather easy to debug. I'm not sure if it's a great fit for SO, but it would help with handling these questions. Maybe there's an existing canonical that my search terms didn't provide me with?

There was recently a question on Meta about a question specifically about this issue: Was deleting this question the right choice?.

6
  • 3
    I think some people in the python-canonical room planned/discussed to do that (at least I recall Karl did): chat.stackoverflow.com/rooms/247434/python-canon-discussion Either way, it could be a joined effort if everyone involved is interested enough. Commented Apr 23, 2023 at 18:56
  • 2
    @NordineLotfi Thanks. Karl linked to this question, where the error message reads a bit differently. Useful for catching search terms. Commented Apr 23, 2023 at 19:01
  • 1
    Yeah, now that I recall, I think there was this canonical that Karl made: stackoverflow.com/questions/73631401/… not sure if it would work as duplicate target for all of the result on the query you linked in your post, but it might be a good basis at least. Commented Apr 23, 2023 at 19:04
  • 3
    @NordineLotfi Great! Then this Meta question is resolved. I think the SO question needs some editing. It only has 3000 views after 7 months, which I assume isn't a lot, as this error is quite frequently occurring. It also never popped up when I searched for it. Commented Apr 23, 2023 at 19:07
  • 3
    That question is sooooo broad. It talks about all TypeErrors. But even the error you mention in itself is very broad for a canonical - it can happen from many different causes. There is this one that is focused on the common problem of getting this error when calling an instance method on the class, instead of on an instance.
    – Tomerikoo
    Commented Apr 24, 2023 at 13:28
  • 1
    I stand half corrected - that canonical by Karl actually breaks down the errors into groups and the first one is exactly the errors related to missing arguments so it can be a good duplicate target. The problem, mentioned in comments to your answer below, is that most readers will probably not bother to read all that (great) explanation. They want a fast correction to apply to their code and move on... But I guess that's just their problem
    – Tomerikoo
    Commented Apr 24, 2023 at 13:34

3 Answers 3

11

An existing canonical was created by Karl Knechtel 7 months ago. Thanks to Nordine Lotfi for finding it.

As such, this question is resolved.

5
  • 2
    The canonical suffers from the common issue with canonicals that it is very long and complicated, though. The actual issue of required arguments is described very briefly in two sentences, buried in a 3600 word (!) Q&A, and is followed by a longish discussion of why this is a TypeError and not a ValueError and the C++ heritage thereof, something that questioners likely are not so interested in. I would be much more in favor of creating/adapting a narrow focus canonical just for the issue of a mismatched number of arguments.
    – Marijn
    Commented Apr 24, 2023 at 9:05
  • @Marijn Perhaps you can add another answer to the canonical one? If not, your comment is likely best suited for an answer to this question, here on Meta. Commented Apr 24, 2023 at 9:33
  • I agree with Andreas. I think it would be beneficial if you added a more focused answer to the canonical. Also, I don't think it is possible to both make this canonical and not complicated. Based on the fact it is a very broad subject (eg: TypeError, etc), it is only normal that it has so many details. The more, the merrier. @Marijn Commented Apr 24, 2023 at 11:11
  • 5
    @NordineLotfi I actually think the topic of all TypeErrors is too big for a canonical and it should be split in several smaller canonicals. Adding a focused answer would help a bit, but if you close a question as a duplicate of the canonical then still the questioner needs to look for that specific answer on a very long page with other irrelevant (for them) information, or when closing a question you need to provide a separate comment with a link to the specific answer. Both outcomes are not ideal.
    – Marijn
    Commented Apr 24, 2023 at 13:34
  • 2
    @Marijn I agree, basically. I added an answer here to give my perspective on the Q&A, as its author. The short version is that it is only marginally intended as a "canonical" in the first place. I wrote it somewhere near the same time as stackoverflow.com/questions/72495496 (which was poorly received). Since then I have changed my outlook on artificial canonicals somewhat. Commented Apr 24, 2023 at 21:23
6

I was away for a few days and missed the discussion.

I don't really intend for my canonical to be used to close every possible TypeError question. The purpose is primarily to hold reference links, but also to give an immediate target to close zero-effort debugging questions and shut down FGITW, until a more specific dupe can be found.

Over and above that, however, the "canonical" is written as a reference Q&A that can be looked up and appreciated on its own. I wrote it to be parallel to I'm getting an IndentationError. How do I fix it? ; as such, I put a lot of effort into discussing technique for diagnosing the issue and gaining an understanding of the problem, rather than simply explaining specific issues/problem instances.

I don't think that the error message described here can necessarily be served by a single canonical, either, because it can have multiple causes. A good dupe closure will generally choose a target that is appropriate to the apparent cause that OP asked about.

My 3600 word (thanks for counting I guess) essay includes these two links:

"TypeError: method() takes 1 positional argument but 2 were given" but I only passed one

TypeError in Threading. function takes x positional argument but y were given

Putting them side by side hopefully illustrates the problem - these OPs had the "same problem" created in very different ways, and the second one was effectively a typo that I ended up closing as a duplicate of something completely different (i.e., OP had the "wrong number of arguments" due to using incorrect syntax to describe the arguments, rather than because of a misunderstanding about how many arguments there should be).

I would be happy, however, to see that section titled "Error messages about wrong arguments" reworked into a better canonical. The problem is figuring out the appropriate level of generalization. "The error message says the number of arguments is wrong; why?" might be an appropriate level.

1
  • Thanks for the explanation, I didn't consider the use as immediate but temporary close target but that is actually very useful I think. Also I do appreciate the question as a reference Q&A, it is very thorough and readable - just a bit long :)
    – Marijn
    Commented Apr 25, 2023 at 5:38
3

This answer combines and expands on my comments below Andreas' answer.

While there is an existing canonical that covers TypeErrors related to the number of arguments of a function or class, it suffers from the common issue with canonicals that it is very long and complicated. The actual issue of required arguments is described very briefly in two sentences in the first answer, buried in a 3600 word (!) Q&A, and is followed by a longish discussion of why this is a TypeError and not a ValueError and the C++ heritage thereof, something that questioners likely are not so interested in. To find the solution you need to also check the second long answer, where again somewhere in the middle it is explained how to fix the error.

I actually think the topic of all TypeErrors is too big for a canonical and it should be split in several smaller canonicals. Adding a focused answer to the existing canonical (as suggested by Andreas and Nordine Lotfi) would help a bit, but if you close a question as a duplicate of the canonical then still the questioner needs to look for that specific answer on a very long page with other irrelevant (for them) information, or when closing a question you need to provide a separate comment with a link to the specific answer. Both outcomes are not ideal, the first because the asker needs to find their specific answer in a wall of text and code, and the second because it requires extra effort when voting to close a question.

When creating separate canonicals I think that even the topic of the number of arguments can be split in smaller units, for example one canonical for argument-related TypeErrors when calling class functions (that have a hidden self argument) and another one for regular functions.

The topic of canonicals being too broad has been discussed previously over the years. A more extensive version of the points raised above is https://meta.stackoverflow.com/a/406365/. Also Is there a canonical duplicate for "how do I fix my ArgumentNullException" questions? and https://meta.stackoverflow.com/a/315675/ provide interesting examples and arguments for keeping canonicals focussed.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .