Python Forum
Why this pycharm warning for argparse formatter_class value?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why this pycharm warning for argparse formatter_class value?
#1
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
Reply
#2
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.
Reply
#3
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Bug argparse io issue pyDream 8 702 Apr-02-2024, 12:42 PM
Last Post: pyDream
  Not able to figure out what is wrong with argparse radioactive9 31 8,621 Mar-16-2022, 07:50 PM
Last Post: deanhystad
  argparse --help in one line. Denial 1 2,004 Sep-20-2020, 03:38 PM
Last Post: deanhystad
  Argparse error when inputting values tqader 2 2,892 Sep-11-2020, 07:42 PM
Last Post: buran
  Can argparse support undocumented options? pjfarley3 3 2,227 Aug-14-2020, 06:13 AM
Last Post: pjfarley3
  In ArgParse, can two compatible formatter_class values be used? pjfarley3 2 2,601 Jul-31-2020, 02:01 PM
Last Post: pjfarley3
  Why am I getting KeyError 'file' when using argparse? Mike Ru 1 3,085 Jun-09-2019, 04:48 PM
Last Post: metulburr
  How can I get some arguments using argparse? Mike Ru 0 1,888 Jun-05-2019, 12:57 PM
Last Post: Mike Ru
  argparse 2skywalkers 4 3,145 May-15-2019, 07:00 PM
Last Post: Gribouillis
  argparse and imported modules spatialdawn 2 5,473 Dec-01-2018, 12:35 PM
Last Post: spatialdawn

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020