Skip to content
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

issue with whitespaces in sub configs when parsing CLI args #368

Closed
AlessioM opened this issue Aug 20, 2024 · 5 comments · Fixed by #369
Closed

issue with whitespaces in sub configs when parsing CLI args #368

AlessioM opened this issue Aug 20, 2024 · 5 comments · Fixed by #369
Labels
bug Something isn't working

Comments

@AlessioM
Copy link

Whitespaces in Json Lists cause issue when parsing CLI args

Tested in pydantic_settings version 2.4.0

Potential Fix

add a .strip() call at the end of the _consume_comma call here:
https://github.com/pydantic/pydantic-settings/blob/main/pydantic_settings/sources.py#L1178

Example

#test.py
from pydantic import Field
from pydantic_settings import BaseSettings
from typing import List

class SubConfig(BaseSettings):
    a: int = Field()

class Config(
    BaseSettings,
    cli_parse_args=True
):
    fields: List[SubConfig] = Field()

if __name__ == "__main__":
    cfg = Config()
    print(cfg)

Good Case

>> python test.py --fields '[{"a":1},{"a":2}]'
fields=[SubConfig(a=1), SubConfig(a=2)]

(note: no whitespace around comma in json)

Broken Case

python test.py --fields '[{"a":1}, {"a":2}]'
Traceback (most recent call last):
  File "<<LOCAL_ENV>>lib/python3.11/site-packages/pydantic_settings/sources.py", line 430, in __call__
    field_value = self.prepare_field_value(field_name, field, field_value, value_is_complex)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<<LOCAL_ENV>>lib/python3.11/site-packages/pydantic_settings/sources.py", line 632, in prepare_field_value
    raise e
  File "<<LOCAL_ENV>>lib/python3.11/site-packages/pydantic_settings/sources.py", line 629, in prepare_field_value
    value = self.decode_complex_value(field_name, field, value)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<<LOCAL_ENV>>lib/python3.11/site-packages/pydantic_settings/sources.py", line 242, in decode_complex_value
    return json.loads(value)
           ^^^^^^^^^^^^^^^^^
  File "<<LOCAL_ENV>>lib/python3.11/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<<LOCAL_ENV>>lib/python3.11/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<<LOCAL_ENV>>lib/python3.11/json/decoder.py", line 353, in raw_decode
    obj, end = self.scan_once(s, idx)
               ^^^^^^^^^^^^^^^^^^^^^^
json.decoder.JSONDecodeError: Expecting ',' delimiter: line 1 column 13 (char 12)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/alessio/prj/analysis_tools/isee_training/test.py", line 16, in <module>
    cfg = Config()
          ^^^^^^^^
  File "<<LOCAL_ENV>>lib/python3.11/site-packages/pydantic_settings/main.py", line 145, in __init__
    **__pydantic_self__._settings_build_values(
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<<LOCAL_ENV>>lib/python3.11/site-packages/pydantic_settings/main.py", line 329, in _settings_build_values
    source_state = source()                
                   ^^^^^^^^
  File "<<LOCAL_ENV>>lib/python3.11/site-packages/pydantic_settings/sources.py", line 1070, in __call__
    return super().__call__()
           ^^^^^^^^^^^^^^^^^^
  File "<<LOCAL_ENV>>lib/python3.11/site-packages/pydantic_settings/sources.py", line 432, in __call__
    raise SettingsError(
pydantic_settings.sources.SettingsError: error parsing value for field "fields" from source "CliSettingsSource"

(note: single whitespace right of comma in json)

@hramezani
Copy link
Member

Thanks @AlessioM for reporting this issue and the suggestion for the fix. Would you like to create a PR?

@kschwab what do you think about the fix?

@hramezani hramezani added bug Something isn't working and removed unconfirmed labels Aug 20, 2024
@hramezani hramezani removed their assignment Aug 20, 2024
@AlessioM
Copy link
Author

Thanks @AlessioM for reporting this issue and the suggestion for the fix. Would you like to create a PR?

@kschwab what do you think about the fix?

I did already, see above

@hramezani
Copy link
Member

Thanks @AlessioM for reporting this issue and the suggestion for the fix. Would you like to create a PR?
@kschwab what do you think about the fix?

I did already, see above

You need to create a PR in this repository. Also, don't forget to add a test for your fix.
Thanks.

@kschwab
Copy link
Contributor

kschwab commented Aug 21, 2024

Thanks @AlessioM for raising the issue. It was even more broad than what was raised here. I have opened PR with fix 👍🏾

@AlessioM
Copy link
Author

Thanks for resovling this so quickly!

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 a pull request may close this issue.

3 participants