{Core} Make parser compatible with Python 3.11's argparse
#24497
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Fix #23015
A rework of #24109
Azure CLI fails on Python 3.11:
Python 3.11 updated
argparse
's behavior:https://docs.python.org/3.11/whatsnew/changelog.html#id50
The PR for this change is python/cpython#18605
Per Git blame,
subparser.choices[command_verb] = command_verb
was renamed fromsubparser.choices[command_name] = command_name
by 517872asubparser.choices[command_name] = command_name
was introduced as early as 2a6f545Running this line makes
subparser.choices
astr:str
mapping.In
argparse._SubParsersAction
,self.choices
is the samedict
object asself._name_parser_map
:https://github.com/python/cpython/blob/v3.10.8/Lib/argparse.py#L1159
https://github.com/python/cpython/blob/v3.10.8/Lib/argparse.py#L769
Later, in
argparse._SubParsersAction.add_parser
,self._name_parser_map
'scommand_verb
key is overwritten as astr:parser
mapping:azure-cli/src/azure-cli-core/azure/cli/core/parser.py
Line 100 in 110f7b4
https://github.com/python/cpython/blob/v3.10.8/Lib/argparse.py#L1179
Before running this line:
After running this line:
So
subparser.choices[command_verb] = command_verb
is basically a no-op, but it starts to cause failure in Python 3.11 due to the new duplication check.I am not sure why
subparser.choices[command_verb] = command_verb
was introduced by 2a6f545 6 years ago, possibly due to some Python 2argparse
's limitation?I checked http://bugs.python.org/issue9253 mentioned in the comment, but didn't see how it is related to this workaround.
We can think it in another way. Since Python 3.11's
argparse
has no functionality change (only an additional check), if this line should be removed for Python 3.11, it should also be removed for Python 3.10-.