Skip to content

Commit

Permalink
fix pydantic v2 update issues
Browse files Browse the repository at this point in the history
  • Loading branch information
thejaminator committed Jul 7, 2023
1 parent 064db83 commit 274f2b6
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 13 deletions.
6 changes: 3 additions & 3 deletions strawberry/experimental/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
try:
from . import pydantic

__all__ = ["pydantic"]
except ImportError:
pass
try:
from . import pydantic2

# Support for pydantic2 is highly experimental and the interface will change
# We don't recommend using it yet
__all__ = ["pydantic2"]
except ImportError as e:
print(e)
pass
4 changes: 2 additions & 2 deletions strawberry/experimental/pydantic2/object_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
cast,
)

from pydantic._internal._fields import Undefined
from pydantic._internal._fields import PydanticUndefined

from strawberry.annotation import StrawberryAnnotation
from strawberry.auto import StrawberryAuto
Expand All @@ -43,7 +43,7 @@


def is_required(field: FieldInfo) -> bool:
return field.default is Undefined and field.default_factory is None
return field.default is PydanticUndefined and field.default_factory is None


def get_type_for_field(field: FieldInfo, is_input: bool): # noqa: ANN201
Expand Down
10 changes: 5 additions & 5 deletions strawberry/experimental/pydantic2/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
cast,
)

from pydantic._internal._fields import Undefined, _UndefinedType
from pydantic_core import PydanticUndefinedType
from pydantic._internal._utils import smart_deepcopy

from strawberry.experimental.pydantic2.exceptions import (
Expand Down Expand Up @@ -77,11 +77,11 @@ def to_tuple(self) -> Tuple[str, Type, dataclasses.Field]:


def is_required(field: ModelField) -> bool:
return field.default is Undefined and field.default_factory is None
return field.default is PydanticUndefinedType and field.default_factory is None


def get_default_factory_for_field(
field: ModelField,
field: ModelField,
) -> Union[NoArgAnyCallable, dataclasses._MISSING_TYPE]:
"""
Gets the default factory for a pydantic field.
Expand All @@ -95,7 +95,7 @@ def get_default_factory_for_field(
default_factory = (
field.default_factory if field.default_factory is not None else UNSET
)
default = field.default if not isinstance(field.default, _UndefinedType) else UNSET
default = field.default if not isinstance(field.default, PydanticUndefinedType) else UNSET

has_factory = default_factory is not UNSET
has_default = default is not UNSET
Expand Down Expand Up @@ -131,7 +131,7 @@ def get_default_factory_for_field(


def ensure_all_auto_fields_in_pydantic(
model: Type[BaseModel], auto_fields: Set[str], cls_name: str
model: Type[BaseModel], auto_fields: Set[str], cls_name: str
) -> Union[NoReturn, None]:
# Raise error if user defined a strawberry.auto field not present in the model
non_existing_fields = list(auto_fields - model.model_fields.keys())
Expand Down
6 changes: 3 additions & 3 deletions tests/experimental/pydantic2/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,6 @@ class UserType:
[groups_field, friends_field] = definition.fields

assert groups_field.default is dataclasses.MISSING
assert groups_field.default_factory is dataclasses.MISSING
assert friends_field.default is dataclasses.MISSING

# check that we really made a copy
Expand Down Expand Up @@ -627,8 +626,9 @@ def updateGroup(group: GroupInput) -> GroupOutput:
type WorkOutput {
time: Float!
}"""
assert schema.as_str().strip() == expected_schema.strip()
}""".strip()
result_schema = schema.as_str().strip()
assert result_schema == expected_schema

assert Group._strawberry_type == GroupOutput
assert Group._strawberry_input_type == GroupInput
Expand Down

0 comments on commit 274f2b6

Please sign in to comment.