Python Forum
Why this pycharm warning for argparse formatter_class value? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Why this pycharm warning for argparse formatter_class value? (/thread-29481.html)



Why this pycharm warning for argparse formatter_class value? - pjfarley3 - Sep-04-2020

The below script runs just fine from the command line, but pycharm complains that the formatter_class value in the call to argparse.ArgumentParser is of an "Unexpected type", it says it is expecting "_FormaterClass" as the type.

The class MyFormatter is taken from a response to an earlier post of mine that suggested it and it seems to work without a problem. URL for that discussion is:

Discussion of argparse formatters

Should I be concerned about this pycharm warning?

OS version is Win10 (64-bit) w/latest update, pycharm is version 2020.2.1, python version is 3.8.3 (64-bit).

Peter

argtest1.py code is:

import argparse

MYVERSION = "0.0.1"


# Used by getarguments() to both display default values and leave
# epilog indented as-typed
class MyFormatter(
                  argparse.ArgumentDefaultsHelpFormatter,
                  argparse.RawDescriptionHelpFormatter):
    pass


def getarguments():
    """Process and display command-line options using argparse"""
    _pgmdesc = "MySript that does something"
    _epidesc = ("""\
        MyScipt Epilog line 1
        MyScipt Epilog line 2
        """)
    sstparser = argparse.ArgumentParser(description=_pgmdesc,
                                        epilog=_epidesc,
                                        formatter_class=MyFormatter,
                                        fromfile_prefix_chars="@")
    sstparser.add_argument("-V", "--version", action="version", version="%(prog)s " + MYVERSION)
    sstparser.add_argument("-s", "--randseed", help="Override initial random seed value", type=int, default=0)
    sstargs = sstparser.parse_args()
    return sstargs


myparse = getarguments()
exit()
Output from argtest1.py -h is:

Output:
usage: argtest1.py [-h] [-V] [-s RANDSEED] MyScript that does something optional arguments: -h, --help show this help message and exit -V, --version show program's version number and exit -s RANDSEED, --randseed RANDSEED Override initial random seed value (default: 0) MyScipt Epilog line 1 MyScipt Epilog line 2
Output from argtest1.py -V is:

Output:
argtest1.py 0.0.1



RE: Why this pycharm warning for argparse formatter_class value? - Gribouillis - Sep-05-2020

It looks to me like a drawback of Pycharm (which I don't use). Try to use argparse.ArgumentDefaultsHelpFormatter alone instead of MyFormatter to see if it complains again. If it does, it is a flaw of PyCharm.


RE: Why this pycharm warning for argparse formatter_class value? - pjfarley3 - Sep-09-2020

It would seem to be a pycharm issue. Using argparse.ArgumentDefaultsHelpFormatter directly as the value of formatter_class generates the same (or at least very similar) warning about a mis-matched argument type for the formatter_class argument.

I will ask over on the pycharm community page and then report a possible bug to pycharm support if it is not WAD.

Thanks for your suggestion. I will mark this question as answered here.

Peter