Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-86357: argparse: use str() consistently and explicitly to print choices#117766

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
serhiy-storchaka merged 1 commit intopython:mainfromrindeal:patch-2
Oct 14, 2024

Conversation

@rindeal
Copy link
Contributor

@rindealrindeal commentedApr 11, 2024
edited by bedevere-appbot
Loading

This commit replacesrepr() withstr(), as the former should never be used for normal user-facing printing, and makes it explicit and consistent across the library.

For example I tried usingStrEnum for choices and it printedchoose from <Enum.FOO: 'foo'>, ... instead ofchoose from foo, ....

@ghost
Copy link

ghost commentedApr 11, 2024
edited by ghost
Loading

All commit authors signed the Contributor License Agreement.
CLA signed

@bedevere-app
Copy link

Most changes to Pythonrequire a NEWS entry. Add one using theblurb_it web app or theblurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply theskip news label instead.

@bedevere-app
Copy link

Most changes to Pythonrequire a NEWS entry. Add one using theblurb_it web app or theblurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply theskip news label instead.

@JelleZijlstra
Copy link
Member

Since this changes user-facing behavior, it needs an issue and a NEWS entry.

The change in_get_action_name could use a new unit test; it looks like the existing code there would be broken ifchoices contained a non-str.

@rindeal
Copy link
ContributorAuthor

Since this changes user-facing behavior, it needs an issue and a NEWS entry.

Done

The change in_get_action_name could use a new unit test; it looks like the existing code there would be broken ifchoices contained a non-str.

True. But that's a job fortyping and some linter, not unit tests, or?

@encukou
Copy link
Member

Since this changes user-facing behavior, it needsan issue. Do you want to file one?

As the docs say:

Use ofenum.Enum is not recommended because it is difficult to control its appearance in usage, help, and error messages.

So, in today'sargparse, you should use actual strings inchoices, and convert the value later usingMyEnum(params.foo).

As far as I know,repr is used intentionally, to add quotes around each of the strings. The change you are proposing is not obviously right.

jkittner reacted with thumbs up emoji

@rindeal
Copy link
ContributorAuthor

rindeal commentedMay 9, 2024
edited
Loading

Since this changes user-facing behavior, it needsan issue. Do you want to file one?

Done.#118839

As the docs say:

Use ofenum.Enum is not recommended because it is difficult to control its appearance in usage, help, and error messages.
So, in today'sargparse, you should use actual strings inchoices, and convert the value later usingMyEnum(params.foo).

That concerns#86667, which is unrelated to this one. The fix for the issue then, in fact, introduced another bug, ie., the sentence you quoted. It just comments onthe bad code example the fix deleted and does not hold up on its own, since anything can be used as choices and one can always expect to seestr(choice) in usage, help, etc. I suggest either removing the sentence altogether or reverting the commit and instead just changeenum.Enum toenum.StrEnum. I might do it as part of this PR, too.

As far as I know,repr is used intentionally, to add quotes around each of the strings. The change you are proposing is not obviously right.

Usingrepr() is always wrong in this context. Quoting the choices is debatable, but it's just a cosmetic preference and not used in any other place. Should its omission hinder the merging of this PR, then its trivial to do it the right way instead of therepr() hack.

@nineteendo
Copy link
Contributor

nineteendo commentedMay 9, 2024
edited
Loading

Could you addgh-118839 to the title?

gh-118839: argparse: use str() consistently and explicitly to print choices

@rindealrindeal changed the titleargparse: use str() consistently and explicitly to print choicesgh-118839: argparse: use str() consistently and explicitly to print choicesMay 9, 2024
Copy link
Member

@serhiy-storchakaserhiy-storchaka left a comment

Choose a reason for hiding this comment

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

Please add tests for an StrEnum. I suspect they can expose other issues.

@encukou
Copy link
Member

Usingrepr() is always wrong in this context.

That's false.repr is correct for strings, integers, floats, and more.
str works for integers, floats, and StrEnums -- but not plain Enums, and it's not ideal for strings.
There are many other types that people will want to use withchoices.
Theproper function would be one that can reverse the user-suppliedtype, and ideally add quoting when it's useful to set the values apart from other text.

IMO, we should continue in the direction set in#86667: choices are designed forstrings.
Perhaps we should add a way to attach string labels to arbitrary-type choices, butgenerating those labels can only be done for a small set of specific types. StrEnum is not the end here; after it we'll get a request forEnum, and there'll always be another type to support, with special handling.

As Serhiy says, StrEnum aren't well supported in general and may have other issues; that'll also be the case for any other types.

@rindeal
Copy link
ContributorAuthor

rindeal commentedMay 10, 2024
edited
Loading

There seems to be some misunderstandings here and there. Therefore, I'll summarize and clarify a few points.

This PR should address inaccuracies in both theargparse code and its documentation, with regards to thechoices parameter. More specifically, what values the parameter accepts and how they're processed.

Premises based on the API documentation:

  1. Thechoices parametermust accept anyiterablecontainer, includingStrEnum.
  2. The items in such a containermay be of any type, not just strings orStrEnum members.
  3. The container item typeshould match thetype parameter.
  4. Container itemsmust have a functional__eq__() to ensure the container's__contains__() operates correctly.
  5. Type-converted argument stringsshall be checked for presence in the container using__contains__().
  6. Container itemsmust be convertible to a string using__str__().
  7. The resulting stringshall be used to display help, usage, error messages, etc. to the user.
  8. The stringsshall be separated with commas, if the whole container is displayed to the user.

References

Current docs:https://docs.python.org/3/library/argparse.html#choices
Earliest docs:

@rindealrindealforce-pushed thepatch-2 branch 5 times, most recently frome4ee132 toaea8322CompareSeptember 25, 2024 15:45
Copy link
Member

@serhiy-storchakaserhiy-storchaka left a comment

Choose a reason for hiding this comment

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

LGTM.

This does not solve all issues, but it is an improvement for enums. Good enough.

@serhiy-storchaka
Copy link
Member

It would be nice to add a test that uses enums to demonstrate the benefit of this change.

savannahostrowski reacted with thumbs up emoji

@rindeal
Copy link
ContributorAuthor

It would be nice to add a test that uses enums to demonstrate the benefit of this change.

Reasonable wish, it’s been granted.

Copy link
Member

@serhiy-storchakaserhiy-storchaka left a comment

Choose a reason for hiding this comment

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

Thank you for your test@rindeal. I have few more comments.

Fixes:python#86357Signed-off-by: Jan Chren ~rindeal <dev.rindeal@gmail.com>
@rindeal
Copy link
ContributorAuthor

@serhiy-storchaka I fixed the conflicts you introduced and implemented all changes you suggested. Please review the updates so it can be finally merged.

Copy link
Member

@serhiy-storchakaserhiy-storchaka left a comment

Choose a reason for hiding this comment

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

Thank you for your update@rindeal.

For future, please do not use rebase. Just stack your changes one over others. This will help to review a PR iteratively, ignoring already reviewed parts. This was not a large issue for this small PR, but it may be a pain for larger PRs.

@serhiy-storchakaserhiy-storchaka merged commit66b3922 intopython:mainOct 14, 2024
@serhiy-storchakaserhiy-storchaka added needs backport to 3.12only security fixes needs backport to 3.13bugs and security fixes labelsOct 14, 2024
@miss-islington-app
Copy link

Thanks@rindeal for the PR, and@serhiy-storchaka for merging it 🌮🎉.. I'm working now to backport this PR to: 3.12.
🐍🍒⛏🤖

@miss-islington-app
Copy link

Thanks@rindeal for the PR, and@serhiy-storchaka for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13.
🐍🍒⛏🤖 I'm not a witch! I'm not a witch!

@miss-islington-app
Copy link

Sorry,@rindeal and@serhiy-storchaka, I could not cleanly backport this to3.12 due to a conflict.
Please backport usingcherry_picker on command line.

cherry_picker 66b3922b97388c328c9bd8df050eef11c0261fae 3.12

@miss-islington-app
Copy link

Sorry,@rindeal and@serhiy-storchaka, I could not cleanly backport this to3.13 due to a conflict.
Please backport usingcherry_picker on command line.

cherry_picker 66b3922b97388c328c9bd8df050eef11c0261fae 3.13

serhiy-storchaka pushed a commit to serhiy-storchaka/cpython that referenced this pull requestOct 14, 2024
…y to print choices (pythonGH-117766)(cherry picked from commit66b3922)Co-authored-by: rindeal <dev.rindeal@gmail.com>Signed-off-by: Jan Chren ~rindeal <dev.rindeal@gmail.com>
@bedevere-app
Copy link

GH-125431 is a backport of this pull request to the3.13 branch.

@bedevere-appbedevere-appbot removed the needs backport to 3.13bugs and security fixes labelOct 14, 2024
serhiy-storchaka pushed a commit to serhiy-storchaka/cpython that referenced this pull requestOct 14, 2024
…y to print choices (pythonGH-117766)(cherry picked from commit66b3922)Co-authored-by: rindeal <dev.rindeal@gmail.com>Signed-off-by: Jan Chren ~rindeal <dev.rindeal@gmail.com>
@bedevere-app
Copy link

GH-125432 is a backport of this pull request to the3.12 branch.

@bedevere-appbedevere-appbot removed the needs backport to 3.12only security fixes labelOct 14, 2024
serhiy-storchaka added a commit that referenced this pull requestOct 14, 2024
…rint choices (GH-117766) (GH-125432)(cherry picked from commit66b3922)Signed-off-by: Jan Chren ~rindeal <dev.rindeal@gmail.com>Co-authored-by: rindeal <dev.rindeal@gmail.com>
serhiy-storchaka added a commit that referenced this pull requestOct 14, 2024
…rint choices (GH-117766) (GH-125431)(cherry picked from commit66b3922)Signed-off-by: Jan Chren ~rindeal <dev.rindeal@gmail.com>Co-authored-by: rindeal <dev.rindeal@gmail.com>
@rindealrindeal deleted the patch-2 branchOctober 14, 2024 14:37
dogukancagatay added a commit to dogukancagatay/pytest-split that referenced this pull requestNov 14, 2024
Due to recent changes in argparse error message construction the testthat is checking for the non-existing split algorithm was failing.python/cpython#117766Fixed with a lenient error string check with additional dynamic algorithmcheck.
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@serhiy-storchakaserhiy-storchakaserhiy-storchaka approved these changes

@encukouencukouAwaiting requested review from encukou

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

5 participants

@rindeal@JelleZijlstra@encukou@nineteendo@serhiy-storchaka

[8]ページ先頭

©2009-2025 Movatter.jp