Skip to content

Commit

Permalink
feat: support pydantic-compat v0.2. (#236)
Browse files Browse the repository at this point in the history
  • Loading branch information
tlambert03 authored Dec 17, 2023
1 parent 9a16286 commit 994fe37
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
11 changes: 7 additions & 4 deletions src/ome_types/_pydantic_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ def field_type(field: FieldInfo) -> Any:

def field_regex(obj: type[BaseModel], field_name: str) -> str | None:
field_info = obj.model_fields[field_name]
if field_info.json_schema_extra and isinstance(
field_info.json_schema_extra, dict
):
return field_info.json_schema_extra.get("pattern") # type: ignore
meta = field_info.json_schema_extra or {}
# if a "metadata" key exists... use it.
# After pydantic-compat 0.2, this is where it will be.
if "metadata" in meta: # type: ignore
meta = meta["metadata"] # type: ignore
if meta:
return meta.get("pattern") # type: ignore
return None

def get_default(f: FieldInfo) -> Any:
Expand Down
7 changes: 6 additions & 1 deletion src/xsdata_pydantic_basemodel/pydantic_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,16 @@
from pydantic_core import PydanticUndefined as Undefined

def _get_metadata(pydantic_field: FieldInfo) -> dict[str, Any]:
return (
meta = (
pydantic_field.json_schema_extra
if isinstance(pydantic_field.json_schema_extra, dict)
else {}
)
# if a "metadata" key exists... use it.
# After pydantic-compat 0.2, this is where it will be.
if "metadata" in meta:
meta = meta["metadata"] # type: ignore
return meta

else:
from pydantic.fields import Undefined as Undefined # type: ignore
Expand Down

0 comments on commit 994fe37

Please sign in to comment.