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

Improve mypy support for create_type #1175

Merged
merged 3 commits into from
Aug 26, 2021
Merged

Improve mypy support for create_type #1175

merged 3 commits into from
Aug 26, 2021

Conversation

patrick91
Copy link
Member

MyPy unfortunately doesn't allow using classes created from functions as base classes, so
for example this code would fail:

import strawberry
from strawberry.tools import create_type

@strawberry.field
def name() -> str:
    return "foo"

MyType = create_type("MyType", [name])

class Query(MyType):
    ...

with this error:

E     main:10: error: Invalid base class "MyType"   (diff)
E     main:10: error: Variable "main.MyType" is not valid as a type (diff)
E     main:10: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases (diff)
E     main:13: note: Revealed type is "def (*_args: Any, **_kwds: Any) -> main.Query" (diff)

So I've changed the plugin to tell mypy that the return type is a type alias and it fixes the issue 😊

@codecov
Copy link

codecov bot commented Aug 26, 2021

Codecov Report

Merging #1175 (a86f3c9) into main (d9ea4c3) will not change coverage.
The diff coverage is n/a.

@@           Coverage Diff           @@
##             main    #1175   +/-   ##
=======================================
  Coverage   97.45%   97.45%           
=======================================
  Files          88       88           
  Lines        3296     3296           
  Branches      473      473           
=======================================
  Hits         3212     3212           
  Misses         46       46           
  Partials       38       38           

@botberry
Copy link
Member

Thanks for adding the RELEASE.md file!

Here's a preview of the changelog:


This releases fixes a MyPy issue that prevented from using types created with
create_type as base classes. This is now allowed and doesn't throw any error:

import strawberry
from strawberry.tools import create_type

@strawberry.field
def name() -> str:
    return "foo"

MyType = create_type("MyType", [name])

class Query(MyType):
    ...

Here's the preview release card for twitter:

Here's the tweet text:

🆕 Release (next) is out! Thanks to Patrick Arminio for the PR 👏

Get it here 👉 https://github.com/strawberry-graphql/strawberry/releases/tag/(next)

@botberry
Copy link
Member

Thanks for adding the RELEASE.md file!

Here's a preview of the changelog:


This releases fixes a MyPy issue that prevented from using types created with
create_type as base classes. This is now allowed and doesn't throw any error:

import strawberry
from strawberry.tools import create_type

@strawberry.field
def name() -> str:
    return "foo"

MyType = create_type("MyType", [name])

class Query(MyType):
    ...

Here's the preview release card for twitter:

Here's the tweet text:

🆕 Release (next) is out! Thanks to Patrick Arminio for the PR 👏

Get it here 👉 https://github.com/strawberry-graphql/strawberry/releases/tag/(next)

@patrick91 patrick91 merged commit 2097f78 into main Aug 26, 2021
@patrick91 patrick91 deleted the fix/mypy-create-type branch August 26, 2021 09:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants