-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Global Arguments stop working in Python 3.9.8 #20269
Description
Symptom
Global Arguments now stop working in Python 3.9.8:
> az cloud show --query profile
{
"endpoints": {
"activeDirectory": "https://login.microsoftonline.com",
...
Notice --query doesn't take effact.
Root cause
This is due to the argparse breaking change python/cpython#28420 which aims to solve https://bugs.python.org/issue45235. Python 3.9.8 is the first released version with this change. It will spread to all Python versions later.
In Knack, global arguments are registered on all root/intermediate/leaf parsers, and they share the same dest. Previously, leaf-level arguments takes precedence. After this change, root-level arguments take precedence.
We are not sure if this is intended or not, as according to Semantic Versioning, a PATCH version update shouldn't cause breaking change.
Since Azure CLI does some trimming on the command table, using --query on the root parser won't work. We have to use Knack directly to demonstrate it:
| command | 3.9.7 | 3.9.8 |
|---|---|---|
python .\knack\examples\exapp2 sample-json --query id |
✔ | ❌ |
python .\knack\examples\exapp2 --query id sample-json |
❌ | ✔ |
Proposed solution
Knack should only register global arguments on leaf parsers to avoid this issue. We don't allow using global arguments in the middle of a command anyway:
- ✔
az cloud show --query profile - ❌
az --query profile cloud show - ❌
az cloud --query profile show
References
This issue is also affecting other CLI tools utilizing argparse:
A related topic for --output was extensively discussed before: