Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Jul 22, 2023
1 parent 43dcf1d commit c1566a0
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 43 deletions.
2 changes: 1 addition & 1 deletion strawberry/experimental/pydantic/conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
from strawberry.union import StrawberryUnion

if TYPE_CHECKING:
from strawberry.type import StrawberryType
from strawberry.field import StrawberryField
from strawberry.type import StrawberryType


def _convert_from_pydantic_to_strawberry_type(
Expand Down
25 changes: 12 additions & 13 deletions strawberry/experimental/pydantic/error_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import dataclasses
import warnings
from typing import (
TYPE_CHECKING,
Any,
Callable,
List,
Expand All @@ -23,13 +22,15 @@
get_strawberry_type_from_model,
normalize_type,
)
from strawberry.experimental.pydantic.v2_compat import lenient_issubclass, get_model_fields, CompatModelField
from strawberry.experimental.pydantic.v2_compat import (
CompatModelField,
get_model_fields,
lenient_issubclass,
)
from strawberry.object_type import _process_type, _wrap_dataclass
from strawberry.types.type_resolver import _get_fields
from strawberry.utils.typing import get_list_annotation, is_list



from .exceptions import MissingFieldsListError


Expand All @@ -39,8 +40,6 @@ def get_type_for_field(field: CompatModelField) -> Union[Any, Type[None], Type[L
return field_type_to_type(type_)




def field_type_to_type(type_: Type) -> Union[Any, List[Any], None]:
error_class: Any = str
strawberry_type: Any = error_class
Expand All @@ -64,13 +63,13 @@ def field_type_to_type(type_: Type) -> Union[Any, List[Any], None]:


def error_type(
model: Type[BaseModel],
*,
fields: Optional[List[str]] = None,
name: Optional[str] = None,
description: Optional[str] = None,
directives: Optional[Sequence[object]] = (),
all_fields: bool = False,
model: Type[BaseModel],
*,
fields: Optional[List[str]] = None,
name: Optional[str] = None,
description: Optional[str] = None,
directives: Optional[Sequence[object]] = (),
all_fields: bool = False,
) -> Callable[..., Type]:
def wrap(cls: Type) -> Type:
model_fields = get_model_fields(model)
Expand Down
18 changes: 9 additions & 9 deletions strawberry/experimental/pydantic/fields.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import builtins
import types
from decimal import Decimal
from typing import Any, List, Optional, Type, Union
from uuid import UUID

import pydantic
from pydantic import BaseModel

from strawberry.experimental.pydantic.exceptions import (
UnregisteredTypeException,
UnsupportedTypeError,
)
from strawberry.experimental.pydantic.v2_compat import (
lenient_issubclass,
IS_PYDANTIC_V2,
get_args,
get_origin,
is_new_type,
lenient_issubclass,
new_type_supertype,
IS_PYDANTIC_V2,
)
from strawberry.experimental.pydantic.exceptions import (
UnregisteredTypeException,
UnsupportedTypeError,
)
from strawberry.types.types import StrawberryObjectDefinition

try:
from typing import GenericAlias as TypingGenericAlias # type: ignore
from types import UnionType as TypingUnionType
from typing import GenericAlias as TypingGenericAlias # type: ignore
except ImportError:
import sys

Expand Down Expand Up @@ -82,7 +82,7 @@
}

"""TODO:
Most of these fields are not supported by pydantic V2
Most of these fields are not supported by pydantic V2
"""
FIELDS_MAP = (
{
Expand Down
7 changes: 5 additions & 2 deletions strawberry/experimental/pydantic/object_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@
get_default_factory_for_field,
get_private_fields,
)
from strawberry.experimental.pydantic.v2_compat import CompatModelField, get_model_fields, IS_PYDANTIC_V2
from strawberry.experimental.pydantic.v2_compat import (
IS_PYDANTIC_V2,
CompatModelField,
get_model_fields,
)
from strawberry.field import StrawberryField
from strawberry.object_type import _process_type, _wrap_dataclass
from strawberry.types.type_resolver import _get_fields
Expand All @@ -40,7 +44,6 @@
from graphql import GraphQLResolveInfo



def get_type_for_field(field: CompatModelField, is_input: bool): # noqa: ANN201
outer_type = field.outer_type_
replaced_type = replace_types_recursively(outer_type, is_input)
Expand Down
9 changes: 6 additions & 3 deletions strawberry/experimental/pydantic/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@
cast,
)


from strawberry.experimental.pydantic.exceptions import (
AutoFieldsNotInBaseModelError,
BothDefaultAndDefaultFactoryDefinedError,
UnregisteredTypeException,
)
from strawberry.experimental.pydantic.v2_compat import CompatModelField, smart_deepcopy, PYDANTIC_MISSING_TYPE, \
get_model_fields
from strawberry.experimental.pydantic.v2_compat import (
PYDANTIC_MISSING_TYPE,
CompatModelField,
get_model_fields,
smart_deepcopy,
)
from strawberry.private import is_private
from strawberry.unset import UNSET
from strawberry.utils.typing import (
Expand Down
18 changes: 10 additions & 8 deletions strawberry/experimental/pydantic/v2_compat.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import dataclasses
from dataclasses import dataclass
from typing import Dict, Type, Any, Optional, Callable
from typing import TYPE_CHECKING, Any, Callable, Dict, Optional, Type

import pydantic
from pydantic import BaseModel
from pydantic.version import VERSION as PYDANTIC_VERSION
from pydantic_core import PydanticUndefined

if TYPE_CHECKING:
from pydantic.fields import FieldInfo

IS_PYDANTIC_V2: bool = PYDANTIC_VERSION.startswith("2.")
IS_PYDANTIC_V1: bool = not IS_PYDANTIC_V2

Expand All @@ -25,12 +28,10 @@ class CompatModelField:


if pydantic.VERSION[0] == "2":
from pydantic._internal._utils import smart_deepcopy
from pydantic._internal._utils import lenient_issubclass
from typing_extensions import get_args, get_origin

from pydantic._internal._typing_extra import is_new_type
from pydantic.v1.fields import ModelField
from pydantic.fields import FieldInfo
from pydantic._internal._utils import lenient_issubclass, smart_deepcopy

PYDANTIC_MISSING_TYPE: Type = PydanticUndefined

Expand All @@ -57,10 +58,11 @@ def get_model_fields(model: Type[BaseModel]) -> Dict[str, CompatModelField]:
return new_fields

else:
from pydantic.utils import smart_deepcopy # type: ignore
from pydantic.utils import lenient_issubclass
from pydantic.typing import get_args, get_origin, is_new_type, new_type_supertype
from pydantic import ModelField
from pydantic.utils import (
lenient_issubclass,
smart_deepcopy, # type: ignore
)

PYDANTIC_MISSING_TYPE = dataclasses.MISSING

Expand Down
10 changes: 5 additions & 5 deletions tests/experimental/pydantic/test_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import re
import sys
from enum import Enum
from typing import Any, Dict, List, NewType, Optional, Union, cast
from typing import Any, Dict, List, NewType, Optional, Union

import pytest
from pydantic import BaseConfig, BaseModel, Field, ValidationError
from pydantic import BaseModel, Field, ValidationError

import strawberry
from strawberry.experimental.pydantic.exceptions import (
Expand All @@ -16,16 +16,16 @@
from strawberry.experimental.pydantic.utils import get_default_factory_for_field
from strawberry.experimental.pydantic.v2_compat import (
IS_PYDANTIC_V2,
CompatModelField,
PYDANTIC_MISSING_TYPE,
CompatModelField,
)
from strawberry.type import StrawberryList, StrawberryOptional
from strawberry.types.types import StrawberryObjectDefinition

if IS_PYDANTIC_V2:
from pydantic.v1.fields import ModelField
pass
else:
from pydantic.fields import ModelField
pass


def test_can_use_type_standalone():
Expand Down
2 changes: 1 addition & 1 deletion tests/experimental/pydantic/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from strawberry.experimental.pydantic.v2_compat import IS_PYDANTIC_V1
from strawberry.type import StrawberryOptional
from strawberry.types.types import StrawberryObjectDefinition
from tests.experimental.pydantic.utils import needs_pydanticv2, needs_pydanticv1
from tests.experimental.pydantic.utils import needs_pydanticv1, needs_pydanticv2


@needs_pydanticv1
Expand Down
2 changes: 1 addition & 1 deletion tests/experimental/pydantic/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
from strawberry.experimental.pydantic.v2_compat import IS_PYDANTIC_V2

needs_pydanticv2 = pytest.mark.skipif(not IS_PYDANTIC_V2, reason="requires Pydantic v2")
needs_pydanticv1 = pytest.mark.skipif(IS_PYDANTIC_V2, reason="requires Pydantic v1")
needs_pydanticv1 = pytest.mark.skipif(IS_PYDANTIC_V2, reason="requires Pydantic v1")

0 comments on commit c1566a0

Please sign in to comment.