forked from strawberry-graphql/strawberry
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pydantic v2 support (strawberry-graphql#2972)
* work so far * add modelfield * it works kinda * fix missing type * fix default tesT * skip tests for constrained types * add tests pass? * ruff * Revert "ruff" This reverts commit dc0b9dd. * make most of mypy happy * make linters and mypy happy * fix pydantic v1 import * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * remove extra ruff * fix tests for pydantic v1 * remove weird stuff ruff added * add release file * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * bump pydantic in pyproject.toml * upgrade pydantic lock file * remove unused * fix mypy * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix poetry * remove implicit default test * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * force pydantic 1.10 * Update noxfile.py Co-authored-by: Patrick Arminio <[email protected]> * Update tests/experimental/pydantic/schema/test_defaults.py Co-authored-by: Patrick Arminio <[email protected]> * rename v2_compat -> _compat * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * ignore mypy windows tests --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Patrick Arminio <[email protected]>
- Loading branch information
Showing
18 changed files
with
566 additions
and
195 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Release type: minor | ||
|
||
Adds initial support for pydantic V2. | ||
|
||
This is extremely experimental for wider initial testing. | ||
|
||
We do not encourage using this in production systems yet. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import dataclasses | ||
from dataclasses import dataclass | ||
from typing import TYPE_CHECKING, Any, Callable, Dict, Optional, Type | ||
|
||
import pydantic | ||
from pydantic import BaseModel | ||
from pydantic.version import VERSION as PYDANTIC_VERSION | ||
|
||
if TYPE_CHECKING: | ||
from pydantic.fields import FieldInfo | ||
|
||
IS_PYDANTIC_V2: bool = PYDANTIC_VERSION.startswith("2.") | ||
IS_PYDANTIC_V1: bool = not IS_PYDANTIC_V2 | ||
|
||
|
||
@dataclass | ||
class CompatModelField: | ||
name: str | ||
type_: Any | ||
outer_type_: Any | ||
default: Any | ||
default_factory: Optional[Callable[[], Any]] | ||
required: bool | ||
alias: Optional[str] | ||
allow_none: bool | ||
has_alias: bool | ||
description: Optional[str] | ||
|
||
|
||
if pydantic.VERSION[0] == "2": | ||
from typing_extensions import get_args, get_origin | ||
|
||
from pydantic._internal._typing_extra import is_new_type | ||
from pydantic._internal._utils import lenient_issubclass, smart_deepcopy | ||
from pydantic_core import PydanticUndefined | ||
|
||
PYDANTIC_MISSING_TYPE = PydanticUndefined | ||
|
||
def new_type_supertype(type_: Any) -> Any: | ||
return type_.__supertype__ | ||
|
||
def get_model_fields(model: Type[BaseModel]) -> Dict[str, CompatModelField]: | ||
field_info: dict[str, FieldInfo] = model.model_fields | ||
new_fields = {} | ||
# Convert it into CompatModelField | ||
for name, field in field_info.items(): | ||
new_fields[name] = CompatModelField( | ||
name=name, | ||
type_=field.annotation, | ||
outer_type_=field.annotation, | ||
default=field.default, | ||
default_factory=field.default_factory, | ||
required=field.is_required(), | ||
alias=field.alias, | ||
# v2 doesn't have allow_none | ||
allow_none=False, | ||
has_alias=field is not None, | ||
description=field.description, | ||
) | ||
return new_fields | ||
|
||
else: | ||
from pydantic.typing import ( # type: ignore[no-redef] | ||
get_args, | ||
get_origin, | ||
is_new_type, | ||
new_type_supertype, | ||
) | ||
from pydantic.utils import ( # type: ignore[no-redef] | ||
lenient_issubclass, | ||
smart_deepcopy, | ||
) | ||
|
||
PYDANTIC_MISSING_TYPE = dataclasses.MISSING # type: ignore[assignment] | ||
|
||
def get_model_fields(model: Type[BaseModel]) -> Dict[str, CompatModelField]: | ||
new_fields = {} | ||
# Convert it into CompatModelField | ||
for name, field in model.__fields__.items(): # type: ignore[attr-defined] | ||
new_fields[name] = CompatModelField( | ||
name=name, | ||
type_=field.type_, | ||
outer_type_=field.outer_type_, | ||
default=field.default, | ||
default_factory=field.default_factory, | ||
required=field.required, | ||
alias=field.alias, | ||
allow_none=field.allow_none, | ||
has_alias=field.has_alias, | ||
description=field.field_info.description, | ||
) | ||
return new_fields | ||
|
||
|
||
__all__ = [ | ||
"smart_deepcopy", | ||
"lenient_issubclass", | ||
"get_args", | ||
"get_origin", | ||
"is_new_type", | ||
"new_type_supertype", | ||
"get_model_fields", | ||
"PYDANTIC_MISSING_TYPE", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.