-
-
Notifications
You must be signed in to change notification settings - Fork 60
Adding support for pydantic extras in models #732
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -1147,8 +1147,24 @@ def check_values(cfg): | |||||
| group_key = next((g for g in self.groups if key.startswith(g + ".")), None) | ||||||
| if group_key: | ||||||
| subkey = key[len(group_key) + 1 :] | ||||||
| raise NSKeyError(f"Group '{group_key}' does not accept nested key '{subkey}'") | ||||||
| raise NSKeyError(f"Key '{key}' is not expected") | ||||||
| # Check if this is a Pydantic model with extra configuration | ||||||
| group = self.groups[group_key] | ||||||
| should_raise_error = True | ||||||
| if hasattr(group, "group_class") and group.group_class: | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| from ._optionals import get_pydantic_extra_config | ||||||
|
|
||||||
| extra_config = get_pydantic_extra_config(group.group_class) | ||||||
| if extra_config == "allow": | ||||||
| # Allow extra fields - don't raise an error | ||||||
| should_raise_error = False | ||||||
| elif extra_config == "ignore": | ||||||
| # Ignore extra fields - don't raise an error, Pydantic will ignore during instantiation | ||||||
| should_raise_error = False | ||||||
|
Comment on lines
+1152
to
+1162
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All this logic seems to be pydantic-specific. So it might be better to have all of it in a function imported from optionals, e.g. |
||||||
| # For 'forbid' or None (default), raise error | ||||||
| if should_raise_error: | ||||||
| raise NSKeyError(f"Group '{group_key}' does not accept nested key '{subkey}'") | ||||||
| else: | ||||||
| raise NSKeyError(f"Key '{key}' is not expected") | ||||||
|
|
||||||
| try: | ||||||
| with parser_context(load_value_mode=self.parser_mode): | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.