Skip to content
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

Make strict config overridable in field for Path #7281

Merged
merged 1 commit into from Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions pydantic/_internal/_std_types_schema.py
Expand Up @@ -29,6 +29,7 @@

from pydantic.errors import PydanticSchemaGenerationError
from pydantic.fields import FieldInfo
from pydantic.types import Strict

from ..config import ConfigDict
from ..json_schema import JsonSchemaValue, update_json_schema
Expand Down Expand Up @@ -241,6 +242,11 @@ def path_validator(input_value: str) -> os.PathLike[Any]:
python_schema=core_schema.is_instance_schema(source_type),
)

strict: bool | None = None
for annotation in annotations:
if isinstance(annotation, Strict):
strict = annotation.strict

schema = core_schema.lax_or_strict_schema(
lax_schema=core_schema.union_schema(
[
Expand All @@ -253,6 +259,7 @@ def path_validator(input_value: str) -> os.PathLike[Any]:
),
strict_schema=instance_schema,
serialization=core_schema.to_string_ser_schema(),
strict=strict,
)

return (
Expand Down
10 changes: 10 additions & 0 deletions tests/test_types.py
Expand Up @@ -3206,6 +3206,16 @@ class Model(BaseModel):
}


def test_path_strict_override():
class Model(BaseModel):
model_config = ConfigDict(strict=True)

x: Path = Field(strict=False)

m = Model(x='/foo/bar')
assert m.x == Path('/foo/bar')


def test_path_validation_fails():
class Model(BaseModel):
foo: Path
Expand Down