Skip to content

Commit

Permalink
Make strict config overridable in field for Path (#7281)
Browse files Browse the repository at this point in the history
  • Loading branch information
hramezani committed Aug 29, 2023
1 parent 966ea40 commit 427e9e6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
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

0 comments on commit 427e9e6

Please sign in to comment.