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

Pydantic V2 support #13

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion datauri/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def __get_validators__(cls):
yield cls.validate

@classmethod
def validate(cls, v: str) -> Self:
def validate(cls, v: str, x: Any = None) -> Self:
if not isinstance(v, str):
raise TypeError("string required")

Expand All @@ -169,6 +169,16 @@ def validate(cls, v: str) -> Self:
raise ValueError("invalid data-uri format")
return m

@classmethod
def __get_pydantic_json_schema__(cls, core_schema: Any, handler: Any) -> Any:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't core_schema have type Mapping[str, Any], as in pydantic_core?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, added mapping Dict

core_schema.update(
pattern=DATA_URI_REGEX,
examples=[
"data:text/plain;charset=utf-8;base64,VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6eSBkb2cu"
],
)
return core_schema

@classmethod
def __modify_schema__(cls, field_schema: Dict[str, Any]) -> None:
# __modify_schema__ should mutate the dict it receives in place,
Expand Down
15 changes: 11 additions & 4 deletions tests/test_pydantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@

pydantic = pytest.importorskip("pydantic")

try:
pydantic.version.version_info()
func_json = "model_dump_json"
func_dict = "model_dump"
except AttributeError:
func_json = "json"
func_dict = "dict"


def test_pydantic():
class Model(pydantic.BaseModel):
Expand All @@ -13,8 +21,7 @@ class Model(pydantic.BaseModel):
instance = Model(content=t)
assert isinstance(instance.content, DataURI)
assert (
instance.json()
== '{"content": "data:text/plain;charset=utf-8;base64,VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0'
'aGUgbGF6eSBkb2cu"}'
instance.__getattr__(func_json)()
== '{"content":"data:text/plain;charset=utf-8;base64,VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6eSBkb2cu"}'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we test both?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know how to test for this edge case

)
assert instance.dict() == {"content": DataURI(t)}
assert instance.__getattr__(func_dict)() == {"content": DataURI(t)}