mirror of
https://github.com/graphql-python/graphene.git
synced 2025-07-18 20:12:22 +03:00
update EnumOptions and EnumMeta to support inheritance
This commit is contained in:
parent
5e050a6800
commit
ba6b46088b
|
@ -1,4 +1,5 @@
|
||||||
from enum import Enum as PyEnum
|
from enum import Enum as PyEnum
|
||||||
|
from typing import Iterable, Type, Optional
|
||||||
|
|
||||||
from graphene.utils.subclass_with_meta import SubclassWithMeta_Meta
|
from graphene.utils.subclass_with_meta import SubclassWithMeta_Meta
|
||||||
|
|
||||||
|
@ -18,11 +19,18 @@ EnumType = type(PyEnum)
|
||||||
class EnumOptions(BaseOptions):
|
class EnumOptions(BaseOptions):
|
||||||
enum = None # type: Enum
|
enum = None # type: Enum
|
||||||
deprecation_reason = None
|
deprecation_reason = None
|
||||||
|
enums = () # type: Iterable[Type[Enum]]
|
||||||
|
|
||||||
|
|
||||||
class EnumMeta(SubclassWithMeta_Meta):
|
class EnumMeta(SubclassWithMeta_Meta):
|
||||||
def __new__(cls, name_, bases, classdict, **options):
|
def __new__(cls, name_, bases, classdict, **options):
|
||||||
enum_members = dict(classdict, __eq__=eq_enum)
|
enum_members = dict(__eq__=eq_enum)
|
||||||
|
meta = classdict.get("Meta", None) # type: Optional[EnumOptions]
|
||||||
|
if meta and hasattr(meta, 'enums'):
|
||||||
|
for enum in meta.enums:
|
||||||
|
enum_members.update(enum.as_dict())
|
||||||
|
enum_members.update(classdict)
|
||||||
|
|
||||||
# We remove the Meta attribute from the class to not collide
|
# We remove the Meta attribute from the class to not collide
|
||||||
# with the enum values.
|
# with the enum values.
|
||||||
enum_members.pop("Meta", None)
|
enum_members.pop("Meta", None)
|
||||||
|
@ -106,3 +114,10 @@ class Enum(UnmountedType, BaseType, metaclass=EnumMeta):
|
||||||
is mounted (as a Field, InputField or Argument)
|
is mounted (as a Field, InputField or Argument)
|
||||||
"""
|
"""
|
||||||
return cls
|
return cls
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def as_dict(cls):
|
||||||
|
return {
|
||||||
|
enum_meta.name: enum_meta.value
|
||||||
|
for _, enum_meta in cls._meta.enum.__members__.items()
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user