-
Notifications
You must be signed in to change notification settings - Fork 299
Add extra
parameter to the validate functions
#1722
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?
Conversation
please review |
Codecov ReportAttention: Patch coverage is 📢 Thoughts on this report? Let us know! |
CodSpeed Performance ReportMerging #1722 will not alter performanceComparing Summary
|
CI checks are failing with:
Hopefully a temporary issue. But how do I re-run them? [Edit: a force-push or two later, everything seems ok now] |
bae98e8
to
74c3cd9
Compare
@sydney-runkle anything I can do to help get this merged? |
@davidhewitt this has been gathering dust for a while.. anything preventing it from being merged? |
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.
Sorry for the delay @anvilpete, I think this makes sense to include. Left some initial comments
@@ -107,6 +108,8 @@ class SchemaValidator: | |||
input: The Python object to validate. | |||
strict: Whether to validate the object in strict mode. | |||
If `None`, the value of [`CoreConfig.strict`][pydantic_core.core_schema.CoreConfig] is used. | |||
extra: The behavior for handling extra fields. |
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.
extra: The behavior for handling extra fields. | |
extra: Whether to ignore, allow, or forbid extra data during model validation. |
And on other places where extra
is documented.
from_attributes: Option<bool>, | ||
context: Option<&Bound<'_, PyAny>>, | ||
self_instance: Option<&Bound<'_, PyAny>>, | ||
allow_partial: PartialMode, | ||
by_alias: Option<bool>, | ||
by_name: Option<bool>, | ||
) -> PyResult<PyObject> { | ||
let extra_behavior = extra.map(|e| ExtraBehavior::from_str(e.to_str()?)).transpose()?; |
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.
ExtraBehavior::from_str
raises a SchemaError
, which doesn't really make sense here. Let's raise a ValueError
instead (same for other methods).
@pytest.mark.parametrize( | ||
'schema_extra_behavior,validate_fn_extra_kw', | ||
[ | ||
({'extra_behavior': 'forbid'}, None), | ||
({'extra_behavior': 'ignore'}, 'forbid'), | ||
], | ||
) |
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.
To simplify a bit, you could have both parameters as a string
@pytest.mark.parametrize( | |
'schema_extra_behavior,validate_fn_extra_kw', | |
[ | |
({'extra_behavior': 'forbid'}, None), | |
({'extra_behavior': 'ignore'}, 'forbid'), | |
], | |
) | |
@pytest.mark.parametrize( | |
'schema_extra_behavior,validate_fn_extra_kw', | |
[ | |
('forbid', None), | |
('ignore', 'forbid'), | |
], | |
) |
v = py_and_json( | ||
cs.arguments_v3_schema( | ||
[ | ||
cs.arguments_v3_parameter(name='a', schema=cs.int_schema()), | ||
cs.arguments_v3_parameter(name='b', schema=cs.int_schema(), alias='c'), | ||
], | ||
extra_behavior='forbid', | ||
**schema_extra_behavior, |
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.
**schema_extra_behavior, | |
extra_behavior=schema_extra_behavior, |
@pytest.mark.parametrize( | ||
'schema_extra_behavior,validate_fn_extra_kw', | ||
[ | ||
({'extra_behavior': 'allow'}, None), | ||
({'extra_behavior': 'ignore'}, 'allow'), | ||
], | ||
) | ||
def test_model_class_extra(schema_extra_behavior: dict[str, Any], validate_fn_extra_kw: Union[ExtraBehavior, None]): |
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.
Same as above.
@pytest.mark.parametrize( | ||
'schema_extra_behavior,validate_fn_extra_kw', | ||
[ | ||
({'extra_behavior': 'forbid'}, None), | ||
({'extra_behavior': 'ignore'}, 'forbid'), | ||
], | ||
) |
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.
Same as above.
Change Summary
This change adds an
extra
parameter to the validation functions, overriding any other model configuration. After a corresponding update to Pydantic, this will enable the following from pydantic/pydantic#9278:I've tried to add tests and update existing ones where it makes sense. It's a very small change but touches a wide surface area, so please let me know if I've missed anything.
See also pydantic/pydantic#11057 for a similar request.
Related issue number
Checklist
pydantic-core
(except for expected changes)Selected Reviewer: @sydney-runkle