Skip to content

CLI Serialization Fixes #649

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

Merged
merged 6 commits into from
Jul 20, 2025
Merged

Conversation

kschwab
Copy link
Contributor

@kschwab kschwab commented Jul 2, 2025

  • Adds serialization of sub commands.
  • Fixes serialization of kebab case of flags.

@hramezani
Copy link
Member

Thanks @kschwab

Is it a fix for an issue?

@kschwab
Copy link
Contributor Author

kschwab commented Jul 3, 2025

@hramezani yes, it is an issue I found locally. I can open a formal ticket if you would like.

CliApp.serialize is incorrect when cli_kebab_case is enabled. Currently on main it will do the below:

from pydantic_settings import BaseSettings, CliApp

class Cfg(BaseSettings, cli_kebab_case=True):
    foo_kebab: int = 123

args = CliApp.serialize(Cfg())

print(args)
#> ['--foo_kebab', '123']

CliApp.run(Cfg, cli_args=args)
"""
usage: example.py [-h] [--foo-kebab int]
example.py: error: unrecognized arguments: --foo_kebab 123
"""

With this fix, it will correctly apply the kebab case to serialization:

from pydantic_settings import BaseSettings, CliApp

class Cfg(BaseSettings, cli_kebab_case=True):
    foo_kebab: int = 123

print(CliApp.serialize(Cfg()))
#> ['--foo-kebab', '123'] 

print(CliApp.run(Cfg, cli_args=args))
#> foo_kebab=123

I also missed serialization of subcommands, which addressed as well in this PR. See added test.

@kschwab kschwab marked this pull request as draft July 3, 2025 02:05
@kschwab
Copy link
Contributor Author

kschwab commented Jul 3, 2025

I also just noticed that cli_implicit_flags case will also likely need an adjustment.

@kschwab kschwab force-pushed the cli-serialize-subcommand branch from fa35ea9 to e985e22 Compare July 15, 2025 14:25
@kschwab kschwab changed the title CLI Support for Subcommand Serialization CLI Serialization Fixes Jul 16, 2025
@kschwab kschwab marked this pull request as ready for review July 16, 2025 14:56
@kschwab
Copy link
Contributor Author

kschwab commented Jul 16, 2025

Ok @hramezani, this is ready for review again. When I looked closer, there were a handful of bugs I found with the original serialization. The change set looks larger then it is. I reverted the original serialization implementation, and consolidated it into two standalone functions.

The bugs I found and fixed were:

  • Serialization ordering could be wrong. i.e., ordering of positionals and subcommands has to be strictly maintained.
  • Skip serialization of defaults. Only modified values should be serialized.
  • Handle serialization of kebab case.
  • Handle implicit bool flags.

All the newly added tests currently fail on main.

@hramezani
Copy link
Member

Thanks @kschwab, Great job!

@hramezani hramezani merged commit be11824 into pydantic:main Jul 20, 2025
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants