Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with LazyType/Enums and generics #1235

Merged
merged 8 commits into from
Sep 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ extend-ignore =
E203,

per-file-ignores =
tests/types/test_lazy_types.py:E800
tests/test_forward_references.py:E800
tests/schema/test_resolvers.py:E800
tests/types/test_string_annotations.py:E800
Expand Down
51 changes: 51 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
Release type: patch

Fix issues ([#1158][issue1158] and [#1104][issue1104]) where Generics using LazyTypes
and Enums would not be properly resolved

These now function as expected:

# Enum

```python
T = TypeVar("T")

@strawberry.enum
class VehicleMake(Enum):
FORD = 'ford'
TOYOTA = 'toyota'
HONDA = 'honda'

@strawberry.type
class GenericForEnum(Generic[T]):
generic_slot: T

@strawberry.type
class SomeType:
field: GenericForEnum[VehicleMake]
```

# LazyType

`another_file.py`
```python
@strawberry.type
class TypeFromAnotherFile:
something: bool
```

`this_file.py`
```python
T = TypeVar("T")

@strawberry.type
class GenericType(Generic[T]):
item: T

@strawberry.type
class RealType:
lazy: GenericType[LazyType["TypeFromAnotherFile", "another_file.py"]]
```

[issue1104]: https://github.com/strawberry-graphql/strawberry/issues/1104
[issue1158]: https://github.com/strawberry-graphql/strawberry/issues/1158
7 changes: 7 additions & 0 deletions TWEET.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
🆕 Release $version is out! Thanks to @ignormies for the PR 👏

Get it here 👉 $release_url

---

This release fixes a couple of issues related to generics, `LazyTypes` and `Enums`
8 changes: 7 additions & 1 deletion strawberry/types/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
Union,
)

from strawberry.enum import EnumDefinition
from strawberry.lazy_type import LazyType
from strawberry.type import StrawberryType, StrawberryTypeVar
from strawberry.utils.str_converters import capitalize_first
from strawberry.utils.typing import is_generic as is_type_generic
Expand Down Expand Up @@ -117,7 +119,11 @@ def get_name_from_types(self, types: Iterable[Union[StrawberryType, type]]) -> s
names: List[str] = []

for type_ in types:
if isinstance(type_, StrawberryUnion):
if isinstance(type_, LazyType):
name = type_.type_name
elif isinstance(type_, EnumDefinition):
name = type_.name
elif isinstance(type_, StrawberryUnion):
name = type_.name
elif hasattr(type_, "_type_definition"):
field_type = type_._type_definition # type: ignore
Expand Down
28 changes: 28 additions & 0 deletions tests/types/resolving/test_generics.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from enum import Enum
from typing import Generic, List, Optional, TypeVar, Union

import strawberry
from strawberry.annotation import StrawberryAnnotation
from strawberry.enum import EnumDefinition
from strawberry.field import StrawberryField
from strawberry.type import StrawberryList, StrawberryOptional, StrawberryTypeVar
from strawberry.types.types import TypeDefinition
Expand Down Expand Up @@ -80,3 +82,29 @@ def test_generic_unions():
assert resolved.is_generic

assert resolved == Union[S, T]


def test_generic_with_enums():
T = TypeVar("T")

@strawberry.enum
class VehicleMake(Enum):
FORD = "ford"
TOYOTA = "toyota"
HONDA = "honda"

@strawberry.type
class GenericForEnum(Generic[T]):
generic_slot: T

annotation = StrawberryAnnotation(GenericForEnum[VehicleMake])
resolved = annotation.resolve()

# TODO: Simplify with StrawberryObject
assert isinstance(resolved, type)
assert hasattr(resolved, "_type_definition")
assert isinstance(resolved._type_definition, TypeDefinition)

generic_slot_field: StrawberryField = resolved._type_definition.fields[0]
assert isinstance(generic_slot_field.type, EnumDefinition)
assert generic_slot_field.type is VehicleMake._enum_definition
28 changes: 28 additions & 0 deletions tests/types/test_lazy_types.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# type: ignore
from typing import Generic, TypeVar

import strawberry
from strawberry.annotation import StrawberryAnnotation
from strawberry.field import StrawberryField
from strawberry.lazy_type import LazyType
from strawberry.types.fields.resolver import StrawberryResolver
from strawberry.types.types import TypeDefinition


# This type is in the same file but should adequately test the logic.
Expand Down Expand Up @@ -53,6 +57,30 @@ def test_lazy_type_field():
assert field.type.resolve_type() is LaziestType # type: ignore


def test_lazy_type_generic():
T = TypeVar("T")

@strawberry.type
class GenericType(Generic[T]):
item: T

# Module path is short and relative because of the way pytest runs the file
LazierType = LazyType["LaziestType", "test_lazy_types"]
ResolvedType = GenericType[LazierType]

annotation = StrawberryAnnotation(ResolvedType)
resolved = annotation.resolve()

# TODO: Simplify with StrawberryObject
assert isinstance(resolved, type)
assert hasattr(resolved, "_type_definition")
assert isinstance(resolved._type_definition, TypeDefinition)

items_field: StrawberryField = resolved._type_definition.fields[0]
assert items_field.type is LazierType
assert items_field.type.resolve_type() is LaziestType


def test_lazy_type_object():
# Module path is short and relative because of the way pytest runs the file
LazierType = LazyType["LaziestType", "test_lazy_types"]
Expand Down