CLI integration with existing parser is broken.
braindevices opened this issue · comments
version >=2.10 the additional argument cannot be add to the parser
test script:
from pydantic_settings import CliApp, CliSettingsSource, SettingsConfigDict, BaseSettings
import pydantic_settings
import argparse
class CLISettings(BaseSettings):
model_config = SettingsConfigDict(
case_sensitive=True,
cli_kebab_case=True,
cli_parse_args=True,
cli_implicit_flags=True
)
test: str
def parse_args():
print(f"{pydantic_settings.VERSION=}")
parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
description=__doc__
)
cli_settings = CliSettingsSource(CLISettings, root_parser=parser)
parser.add_argument(
"-v","--verbose",
dest='verbose',
default=0,
action="count",
)
parsed_args = parser.parse_args()
print(f"{parsed_args=}")
settings = CliApp.run(
CLISettings,
cli_args=parsed_args,
cli_settings_source=cli_settings
)
return settings
if __name__ == "__main__":
parse_args()
here is result:
+ exec python test-pydantic.py -h
pydantic_settings.VERSION='2.10.0'
usage: test-pydantic.py [-h] [--test str]
options:
-h, --help show this help message and exit
--test str (required)
+ exec python test-pydantic.py --test hello -vvv
pydantic_settings.VERSION='2.10.0'
usage: test-pydantic.py [-h] [--test str]
test-pydantic.py: error: unrecognized arguments: -vvv
expecting result as <2.10
+ exec python test-pydantic.py -h
pydantic_settings.VERSION='2.9.1'
usage: test-pydantic.py [-h] [--test str] [-v]
options:
-h, --help show this help message and exit
--test str (required)
-v, --verbose
+ exec python test-pydantic.py --test hello -vvv
pydantic_settings.VERSION='2.9.1'
parsed_args=Namespace(verbose=3, test='hello')
Thanks @braindevices for reporting this.
@kschwab could you please check this?
@hramezani this was introduced with #611. Looking at the original issue, #610, I missed a mistake in the motivating example. Can we revert #611 to resolve this issue? #611 causes CliSettingsSource to immediately parse the CLI args if the base settings class has cli_parse_args=True, which is not always the case (as this issue demonstrates).
Perhaps we could update the docs to reflect this and make it more explicit.
I have updated original issue with comment on correct fix.
@braindevices Can you confirm the issue has been resolved on main?
python test-cliapp-late-parse.py -h
pydantic_settings.VERSION='2.10.1'
usage: test-cliapp-late-parse.py [-h] [--test str] [-v]
options:
-h, --help show this help message and exit
--test str (required)
-v, --verbose
python test-cliapp-late-parse.py --test hello -vvv
pydantic_settings.VERSION='2.10.1'
parsed_args=Namespace(verbose=3, test='hello')
@hramezani thanks! main branch works now
I guess this should be release rather earlier than later, since 2.10 and 2.10.1 breaks code.
I wonder should we add this to tests?
Thanks @braindevices for checking.
I will make a new release probably this week.
@hramezani I think we probably need to add some test cases for this to avoid future problem
@hramezani I think we probably need to add some test cases for this to avoid future problem
Can you make a PR for it?
@hramezani @braindevices actually, @trygve-baerland already covered this with a test in #656. I verified locally that it covers the case and does fail prior to the revert and fix 👍🏾