File tree Expand file tree Collapse file tree 2 files changed +47
-0
lines changed Expand file tree Collapse file tree 2 files changed +47
-0
lines changed Original file line number Diff line number Diff line change
1
+ from griffe .dataclasses import Object
2
+ from griffe .expressions import ExprName
3
+ from griffe .tests import temporary_visited_module
4
+
5
+
6
+ with temporary_visited_module (
7
+ """
8
+ from enum import Enum
9
+
10
+ class MyEnum(Enum):
11
+ MY_FIELD = "hello"
12
+
13
+ my_fields = [MyEnum.MY_FIELD.value]
14
+ """ ,
15
+ ) as module :
16
+ expression = module ["my_fields" ].value
17
+
18
+
19
+ attribute = expression .elements [0 ]
20
+ print (attribute .last .parent .is_enum_value )
Original file line number Diff line number Diff line change @@ -607,6 +607,33 @@ def canonical_path(self) -> str:
607
607
except NameResolutionError :
608
608
return self .name
609
609
610
+ @property
611
+ def is_enum_class (self ) -> bool :
612
+ """Whether this name resolves to an enumeration class."""
613
+ try :
614
+ bases = self .parent [self .name ].bases # type: ignore[union-attr,index]
615
+ except Exception : # noqa: BLE001
616
+ return False
617
+
618
+ # TODO: Support inheritance?
619
+ return any (isinstance (base , Expr ) and base .canonical_path == "enum.Enum" for base in bases )
620
+
621
+ @property
622
+ def is_enum_instance (self ) -> bool :
623
+ """Whether this name resolves to an enumeration instance."""
624
+ try :
625
+ return self .parent .is_enum_class # type: ignore[union-attr]
626
+ except Exception : # noqa: BLE001
627
+ return False
628
+
629
+ @property
630
+ def is_enum_value (self ) -> bool :
631
+ """Whether this name resolves to an enumeration value."""
632
+ try :
633
+ return self .name == "value" and self .parent .is_enum_instance # type: ignore[union-attr]
634
+ except Exception : # noqa: BLE001
635
+ return False
636
+
610
637
611
638
@dataclass (eq = True , ** dataclass_opts )
612
639
class ExprNamedExpr (Expr ):
You can’t perform that action at this time.
0 commit comments