|
13 | 13 | )
|
14 | 14 | from mypy.semanal_shared import SemanticAnalyzerInterface
|
15 | 15 | from mypy.options import Options
|
| 16 | +from mypy.types import get_proper_type, LiteralType |
16 | 17 |
|
17 | 18 | # Note: 'enum.EnumMeta' is deliberately excluded from this list. Classes that directly use
|
18 | 19 | # enum.EnumMeta do not necessarily automatically have the 'name' and 'value' attributes.
|
@@ -173,6 +174,23 @@ def parse_enum_call_args(self, call: CallExpr,
|
173 | 174 | "%s() with dict literal requires string literals" % class_name, call)
|
174 | 175 | items.append(key.value)
|
175 | 176 | values.append(value)
|
| 177 | + elif isinstance(args[1], RefExpr) and isinstance(args[1].node, Var): |
| 178 | + proper_type = get_proper_type(args[1].node.type) |
| 179 | + if (proper_type is not None |
| 180 | + and isinstance(proper_type, LiteralType) |
| 181 | + and isinstance(proper_type.value, str)): |
| 182 | + fields = proper_type.value |
| 183 | + for field in fields.replace(',', ' ').split(): |
| 184 | + items.append(field) |
| 185 | + elif args[1].node.is_final and isinstance(args[1].node.final_value, str): |
| 186 | + fields = args[1].node.final_value |
| 187 | + for field in fields.replace(',', ' ').split(): |
| 188 | + items.append(field) |
| 189 | + else: |
| 190 | + return self.fail_enum_call_arg( |
| 191 | + "%s() expects a string, tuple, list or dict literal as the second argument" % |
| 192 | + class_name, |
| 193 | + call) |
176 | 194 | else:
|
177 | 195 | # TODO: Allow dict(x=1, y=2) as a substitute for {'x': 1, 'y': 2}?
|
178 | 196 | return self.fail_enum_call_arg(
|
|
0 commit comments