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

build: update pydantic compat to v0.1.0 #224

Merged
merged 2 commits into from
Sep 26, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ classifiers = [
dynamic = ["version"]
dependencies = [
"pydantic >=1.9.0",
"pydantic-compat >=0.0.1",
"pydantic-compat >=0.1.0",
"xsdata >=23.5", # 23.6 necessary for codegen, but not for runtime
"importlib_metadata; python_version < '3.8'",
]
Expand Down
42 changes: 8 additions & 34 deletions src/xsdata_pydantic_basemodel/pydantic_compat.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,23 @@
from __future__ import annotations

from dataclasses import MISSING, field
from typing import TYPE_CHECKING, Any, Callable, TypeVar
from typing import TYPE_CHECKING, Any, Callable, Iterator, TypeVar

from pydantic import BaseModel, version
from pydantic_compat import PYDANTIC2, Field

if TYPE_CHECKING:
from typing import Iterator

from pydantic import Field
__all__ = ["Field", "PYDANTIC2"]

if TYPE_CHECKING:
from pydantic import BaseModel

__all__ = ["Field"]
PYDANTIC2 = version.VERSION.startswith("2")
M = TypeVar("M", bound=BaseModel)
C = TypeVar("C", bound=Callable[..., Any])
M = TypeVar("M", bound=BaseModel)
C = TypeVar("C", bound=Callable[..., Any])


if PYDANTIC2:
from pydantic.fields import Field as _Field
from pydantic.fields import FieldInfo
from pydantic_core import PydanticUndefined as Undefined

def Field(*args: Any, **kwargs: Any) -> Any: # type: ignore
if "metadata" in kwargs:
kwargs["json_schema_extra"] = kwargs.pop("metadata")
if "regex" in kwargs:
kwargs["pattern"] = kwargs.pop("regex")
if "min_items" in kwargs:
kwargs["min_length"] = kwargs.pop("min_items")
return _Field(*args, **kwargs) # type: ignore

def _get_metadata(pydantic_field: FieldInfo) -> dict[str, Any]:
return (
pydantic_field.json_schema_extra
Expand All @@ -39,14 +26,8 @@ def _get_metadata(pydantic_field: FieldInfo) -> dict[str, Any]:
)

else:
from pydantic.fields import Field as _Field
from pydantic.fields import Undefined as Undefined # type: ignore

def Field(*args: Any, **kwargs: Any) -> Any: # type: ignore
if "metadata" in kwargs:
kwargs["json_schema_extra"] = kwargs.pop("metadata")
return _Field(*args, **kwargs) # type: ignore

def _get_metadata(pydantic_field) -> dict: # type: ignore
extra = pydantic_field.field_info.extra
if "json_schema_extra" in extra:
Expand Down Expand Up @@ -74,16 +55,9 @@ def _pydantic_field_to_dataclass_field(name: str, pydantic_field: FieldInfo) ->
metadata = _get_metadata(pydantic_field)

dataclass_field = field( # type: ignore
default=default,
default_factory=default_factory,
# init=True,
# hash=None,
# compare=True,
metadata=metadata,
# kw_only=MISSING,
default=default, default_factory=default_factory, metadata=metadata
)
dataclass_field.name = name
# dataclass_field.type = pydantic_field.type_
return dataclass_field


Expand Down