-
-
Notifications
You must be signed in to change notification settings - Fork 536
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 strawberry.enum typing by using generics #1568
Improve strawberry.enum typing by using generics #1568
Conversation
I'm not sure whether tests are necessary for this change. Please do tell me if they are -- I'm not sure how/where to put them, if so. |
Thanks for adding the Here's a preview of the changelog: Improve typing of
@strawberry.enum
class IceCreamFlavour(Enum):
VANILLA = "vanilla"
STRAWBERRY = "strawberry"
CHOCOLATE = "chocolate" Prior to this change, pyright would complain if you tried to access
Here's the tweet text:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! If you want to add tests, we have some mypy tests under tests/mypy
:
https://github.com/strawberry-graphql/strawberry/blob/main/tests/mypy/test_enum.yml
Codecov Report
@@ Coverage Diff @@
## main #1568 +/- ##
=======================================
Coverage 98.11% 98.11%
=======================================
Files 130 130
Lines 4467 4468 +1
Branches 758 758
=======================================
+ Hits 4383 4384 +1
Misses 44 44
Partials 40 40 |
Also use a TypeVar so that the enum fields can be detected by the typechecker.
a46e682
to
76e014d
Compare
Added a pair of tests for it! It was hard to find a failing test, since mypy (unlike pyright, which is why I noticed this issue in VSCode) seems to have some special-inference for decorators. |
@TimDumol this looks awesome! I'm going to add a test for Pyright too if you don't mind 😊 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great improvement! <3
Thanks for contributing to Strawberry! 🎉 You've been invited to join You can also request a free sticker by filling this form: https://forms.gle/dmnfQUPoY5gZbVT67 And don't forget to join our discord server: https://strawberry.rocks/discord 🔥 |
Description
TypeVar
bound onEnumMeta
instead ofEnumMeta
, which allows type-checkers (like pyright)to detect the fields of the enum being decorated. For example, for the following enum:
Prior to this change, pyright would complain if you tried to access
IceCreamFlavour.VANILLA
, since the type information ofIceCreamFlavour
was beingerased by the
EnumMeta
typing .(when it's called with keyword arguments, e.g.
@strawberry.enum(name="IceCreamFlavor")
),versus when it returns the original enum type (without keyword arguments.
Types of Changes
Issues Fixed or Closed by This PR
NA
Checklist