-1
import argparse
parser = argparse.ArgumentParser
parser.add_argument('-shortarg', '--longarg', type=type, help="help")
                    ^^^^^^^^^^^

I'm getting an error from argparse about what I believe is the "self" parameter of an argument being a string value (the short argument name). In the problems menu (menu that shows the errors in the program, try inferencing next time), it says Expected type '_ActionsContainer', got 'str' instead. The error I get when running the program says:

Traceback (most recent call last):
  File "NAME OF PROGRAM", line 4, in <module>
    parser.add_argument('-shortarg', "--longarg", type=type, help="help")
  File "argparse.py", line 1401, in add_argument
    chars = self.prefix_chars
AttributeError: 'str' object has no attribute 'prefix_chars'

I'm guessing this is an attribute that '_ActionsContainer' has, but not 'str'.

According to the docs, I don't think this should be happening — the command used in there (command to add argument) gets the same error.


I tried adding an extra string before all of the commands, with the "name" of my argument. I expected this to take over the self parameter and possibly match the format.

What actually happened is that it took over and resulted in the same error. Removing the quotes gave an unsolved reference error instead.

import argparse
parser = argparse.ArgumentParser
parser.add_argument('argname', '-shortarg', '--longarg', type=type, help="help")

Changing the order of the arguments to where the positional parameters are before the keywords has its own error, too.

import argparse
parser = argparse.ArgumentParser
parser.add_argument(type=type, help="help", '-shortarg', '--longarg')
  • 1
    What is `parser`, and how was it created? Why should we expect that the part of the documentation that you linked, is relevant there? Please read [mre] and make sure that someone else can **copy and paste** the code **without adding or changing anything** to see the **exact problem, directly**. Also, please make sure to distinguish clearly what your IDE says from what Python says. I have no idea what "the problems menu" is supposed to be, because that has to do with the program you are using to write the code (and I don't know which one that is), not the code itself. – Karl Knechtel Jul 24 '23 at 22:44
  • "According to the docs, I don't think this should be happening — the command used in there (see first code block) gets the same error." - Are you able to use the example from the docs verbatim? What happens if you try gradually transforming that example, into the code that you want? Where exactly do you first encounter a problem? – Karl Knechtel Jul 24 '23 at 22:45
  • My **wild guess** (I was able to reproduce the error this way) is that you wrote something like `parser = argparse.ArgumentParser`, renaming the class instead of actually creating an instance. Please look at the doc example more closely, and also make sure you generally understand how classes work in Python, and what their purpose is. – Karl Knechtel Jul 24 '23 at 22:48
  • @KarlKnechtel I'm currently editing all of the issues you have right now, please wait. – Caleb Birtwistle Jul 24 '23 at 22:49
  • @KarlKnechtel you are correct about your wild guess. I did not think the first two lines were not of importance, which is why i had omitted them. After all, the error only showed up mentioning one line. I do not know the type system as much, too. – Caleb Birtwistle Jul 24 '23 at 22:52
  • Please refer to [TypeError: Missing 1 required positional argument: 'self'](https://stackoverflow.com/questions/17534345). (You get a different error because your attempt does supply enough arguments, but in the wrong positions.) – Karl Knechtel Jul 24 '23 at 22:57
  • You did not create a `parser` instance, missing **()**: `parser = argparse.ArgumentParser()`. – hpaulj Jul 25 '23 at 02:48
  • You get this weird error because, without the (), `parser` is a `ArgumentParser` **class**, as opposed to an **instance** of the class. Hopefully you omited the () by mistake, as opposed to some bad tutorial or a mistreading of the docs and their examples. – hpaulj Jul 25 '23 at 03:51
  • @hpaulj Thanks for the response! You are correct, and I have already fixed the mistake. – Caleb Birtwistle Jul 26 '23 at 20:12

1 Answers1

1

This question was answered by @KarlKnechtel in the comment section.

The line parser = argparse.ArgumentParser renames the class instead of creating an instance. This can be fixed by adding () to the end of the line.