Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Use SettingsErrors when file can't be used as source#432

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
l00ptr wants to merge3 commits intopydantic:main
base:main
Choose a base branch
Loading
froml00ptr:throw-exception-on-unusable-files

Conversation

l00ptr
Copy link

Some file formats (at least yaml) allow to represent non dictionnary values. In such situation we can't add the values read from those files. Instead of raising a ValueError, we now raise a SettingsError and indicate we can't parse the related config file.

@hramezani
Copy link
Member

hramezani commentedOct 2, 2024
edited
Loading

Thanks@l00ptr for this PR. I think this will introduce a breaking change because some people's code might depend on the old error.

This change can be made in the next major releaseV3. but we need to have consistent behavior over all settings sources and add some tests for that.

@l00ptr
Copy link
Author

ok, i think i can try to help on that topic. I can start writing a simple test case for invalid yaml file and handle json decoding error.

@dlax
Copy link
Contributor

dlax commentedOct 4, 2024

I think this will introduce a breaking change because some people's code might depend on the old error.

It seems to me that the change would not break users code becauseSettingsError is a subclass ofValueError:

classSettingsError(ValueError):

so anybody doing:

try:s=Settings()exceptValueError:# handle exception here   ...

would still have their code working. This could be checked in tests added here.

Or did you have something else in mind@hramezani ?

Comment on lines 1915 to 1918
try:
vars.update(self._read_file(file_path))
except ValueError as e:
raise SettingsError(f'Parsing error encountered for {file_path}: {e}')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I think we could improve the user experience by explicitly checking that the result of file read is a dict because otherwise, (e.g. in added tests), theValueError,which is aboutdict.update(), is not very informative:

>>>d{}>>>d.update("abc")Traceback (mostrecentcalllast):File"<stdin>",line1,in<module>ValueError:dictionaryupdatesequenceelement#0 has length 1; 2 is required

Also, I realize that callingdict.update() with an iterable is also valid and so loading a YAML like:

-- x  -y-- a  -b

would work; not sure if it's expected? But maybe it's another issue.

Copy link
Author

@l00ptrl00ptrOct 8, 2024
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

we can probably check if the object returned byself._read_file is a dict or not and handle that specific case.

There are also a lot of cases where reading from (toml, yaml,...) file will raise ValueError. Eg:

importtomlitomli.loads("demo =\ndemo2 =")

Will throw a tomli.TOMLDecodeError :/ finally i think we should simply keep theValueError.

@hramezani
Copy link
Member

hramezani commentedOct 4, 2024
edited
Loading

I think this will introduce a breaking change because some people's code might depend on the old error.

It seems to me that the change would not break users code becauseSettingsError is a subclass ofValueError:

classSettingsError(ValueError):

so anybody doing:

try:s=Settings()exceptValueError:# handle exception here   ...

would still have their code working. This could be checked in tests added here.

Or did you have something else in mind@hramezani ?

Thanks@dlax for the explanation. However, it is still a breaking change because some people's code might depend on the error typeValueError(rare case). So, better not to do it now and postpone the decision beforeV3.

pydantic-settings had a lot of changes from V1 to V2 and we decided not to introduce breaking changes even something like this.

@hramezani
Copy link
Member

@l00ptr Thanks for your update and for adding tests.

As I mentioned above, we can't merge this PR because of breaking changes. but you've added some good tests.
Please update the PR, keep only the tests, and move them to the specific test file.

  • test_source_json.py
  • test_source_toml.py
  • test_source_yaml.py
pydantic-hooky[bot] reacted with thumbs up emoji

@l00ptrl00ptrforce-pushed thethrow-exception-on-unusable-files branch 2 times, most recently frome49dbbd to4a9f0c3CompareOctober 8, 2024 12:59
Comment on lines +1915 to +1928
try:
settings = self._read_file(file_path)
except ValueError as e:
raise SettingsError(f'Failed to parse settings from {file_path}, {e}')
if not isinstance(settings, dict):
raise SettingsError(
f'Failed to parse settings from {file_path}, expecting an object (valid dictionnary)'
)
vars.update(settings)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

@l00ptr Thanks for updating this.
As I mentioned before we can't merge this PR because it will introduce a breaking change. So, we have 2 options here:

1- Keep only the tests that test the current behavior(ValueError) and revert to raisingSettingsError; then, we can merge the tests only.
2- Keep the PR open untilV3 and merge it beforeV3.

I would prefer option 1.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

@hramezani i splitted those changes on 3 commits, let me know if i should create a specific MR for the 2 last commits (about SettingsError) and only keep the first one here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

@l00ptr no need for a new PR.

Then we have to wait to merge it onV3. I will add av3 label to the PR to not merge it untilv3

Julian Vanden Broeck added2 commitsNovember 7, 2024 16:34
Some file formats (at least yaml) allow to represent non dictionnaryvalues. In such situation we can't add the values read from those files.Instead of raising a ValueError, we now raise a SettingsError andindicate we can't parse the related config file.
@l00ptrl00ptrforce-pushed thethrow-exception-on-unusable-files branch from4a9f0c3 to55ff1e5CompareNovember 7, 2024 15:52
We verify the loaded settings from files is dict, otherwise we raise aSettingsError. With that change, we make Pydantic fails a bit faster.
@l00ptrl00ptrforce-pushed thethrow-exception-on-unusable-files branch from55ff1e5 toc6e6fb9CompareNovember 7, 2024 15:55
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@dlaxdlaxdlax left review comments

@hramezanihramezanihramezani left review comments

Assignees

@l00ptrl00ptr

Labels
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

3 participants
@l00ptr@hramezani@dlax

[8]ページ先頭

©2009-2025 Movatter.jp