Skip to content

Commit

Permalink
Make some typing errors go away
Browse files Browse the repository at this point in the history
  • Loading branch information
Mapiarz committed Jun 26, 2024
1 parent 690353a commit 10b9656
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
4 changes: 2 additions & 2 deletions strawberry_django/fields/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
_QS = TypeVar("_QS", bound="models.QuerySet")

try:
from django.db.models.fields.generated import GeneratedField
from django.db.models import GeneratedField # type: ignore
except ImportError:
GeneratedField = None

Expand Down Expand Up @@ -207,7 +207,7 @@ def resolve_type(
self.origin_django_type,
)
if is_optional(
model_field.output_field
model_field.output_field # type: ignore
if GeneratedField is not None
and isinstance(model_field, GeneratedField)
else model_field,
Expand Down
4 changes: 2 additions & 2 deletions strawberry_django/fields/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
TextChoicesField = None

try:
from django.db.models.fields.generated import GeneratedField
from django.db.models import GeneratedField # type: ignore
except ImportError:
GeneratedField = None

Expand Down Expand Up @@ -476,7 +476,7 @@ def resolve_model_field_type(
model_field._strawberry_enum = field_type # type: ignore
# Generated fields
elif GeneratedField is not None and isinstance(model_field, GeneratedField):
model_field_type = type(model_field.output_field)
model_field_type = type(model_field.output_field) # type: ignore
field_type = field_type_map.get(model_field_type, NotImplemented)
# Every other Field possibility
else:
Expand Down
17 changes: 11 additions & 6 deletions tests/fields/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import decimal
import enum
import uuid
from typing import Dict, List, Optional, Tuple, Union, cast
from typing import Any, Dict, List, Optional, Tuple, Union, cast

import pytest
import strawberry
Expand All @@ -23,6 +23,11 @@
from strawberry_django.fields.field import StrawberryDjangoField
from strawberry_django.type import _process_type

try:
from django.db.models import GeneratedField # type: ignore
except ImportError:
GeneratedField = None


class FieldTypesModel(models.Model):
boolean = models.BooleanField()
Expand All @@ -48,21 +53,21 @@ class FieldTypesModel(models.Model):
uuid = models.UUIDField()
json = models.JSONField()
generated_decimal = (
models.GeneratedField(
GeneratedField(
expression=models.F("decimal") * 2,
db_persist=True,
output_field=models.DecimalField(),
)
if hasattr(models, "GeneratedField")
if GeneratedField is not None
else None
)
generated_nullable_decimal = (
models.GeneratedField(
GeneratedField(
expression=models.F("decimal") * 2,
db_persist=True,
output_field=models.DecimalField(null=True, blank=True),
)
if hasattr(models, "GeneratedField")
if GeneratedField is not None
else None
)
foreign_key = models.ForeignKey(
Expand Down Expand Up @@ -110,7 +115,7 @@ class Type:
uuid: auto
json: auto

expected_types = [
expected_types: list[tuple[str, Any]] = [
("id", strawberry.ID),
("boolean", bool),
("char", str),
Expand Down

0 comments on commit 10b9656

Please sign in to comment.