-
-
Notifications
You must be signed in to change notification settings - Fork 47
feat: add python-benedict[schema] optional dependency (pydantic v2)
#565
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
Changes from 11 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
5c1a279
feat: add python-benedict[schema] optional dependency based on pydantic
Copilot d62a546
test: improve INI schema test to assert type coercion
Copilot f3e65c5
refactor: rename test file to test_schema.py and split into per-group…
Copilot b1a5d7c
docs: document schema optional extra in README
Copilot efdc7eb
test: add schema edge-case tests (extra fields stripped, validation e…
Copilot ccf9ef2
test: add schema kwarg coverage for all from_*/to_* IO methods
Copilot 2d8e2bc
fix: use importlib.util.find_spec for pydantic availability check; ap…
Copilot a299adc
feat: validate schema arg is a pydantic BaseModel subclass in apply_s…
Copilot 0d11676
fix: pin zipp>=3.19.1 to remediate CVE-2024-5569 (infinite loop DoS)
Copilot e969fb7
refactor: use try/except ImportError instead of importlib to detect p…
Copilot c701cfa
Potential fix for pull request finding
fabiocaccamo 4cbdf47
pin: use == for zipp version in requirements.txt
Copilot a6a798c
Merge remote-tracking branch 'origin/main' into copilot/update-schema…
Copilot 361b195
fix: add pragma no cover to ImportError, remove zipp from requirement…
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from typing import Any | ||
|
|
||
| try: | ||
| import pydantic | ||
|
|
||
| pydantic_installed = True | ||
| except ImportError: | ||
|
fabiocaccamo marked this conversation as resolved.
Outdated
|
||
| pydantic_installed = False | ||
|
|
||
|
|
||
| def apply_schema(data: Any, schema: Any) -> Any: | ||
| """ | ||
| Validate and parse data using a Pydantic model class. | ||
| Returns the validated data as a plain dict. | ||
| Raises ExtrasRequireModuleNotFoundError if pydantic is not installed. | ||
| Raises TypeError if schema is not a pydantic BaseModel subclass. | ||
| """ | ||
| from benedict.extras import require_schema | ||
|
|
||
| require_schema(installed=pydantic_installed) | ||
| if isinstance(schema, type) and issubclass(schema, pydantic.BaseModel): | ||
| schema_cls: type[pydantic.BaseModel] = schema | ||
| else: | ||
| raise TypeError( | ||
| f"schema must be a pydantic BaseModel subclass, got {type(schema)!r}" | ||
| ) | ||
| instance = schema_cls.model_validate(data) | ||
| return instance.model_dump() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.