Skip to content

Commit

Permalink
make most of mypy happy
Browse files Browse the repository at this point in the history
  • Loading branch information
thejaminator committed Jul 22, 2023
1 parent 1171abc commit 43dcf1d
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 14 deletions.
1 change: 1 addition & 0 deletions strawberry/experimental/pydantic/conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

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


def _convert_from_pydantic_to_strawberry_type(
Expand Down
7 changes: 2 additions & 5 deletions strawberry/experimental/pydantic/error_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
get_strawberry_type_from_model,
normalize_type,
)
from strawberry.experimental.pydantic.v2_compat import lenient_issubclass, get_model_fields
from strawberry.experimental.pydantic.v2_compat import lenient_issubclass, get_model_fields, CompatModelField
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
Expand All @@ -32,11 +32,8 @@

from .exceptions import MissingFieldsListError

if TYPE_CHECKING:
from pydantic.fields import ModelField


def get_type_for_field(field: ModelField) -> Union[Any, Type[None], Type[List]]:
def get_type_for_field(field: CompatModelField) -> Union[Any, Type[None], Type[List]]:
type_ = field.outer_type_
type_ = normalize_type(type_)
return field_type_to_type(type_)
Expand Down
2 changes: 1 addition & 1 deletion strawberry/experimental/pydantic/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

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

Expand Down
3 changes: 1 addition & 2 deletions strawberry/experimental/pydantic/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@

if TYPE_CHECKING:
from pydantic import BaseModel
from pydantic.fields import ModelField
from pydantic.typing import NoArgAnyCallable


Expand Down Expand Up @@ -72,7 +71,7 @@ def to_tuple(self) -> Tuple[str, Type, dataclasses.Field]:

def get_default_factory_for_field(
field: CompatModelField,
) -> Union[NoArgAnyCallable, PYDANTIC_MISSING_TYPE]:
) -> Union[NoArgAnyCallable, dataclasses._MISSING_TYPE]:
"""
Gets the default factory for a pydantic field.
Expand Down
20 changes: 14 additions & 6 deletions strawberry/experimental/pydantic/v2_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import pydantic
from pydantic import BaseModel
from pydantic.v1.typing import NoArgAnyCallable
from pydantic.version import VERSION as PYDANTIC_VERSION
from pydantic_core import PydanticUndefined

Expand All @@ -19,10 +18,10 @@ class CompatModelField:
default: Any
default_factory: Optional[Callable[[], Any]]
required: bool
alias: str
alias: Optional[str]
allow_none: bool
has_alias: bool
description: str
description: Optional[str]


if pydantic.VERSION[0] == "2":
Expand All @@ -33,7 +32,7 @@ class CompatModelField:
from pydantic.v1.fields import ModelField
from pydantic.fields import FieldInfo

PYDANTIC_MISSING_TYPE = PydanticUndefined
PYDANTIC_MISSING_TYPE: Type = PydanticUndefined

def new_type_supertype(type_):
return type_.__supertype__
Expand All @@ -58,7 +57,7 @@ def get_model_fields(model: Type[BaseModel]) -> Dict[str, CompatModelField]:
return new_fields

else:
from pydantic.utils import smart_deepcopy
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
Expand All @@ -83,4 +82,13 @@ def get_model_fields(model: Type[BaseModel]) -> Dict[str, CompatModelField]:
return new_fields


__all__ = ["smart_deepcopy", "lenient_issubclass"]
__all__ = [
"smart_deepcopy",
"lenient_issubclass",
"get_args",
"get_origin",
"is_new_type",
"new_type_supertype",
"get_model_fields",
"PYDANTIC_MISSING_TYPE",
]

0 comments on commit 43dcf1d

Please sign in to comment.