25

I use Python 3 and want to wrap argparse.ArgumentParser with a custom class that sets formatter_class=argparse.RawDescriptionHelpFormatter by default. I can do this successfully, however IntelliJ IDEA 2017.1 with Python Plugin (PyCharm) gives a warning for the following code:

class CustomParser(argparse.ArgumentParser):
def __init__(self, formatter_class=argparse.RawDescriptionHelpFormatter, **kwargs):
    # noinspection PyArgumentList
    super().__init__(formatter_class=formatter_class, **kwargs)  # warning in this line for the last argument if suppression comment above removed

If one removes the comment with the IntelliJ suppression command the warning on kwargs is "Expected a dictionary, got a dict", however it is working. Is this a false positive warning or can this be done better without the warning? Is there a real issue behind this warning that it helps avoiding?

Side question: Is there a difference in using
formatter_class = kwargs.pop('formatter_class', argparse.RawDescriptionHelpFormatter) instead of explicitly defining the named parameter in the signature? According to PEP20 more explicit in the signature is better, right?

jan
  • 2,741
  • 4
  • 35
  • 56
  • There is another warning I see now, too. If I want to change the default formatter with `CustomParser(formatter_class=argparse.HelpFormatter)` I have the warning "Expected type 'Type[RawDescriptionHelpFormatter]', got 'Type[HelpFormatter]' instead" that can be suppressed with `# noinspection PyTypeChecker`. Is that also false positive? – jan Apr 21 '17 at 12:16

3 Answers3

17

Yes, that appears to be a false positive.

You asked about formatter_class=kwargs.pop('formatter_class', argparse.RawDescriptionHelpFormatter). Please don't do that. Mutating kwargs, which appears as the next argument, seems Bad. Additionally, default keyword args should be set to a simple constant rather than some mutable datastructure, as there is a big difference between evaluating at import time and evaluating at run time. See e.g. http://www.effbot.org/zone/default-values.htm . The usual idiom would be formatter_class=None in the signature, and then in the body you test for None and you mutate kwargs to your heart's content.

J_H
  • 17,926
  • 4
  • 24
  • 44
  • Do you know a handy example where this warning would be a real issue and not a false positive? I have not done much in Python since this last question and it looks like a very easy error, right? – jan Jan 04 '18 at 08:36
  • 4
    A week ago Pavel Karateev explained in https://youtrack.jetbrains.com/issue/PY-27935 that this is just a bug, and is now fixed in pycharm 2017.3.3 RC; cf simple repeat-by in base bug https://youtrack.jetbrains.com/issue/PY-27686 – J_H Jan 18 '18 at 15:33
  • It looks like your linked bug concerns only python 2, are you sure this python 3 false positive warning is solved with this release? I'll update and verify this if I find time. – jan Jan 19 '18 at 09:21
  • Quite right, that's strictly a python2 thing. You may want to file a new bug for python3. – J_H Jan 19 '18 at 18:01
2

Marking all the virtual environment directories stored inside the project (e.g. ./venv) as excluded has solved it for me with PyCharm 2020.2.3.

To do so: right-click on the virtual environment directories in the project tree and select: Mark Directory as -> Excluded. Solution from https://youtrack.jetbrains.com/issue/PY-39715.

Remi Cuingnet
  • 943
  • 10
  • 14
1

Yes, that appears to be a false positive. As said @J_H

The best descicion i got by Chat-gpt. It was adding comment # type: ignore at the end of string

class Price(ItemBase):
def __init__(self, *args, **kwargs):
    super(Price, self).__init__(*args, **kwargs)  # type: ignore