-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Union of Annotated fields do not respect Field(strict=False) #9206
Comments
Hmph, thanks for reporting this. Definitely a bug - can likely be fixed in the schema generation logic in |
@sydney-runkle I can pick this up if no one is working on this. Can you please assign this to me? |
The issue comes from the output of @sydney-runkle @samuelcolvin thoughts? |
Sounds good, feel free to open a PR, I'd be happy to review! |
@sydney-runkle I'm not able to figure out the right way to solve this. Please feel free to re-assign it to someone else |
It seems that the error happens because of the union of annotated. Specifically, this has the problem: class StrictTestModel(BaseModel):
model_config = ConfigDict(strict=True)
results_filepath: (
Annotated[NewPath, Field(strict=False)]
| Annotated[FilePath, Field(strict=False)]
) but this doesn't: class StrictTestModel(BaseModel):
model_config = ConfigDict(strict=True)
results_filepath: (
Annotated[NewPath, Field(strict=False)]
) I think what's going on is that For what it's worth, @davidhewitt I think there's a chance you'd be able to figure this out quickly. |
Hey, Indeed, the field's Test examples: Here are a couple of test examples with ModelValidator This does not work: def test_model_field_with_union_type_and_strict_mode_disabled():
class MyModel:
__slots__ = (
"__dict__",
"__pydantic_fields_set__",
"__pydantic_extra__",
"__pydantic_private__",
)
v = SchemaValidator(
{
"type": "model",
"cls": MyModel,
"schema": {
"type": "union",
"strict": True,
"choices": [
{
"type": "model-fields",
"fields": {
"foo": {
"type": "model-field",
"strict": False,
"schema": {"type": "int"},
}
},
},
{
"type": "model-fields",
"fields": {
"bar": {
"type": "model-field",
"strict": False,
"schema": {"type": "int"},
}
},
},
],
},
}
)
m = v.validate_python({"foo": "123"}) Validator: ModelValidator {
revalidate: Never,
validator: Union(
UnionValidator {
mode: Smart,
choices: [
(
ModelFields(
ModelFieldsValidator {
fields: [
Field {
name: "foo",
lookup_key: Simple {
key: "foo",
py_key: Py(
0x00007f7d48d805d0,
),
path: LookupPath(
[
S(
"foo",
Py(
0x00007f7d48d82d90,
),
),
],
),
},
name_py: Py(
0x00007f7d4bc194a0,
),
validator: Int(
IntValidator {
strict: false,
},
),
frozen: false,
},
],
model_name: "Model",
extra_behavior: Ignore,
extras_validator: None,
strict: false,
from_attributes: false,
loc_by_alias: true,
},
),
None,
),
(
ModelFields(
ModelFieldsValidator {
fields: [
Field {
name: "bar",
lookup_key: Simple {
key: "bar",
py_key: Py(
0x00007f7d48d80630,
),
path: LookupPath(
[
S(
"bar",
Py(
0x00007f7d48d810e0,
),
),
],
),
},
name_py: Py(
0x00007f7d4bc19440,
),
validator: Int(
IntValidator {
strict: false,
},
),
frozen: false,
},
],
model_name: "Model",
extra_behavior: Ignore,
extras_validator: None,
strict: false,
from_attributes: false,
loc_by_alias: true,
},
),
None,
),
],
custom_error: None,
strict: true,
name: "union[model-fields,model-fields]",
},
),
class: Py(
0x00005561e20c1fd0,
),
post_init: None,
frozen: false,
custom_init: false,
root_model: false,
undefined: Py(
0x00007f7d4c5c13a0,
),
name: "MyModel",
} This works: def test_model_field():
class MyModel:
__slots__ = (
"__dict__",
"__pydantic_fields_set__",
"__pydantic_extra__",
"__pydantic_private__",
)
v = SchemaValidator(
{
"type": "model",
"cls": MyModel,
"schema": {
"type": "union",
"strict": True,
"choices": [
{
"type": "model-fields",
"fields": {
"foo": {
"type": "model-field",
"strict": False,
"schema": {"type": "int"},
}
},
},
],
},
}
)
m = v.validate_python({"foo": "123"}) Validator: ModelValidator {
revalidate: Never,
validator: ModelFields(
ModelFieldsValidator {
fields: [
Field {
name: "foo",
lookup_key: Simple {
key: "foo",
py_key: Py(
0x00007fd5b5b191d0,
),
path: LookupPath(
[
S(
"foo",
Py(
0x00007fd5b72a1f20,
),
),
],
),
},
name_py: Py(
0x00007fd5b83154a0,
),
validator: Int(
IntValidator {
strict: false,
},
),
frozen: false,
},
],
model_name: "Model",
extra_behavior: Ignore,
extras_validator: None,
strict: false,
from_attributes: false,
loc_by_alias: true,
},
),
class: Py(
0x00005593d66b1880,
),
post_init: None,
frozen: false,
custom_init: false,
root_model: false,
undefined: Py(
0x00007fd5b8cbd3a0,
),
name: "MyModel",
} |
Great analysis here. Will definitely be helpful to have if we move forward with this as a desired change! |
Initial Checks
Description
Cannot override strict mode for the annotated union of
NewPath
andFilepath
fields when strict mode is set for all fields viaConfigDict
.Example Code
Python, Pydantic & OS Version
The text was updated successfully, but these errors were encountered: