Thanks to visit codestin.com
Credit goes to github.com

Skip to content

gh-132558: Improve argparse docs on combining type and choices #133827

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

hansthen
Copy link

@hansthen hansthen commented May 10, 2025

This clarifies the documentation a bit to warn users of the potential side effects of using both type and choices on an action. It also suggests that it is better in those cases to do type conversion in application code.


📚 Documentation preview 📚: https://cpython-previews--133827.org.readthedocs.build/

@bedevere-app bedevere-app bot added docs Documentation in the Doc dir skip news labels May 10, 2025
@github-project-automation github-project-automation bot moved this to Todo in Docs PRs May 10, 2025
@picnixz picnixz changed the title gh-132558: Improve docs on combining *type* and *choices* gh-132558: Improve argparse docs on combining type and choices May 10, 2025
Comment on lines 1130 to 1133
Formatted choices override the default *metavar* which is normally derived
from *dest*. This is usually what you want because the user never sees the
*dest* parameter. If this display isn't desirable (perhaps because there are
many choices), just specify an explicit metavar_.
Copy link
Member

Choose a reason for hiding this comment

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

This should not be inbetween the choices notes.

Copy link
Author

Choose a reason for hiding this comment

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

It was already with the choices documentation. Where would you move it to?

Copy link
Member

Choose a reason for hiding this comment

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

You should move the text you added above it.

Comment on lines 1141 to 1142
Use of :class:`enum.Enum` is not recommended because it is difficult to
control its appearance in these messages.
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 it's previous place was fine, it continued naturally from the previous point.

Comment on lines +1133 to +1143
Note that inclusion in the *choices* sequence is checked after any type_
conversions have been performed, so the type of the objects in the *choices*
sequence should match the type_ specified. This may interfere with
the display of the choices in usage, help and error messages, because the
*choices* may not be familiar to the end-user.

If you have to use both *type* and *choices* you can use a wrapper class
that formats the value after type conversion back to the vocabulary of the
end-user. However, it is usually easier to not specify *type* and perform
type conversion in application code.

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Note that inclusion in the *choices* sequence is checked after any type_
conversions have been performed, so the type of the objects in the *choices*
sequence should match the type_ specified. This may interfere with
the display of the choices in usage, help and error messages, because the
*choices* may not be familiar to the end-user.
If you have to use both *type* and *choices* you can use a wrapper class
that formats the value after type conversion back to the vocabulary of the
end-user. However, it is usually easier to not specify *type* and perform
type conversion in application code.
Note that inclusion in the *choices* sequence is checked after any type_
conversions have been performed, so the type of the objects in the *choices*
sequence should match the type_ specified.
If you want *choices* to be compared against a formatted or transformed
value, consider using a custom wrapper class for type_ that performs the
conversion and formatting during parsing instead.

Copy link
Author

Choose a reason for hiding this comment

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

@savannahostrowski The reason I included the line you suggest removing, was that during discussion on ODP, some people mentioned this specifically as advice. Also, in other places in the documentation of argparse, people recommend against making the type function do too much and limit it too simple types like float and int.

In general, the recommendation to not use type in this scenario seems sound. It leads to less lines of code in user space than creating a wrapper class as workaround. Also, when implementing wrapper classes I noticed that I typically had to cast the value from the wrapper back into the intended class. I would not want to point the user into the wrong direction without also pointing to a more pragmatic solution.

However, it is up to you if you think the last line is unacceptable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting review docs Documentation in the Doc dir skip news
Projects
Status: Todo
Development

Successfully merging this pull request may close these issues.

3 participants