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

Class EnumCallAnalyzer

mypy/semanal_enum.py:58–269  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

56
57
58class EnumCallAnalyzer:
59 def __init__(self, options: Options, api: SemanticAnalyzerInterface) -> None:
60 self.options = options
61 self.api = api
62
63 def process_enum_call(self, s: AssignmentStmt, is_func_scope: bool) -> bool:
64 """Check if s defines an Enum; if yes, store the definition in symbol table.
65
66 Return True if this looks like an Enum definition (but maybe with errors),
67 otherwise return False.
68 """
69 if len(s.lvalues) != 1 or not isinstance(s.lvalues[0], (NameExpr, MemberExpr)):
70 return False
71 lvalue = s.lvalues[0]
72 name = lvalue.name
73 enum_call = self.check_enum_call(s.rvalue, name, is_func_scope)
74 if enum_call is None:
75 return False
76 if isinstance(lvalue, MemberExpr):
77 self.fail("Enum type as attribute is not supported", lvalue)
78 return False
79 # Yes, it's a valid Enum definition. Add it to the symbol table.
80 self.api.add_symbol(name, enum_call, s)
81 return True
82
83 def check_enum_call(
84 self, node: Expression, var_name: str, is_func_scope: bool
85 ) -> TypeInfo | None:
86 """Check if a call defines an Enum.
87
88 Example:
89
90 A = enum.Enum('A', 'foo bar')
91
92 is equivalent to:
93
94 class A(enum.Enum):
95 foo = 1
96 bar = 2
97 """
98 if not isinstance(node, CallExpr):
99 return None
100 call = node
101 callee = call.callee
102 if not isinstance(callee, RefExpr):
103 return None
104 fullname = callee.fullname
105 if fullname not in ENUM_BASES:
106 return None
107
108 new_class_name, items, values, ok = self.parse_enum_call_args(
109 call, fullname.split(".")[-1]
110 )
111 if not ok:
112 # Error. Construct dummy return value.
113 name = var_name
114 if is_func_scope:
115 name += "@" + str(call.line)

Callers 1

file_contextMethod · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…