Skip to content

Fix subcommand aliases set in namespace instead of the subcommand name - #978

Merged
mauvilsa merged 2 commits into
mainfrom
fix-subcommand-aliases
Sep 16, 2026
Merged

mauvilsa merged 2 commits into
mainfrom
fix-subcommand-aliases

Conversation

@mauvilsa

Copy link
Copy Markdown
Owner

What does this PR do?

A subcommand added with aliases was reachable by its alias, but the alias was then used as-is for the namespace. parse_args(["B"]) gave subcommand="B" and put the subcommand's values under the B key, while everything else in the parser, in particular the defaults and the subparser's own settings, uses the subcommand name b. So the parsed namespace depended on which of the equivalent names was typed, and defaults coming from the subparser were lost.

The alias is now replaced by the subcommand name as early as possible, both for the command line and for config files, so the namespace is the same no matter how the subcommand was named.

Changes:

  • _subcommands.py: ActionSubCommands.__call__ looks up the subparser first and takes the name from its subcommand attribute, which is what the namespace key and the dest value are set to. In get_subcommands, a config key that is an alias is renamed to the subcommand name before the subcommand settings keys are collected, and an alias given as the dest value is replaced in the config as well.
  • _completions_jsonschema.py: aliases are left out of the jsonschema completion, since the schema describes the config file, where only the canonical name is meaningful. Previously an alias was both a valid value of the subcommand enum and a valid property, which is no longer the case.

Before submitting

  • Did you read the contributing guideline?
  • If you used a coding agent, did you fully understand and validate all generated code and ensure it follows the contributing guidelines?
  • [n/a] Did you update the documentation? (readme and public docstrings)
  • Did you write unit tests such that there is 100% coverage on related code? (required for bug fixes and new features)
  • Did you verify that new and existing tests pass locally?
  • If this is a bug fix, did you verify that the tests fail without the code fix?
  • Did you make sure that all changes preserve backward compatibility?
  • Did you update the CHANGELOG including a pull request link? (not for typos, docs, test updates, or minor internal changes/refactors)

@mauvilsa mauvilsa added the bug Something isn't working label Sep 16, 2026
@codecov

codecov Bot commented Sep 16, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (cc43ecd) to head (ff1def3).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #978   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           27        27           
  Lines         9069      9079   +10     
=========================================
+ Hits          9069      9079   +10     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@greptile-apps

greptile-apps Bot commented Sep 16, 2026

Copy link
Copy Markdown

Greptile Summary

This PR canonicalizes subcommand aliases during command-line and configuration parsing and excludes aliases from generated configuration schemas.

  • Command-line aliases now store values beneath the canonical subcommand namespace.
  • Configuration alias keys and selector values are normalized to canonical names.
  • JSON Schema completion exposes only canonical subcommands.
  • Tests cover individual alias forms, but the normalization currently mishandles configurations containing both forms.

Confidence Score: 4/5

The PR should not merge until configuration alias normalization stops silently discarding canonical subcommand settings when both forms are present.

The main alias behavior is corrected, but assigning each alias block directly to its canonical key changes an existing ambiguity or precedence case into silent configuration loss.

Files Needing Attention: jsonargparse/_subcommands.py

Important Files Changed
Filename Overview
jsonargparse/_subcommands.py Canonicalizes command-line and configuration aliases, but direct alias-key reassignment can silently overwrite an existing canonical configuration block.
jsonargparse/_completions_jsonschema.py Restricts generated subcommand schema enums and properties to canonical names.
jsonargparse_tests/test_subcommands.py Adds coverage for command-line and config aliases, though canonical-plus-alias collisions remain uncovered.
jsonargparse_tests/test_completions_jsonschema.py Verifies aliases are rejected as JSON Schema selector values and properties.

Reviews (1): Last reviewed commit: "Fix subcommand aliases set in namespace ..." | Re-trigger Greptile

Comment thread jsonargparse/_subcommands.py Outdated
@sonarqubecloud

Copy link
Copy Markdown

@mauvilsa
mauvilsa merged commit b8c053c into main Sep 16, 2026
32 checks passed
@mauvilsa
mauvilsa deleted the fix-subcommand-aliases branch September 16, 2026 19:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant