Skip to content

Commit

Permalink
Add more tests for Resolver type usage with field
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaspiatkowski committed Feb 15, 2024
1 parent e6a811a commit 1560cd3
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 1 deletion.
25 changes: 25 additions & 0 deletions tests/mypy/federation/test_fields.yml
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,28 @@
reveal_type(i.a)
out: |
main:14: note: Revealed type is "builtins.str"
- case: test_does_not_mistake_fields_with_resolvers_for_attributes
main: |
import strawberry
def some_resolver() -> str:
return ""
@strawberry.federation.type
class Example:
a: strawberry.Resolver[str] = strawberry.federation.field(name="a", resolver=some_resolver)
def test_reading(example: Example) -> str:
return example.a
def test_writing(example: Example, value: str) -> None:
example.a = value
def test_cant_instantiate_resolver() -> strawberry.Resolver[str]:
return strawberry.Resolver()
out: |
main:11: error: Incompatible return value type (got "Resolver[str]", expected "str") [return-value]
main:14: error: Incompatible types in assignment (expression has type "str", variable has type "Resolver[str]") [assignment]
main:17: error: Cannot instantiate abstract class "Resolver" with abstract attribute "__do_not_instantiate_this" [abstract]
25 changes: 25 additions & 0 deletions tests/mypy/test_fields.yml
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,28 @@
reveal_type(i.a)
out: |
main:14: note: Revealed type is "builtins.str"
- case: test_does_not_mistake_fields_with_resolvers_for_attributes
main: |
import strawberry
def some_resolver() -> str:
return ""
@strawberry.type
class Example:
a: strawberry.Resolver[str] = strawberry.field(name="a", resolver=some_resolver)
def test_reading(example: Example) -> str:
return example.a
def test_writing(example: Example, value: str) -> None:
example.a = value
def test_cant_instantiate_resolver() -> strawberry.Resolver[str]:
return strawberry.Resolver()
out: |
main:11: error: Incompatible return value type (got "Resolver[str]", expected "str") [return-value]
main:14: error: Incompatible types in assignment (expression has type "str", variable has type "Resolver[str]") [assignment]
main:17: error: Cannot instantiate abstract class "Resolver" with abstract attribute "__do_not_instantiate_this" [abstract]
38 changes: 37 additions & 1 deletion tests/pyright/test_federation.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def get_user_age() -> int:
@strawberry.federation.type
class User:
name: str
age: strawberry.Resolver[int] = strawberry.field(resolver=get_user_age)
age: strawberry.Resolver[int] = strawberry.federation.field(resolver=get_user_age)
something_else: strawberry.Resolver[int] = strawberry.federation.field(resolver=get_user_age)
Expand All @@ -22,6 +22,15 @@ class User:
reveal_type(User)
reveal_type(User.__init__)
def test_reading(user: User) -> int:
return user.age
def test_writing(user: User, value: int) -> None:
user.age = value
def test_cant_instantiate_resolver() -> strawberry.Resolver[int]:
return strawberry.Resolver()
"""


Expand All @@ -48,6 +57,33 @@ def test_federation_type():
line=19,
column=13,
),
Result(
type="error",
message=(
'Expression of type "Resolver[int]" cannot be assigned to return type "int"'
'\n\xa0\xa0"Resolver[int]" is incompatible with "int"'
),
line=22,
column=12,
),
Result(
type="error",
message=(
'Cannot assign member "age" for type "User"'
'\n\xa0\xa0"int" is incompatible with "Resolver[int]"'
),
line=25,
column=16,
),
Result(
type="error",
message=(
'Cannot instantiate abstract class "Resolver"'
'\n\xa0\xa0"Resolver.__do_not_instantiate_this" is not implemented'
),
line=28,
column=12,
),
]


Expand Down
36 changes: 36 additions & 0 deletions tests/pyright/test_fields_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ class User:
reveal_type(User)
reveal_type(User.__init__)
def test_reading(user: User) -> int:
return user.age
def test_writing(user: User, value: int) -> None:
user.age = value
def test_cant_instantiate_resolver() -> strawberry.Resolver[int]:
return strawberry.Resolver()
"""


Expand Down Expand Up @@ -51,4 +60,31 @@ def test_pyright():
line=18,
column=13,
),
Result(
type="error",
message=(
'Expression of type "Resolver[int]" cannot be assigned to return type "int"'
'\n\xa0\xa0"Resolver[int]" is incompatible with "int"'
),
line=21,
column=12,
),
Result(
type="error",
message=(
'Cannot assign member "age" for type "User"'
'\n\xa0\xa0"int" is incompatible with "Resolver[int]"'
),
line=24,
column=16,
),
Result(
type="error",
message=(
'Cannot instantiate abstract class "Resolver"'
'\n\xa0\xa0"Resolver.__do_not_instantiate_this" is not implemented'
),
line=27,
column=12,
),
]
36 changes: 36 additions & 0 deletions tests/pyright/test_fields_resolver_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ class User:
reveal_type(User)
reveal_type(User.__init__)
def test_reading(user: User) -> int:
return user.age
def test_writing(user: User, value: int) -> None:
user.age = value
def test_cant_instantiate_resolver() -> strawberry.Resolver[int]:
return strawberry.Resolver()
"""


Expand Down Expand Up @@ -51,4 +60,31 @@ def test_pyright():
line=18,
column=13,
),
Result(
type="error",
message=(
'Expression of type "Resolver[int]" cannot be assigned to return type "int"'
'\n\xa0\xa0"Resolver[int]" is incompatible with "int"'
),
line=21,
column=12,
),
Result(
type="error",
message=(
'Cannot assign member "age" for type "User"'
'\n\xa0\xa0"int" is incompatible with "Resolver[int]"'
),
line=24,
column=16,
),
Result(
type="error",
message=(
'Cannot instantiate abstract class "Resolver"'
'\n\xa0\xa0"Resolver.__do_not_instantiate_this" is not implemented'
),
line=27,
column=12,
),
]

0 comments on commit 1560cd3

Please sign in to comment.