Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-42572: Improve argparse docs for the type parameter. #23849

Merged
merged 3 commits into from
Dec 20, 2020

Conversation

rhettinger
Copy link
Contributor

@rhettinger rhettinger commented Dec 18, 2020

See https://bugs.python.org/issue42572 and bpo-37572 and bpo-35005.

  • Document the exceptions that are handled.
  • Show a wider range of examples that work with type.
  • Discuss when type shouldn't be used: bool, JSONDecoder, etc.
  • Create a less whimsical example of a user-defined type converter.
  • Explain the interaction between type and default.

https://bugs.python.org/issue42572

@rhettinger rhettinger added docs Documentation in the Doc dir skip news needs backport to 3.9 only security fixes labels Dec 18, 2020
@rhettinger rhettinger changed the title bpo-42572: Import argparse docs type the type parameter. bpo-42572: Improve argparse docs for the type parameter. Dec 19, 2020
Copy link
Member

@ericvsmith ericvsmith left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a great improvement, Raymond. Thanks!

Even :class:`~argparse.FileType` has its limitations for use with the ``type``
keyword. If two arguments use *FileType* and the second one fails, an error is
reported but the first file is not automatically closed. In this case, it would
be better to wait until after the parser has run and then use the
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this true of any type errors, not just with FileType? If I have a FileType and then a float, and the type conversion for the float fails, I'd still leave the file open. I'm worried that the readers might think this is solely related to the second argument type also being FileType.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. That makes sense.

Copy link
Member

@ericvsmith ericvsmith left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great. Thanks again.

I have one nit that I forgot to include on the first go-round. That's the only issue I see.

>>> parser.add_argument('bar', type=open)
>>> parser.parse_args('2 temp.txt'.split())
Namespace(bar=<_io.TextIOWrapper name='temp.txt' encoding='UTF-8'>, foo=2)
If the type_ keyword is used with default_ keyword, the type converter is
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be "... is used with the default keyword ..." (adding "the").

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch.

@rhettinger rhettinger merged commit b0398a4 into python:master Dec 20, 2020
@miss-islington
Copy link
Contributor

Thanks @rhettinger for the PR 🌮🎉.. I'm working now to backport this PR to: 3.9.
🐍🍒⛏🤖

@bedevere-bot
Copy link

GH-23869 is a backport of this pull request to the 3.9 branch.

hectorese1 added a commit to hectorese1/cpython that referenced this pull request Dec 20, 2020
bpo-42572:  Improve argparse docs for the type parameter. (pythonGH-23849)
adorilson pushed a commit to adorilson/cpython that referenced this pull request Mar 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir skip news
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants