MCPcopy
hub / github.com/python/mypy / enum_members

Method enum_members

mypy/nodes.py:3938–3987  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

3936
3937 @property
3938 def enum_members(self) -> list[str]:
3939 # TODO: cache the results?
3940 members = []
3941 for name, sym in self.names.items():
3942 # Case 1:
3943 #
3944 # class MyEnum(Enum):
3945 # @member
3946 # def some(self): ...
3947 if isinstance(sym.node, Decorator):
3948 if any(
3949 dec.fullname == "enum.member"
3950 for dec in sym.node.decorators
3951 if isinstance(dec, RefExpr)
3952 ):
3953 members.append(name)
3954 continue
3955 # Case 2:
3956 #
3957 # class MyEnum(Enum):
3958 # x = 1
3959 #
3960 # Case 3:
3961 #
3962 # class MyEnum(Enum):
3963 # class Other: ...
3964 elif isinstance(sym.node, (Var, TypeInfo)):
3965 if (
3966 # TODO: properly support ignored names from `_ignore_`
3967 name in EXCLUDED_ENUM_ATTRIBUTES
3968 or is_sunder(name)
3969 or name.startswith("__") # dunder and private
3970 ):
3971 continue # name is excluded
3972
3973 if isinstance(sym.node, Var):
3974 if not sym.node.has_explicit_value:
3975 continue # unannotated value not a member
3976
3977 typ = mypy.types.get_proper_type(sym.node.type)
3978 if (
3979 isinstance(typ, mypy.types.FunctionLike) and not typ.is_type_obj()
3980 ) or ( # explicit `@member` is required
3981 isinstance(typ, mypy.types.Instance)
3982 and typ.type.fullname == "enum.nonmember"
3983 ):
3984 continue # name is not a member
3985
3986 members.append(name)
3987 return members
3988
3989 def __getitem__(self, name: str) -> SymbolTableNode:
3990 n = self.get(name)

Callers

nothing calls this directly

Calls 7

is_sunderFunction · 0.90
isinstanceFunction · 0.85
anyFunction · 0.85
appendMethod · 0.80
itemsMethod · 0.45
startswithMethod · 0.45
is_type_objMethod · 0.45

Tested by

no test coverage detected