MCPcopy Index your code
hub / github.com/python/mypy / check_enum_call

Method check_enum_call

mypy/semanal_enum.py:83–133  ·  view source on GitHub ↗

Check if a call defines an Enum. Example: A = enum.Enum('A', 'foo bar') is equivalent to: class A(enum.Enum): foo = 1 bar = 2

(
        self, node: Expression, var_name: str, is_func_scope: bool
    )

Source from the content-addressed store, hash-verified

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)
116 info = self.build_enum_call_typeinfo(name, [], fullname, node.line)
117 else:
118 if new_class_name != var_name:
119 msg = f'String argument 1 "{new_class_name}" to {fullname}(...) does not match variable name "{var_name}"'
120 self.fail(msg, call)
121
122 name = cast(StrExpr, call.args[0]).value
123 if name != var_name or is_func_scope:
124 # Give it a unique name derived from the line number.
125 name += "@" + str(call.line)
126 info = self.build_enum_call_typeinfo(name, items, fullname, call.line)
127 # Store generated TypeInfo under both names, see semanal_namedtuple for more details.
128 if name != var_name or is_func_scope:
129 self.api.add_symbol_skip_local(name, info)
130 call.analyzed = EnumCallExpr(info, items, values)
131 call.analyzed.set_line(call)
132 info.line = node.line
133 return info
134
135 def build_enum_call_typeinfo(
136 self, name: str, items: list[str], fullname: str, line: int

Callers 1

process_enum_callMethod · 0.95

Calls 9

parse_enum_call_argsMethod · 0.95
failMethod · 0.95
EnumCallExprClass · 0.90
isinstanceFunction · 0.85
strClass · 0.85
splitMethod · 0.80
add_symbol_skip_localMethod · 0.45
set_lineMethod · 0.45

Tested by

no test coverage detected