strawberry icon indicating copy to clipboard operation
strawberry copied to clipboard

WIP - Fix `enum_value`

Open patrick91 opened this issue 3 years ago • 1 comments

This PR will fix an issue that doesn't allow you from using Enums that use enum_value for defining their value. The issue comes from Enum(...) trying to find the enum value using it's value, but when using strawberry.enum_value the value is the enum value definition.

We can fix this by changing the value or by adding an __eq__ method, not sure which one is better.

The PR has only the tests :)

patrick91 avatar Sep 20 '22 12:09 patrick91

Codecov Report

Merging #2185 (9cf63a8) into main (76cf1d5) will not change coverage. The diff coverage is n/a.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2185   +/-   ##
=======================================
  Coverage   98.19%   98.19%           
=======================================
  Files         162      162           
  Lines        6492     6492           
  Branches     1236     1236           
=======================================
  Hits         6375     6375           
  Misses         58       58           
  Partials       59       59           

codecov[bot] avatar Sep 20 '22 12:09 codecov[bot]

I think there is a better solution: a subclass of an enum and using class keyword arguments. Also with adapter function

would look like:

class Foo(strawberry.Enum, description="foo")

and for existing enum (metadata like description is added via function)

strawberry.Enum.adapt(enum, adapt_fn=foo)

Edit: I changed the function parameter name

devkral avatar Sep 25 '22 18:09 devkral

in some way like the graphene Enum

devkral avatar Sep 26 '22 06:09 devkral

I will upload a draft

devkral avatar Sep 26 '22 10:09 devkral

see #2203

can be used like

from strawberry import StrawberryEnum
class Foo(str, StrawberryEnum, name="j"):
  a = "test"

Limitation: needs type

devkral avatar Sep 26 '22 11:09 devkral

the limitation has been lifted and it seems that only some small issues remain

Is there interest in my solution? Shall I continue working on it?

anyway: to remove the EnumValueDefinition you can create an enum subclass by enum_value. this will be directly used instead of creating an extra Value.

devkral avatar Oct 15 '22 13:10 devkral

@devkral I'm really sorry I haven't answered you earlier!

I think I found an easier solution in #2306, let me know what you think

patrick91 avatar Nov 02 '22 23:11 patrick91