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 usingformatter_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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With