This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Make _SubParsersAction public
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.9, Python 3.8, Python 3.7, Python 3.6, Python 3.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: kj, paul.j3, rhettinger, sourcedelica
Priority: normal Keywords:

Created on 2020-08-19 19:21 by sourcedelica, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (8)
msg375670 - (view) Author: Eric Pederson (sourcedelica) Date: 2020-08-19 19:21
ArgumentParser.add_subparsers() returns a _SubParsersAction.  This requires user code using type annotations to use a protected type which causes type checkers like PyCharm to complain.  For example:

def add_subparser(name: str, subparser: _SubParsersAction, subparsers: dict) -> ArgumentParser:
    parser = subparser.add_parser(name)
    parser.add_argument('-v', '--verbose', action='store_true')
    subparsers[name] = parser
    return parser

You can't use plain Action because Action doesn't have the add_parser() method.
msg404724 - (view) Author: Ken Jin (kj) * (Python committer) Date: 2021-10-22 08:16
Turning a class public or not requires a lot more discussion by the argparse maintainers (I'm definitely not one of them). Have you considered this workaround for PyCharm?

from argparse import (
    _SubParsersAction,  # noqa
    ArgumentParser,
)
msg404759 - (view) Author: Ken Jin (kj) * (Python committer) Date: 2021-10-22 12:28
Closing this because this is a linter problem (inside PyCharm), not a Python problem. In the earlier message I provided a solution for PyCharm.

PS, mypy --strict doesn't complain when I import _SubParsersAction, meaning most type checkers likely won't complain either.

Thanks for your time!
msg404769 - (view) Author: Eric Pederson (sourcedelica) Date: 2021-10-22 13:51
Yes - that was the first thing I tried and why I logged the bug.  I will log a bug with Pycharm but it still seems like the long term solution is to make _SubParsersAction public since it is used like a public object.  I thought that logging the bug here would be the start of the discussion by the argparse maintainers.  Where can I get their attention?
msg404774 - (view) Author: Ken Jin (kj) * (Python committer) Date: 2021-10-22 14:34
>  Where can I get their attention?

I've nosied Raymond on this bug, he should be receiving our messages.
msg404979 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2021-10-25 16:25
`add_argument` also returns a Action subclass object.  All of those subclasses are "private".  While that return reference is usually ignored, sometimes it is useful to it, assigning it to a variable or list.  The documentation could be clearer about access to Action objects.
msg404982 - (view) Author: Eric Pederson (sourcedelica) Date: 2021-10-25 16:36
But is _SubParsersAction really private if there are methods on it that are required to use (add_parser())?
msg405009 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2021-10-25 23:51
A _SubParsersAction is private to the extent that users should not attempt to create it directly, and thus don't need to know the details - beyond what's documented:

"The add_subparsers() method is normally called with no arguments and returns a special action object. This object has a single method, add_parser(), which takes a command name and any ArgumentParser constructor arguments, and returns an ArgumentParser object that can be modified as usual."

All action objects have methods like __call__ and format_usage. The subparsers Action has other methods, but add_parser is the only new "public" method.

There's a tension in writing documentation between getting all details just right, and providing just enough for most users.  As it is, many new users are over whelmed by the documentation.

My understanding is that the "private/public" designation is a convenience for users, and not enforced by Python developers.  

I gather though that some corporate users have policies that prohibit modification of "private" objects, supposedly due to a fear that Python could modify or eliminate those objects without proper notification.  Somehow the "public" documentation sets that part of the code in stone.  My experience here is that it's easier modify the documentation to fit the code than the other way around :)
History
Date User Action Args
2022-04-11 14:59:34adminsetgithub: 85758
2021-10-25 23:51:03paul.j3setmessages: + msg405009
2021-10-25 16:36:15sourcedelicasetmessages: + msg404982
2021-10-25 16:25:39paul.j3setnosy: + paul.j3
messages: + msg404979
2021-10-22 14:34:02kjsetmessages: + msg404774
2021-10-22 13:51:55sourcedelicasetmessages: + msg404769
2021-10-22 12:28:22kjsetstatus: open -> closed
resolution: not a bug
messages: + msg404759

stage: resolved
2021-10-22 08:16:35kjsetnosy: + rhettinger
messages: + msg404724
2021-10-21 22:52:05iritkatrielsetnosy: + kj
2020-08-19 19:21:34sourcedelicacreate