Skip to content

Commit b5710d3

Browse files
author
Guido van Rossum
committed
Support functional API for Enum.
Fixes #2306.
1 parent 72967fd commit b5710d3

File tree

8 files changed

+299
-24
lines changed

8 files changed

+299
-24
lines changed

mypy/checker.py

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,17 @@
1717
TupleExpr, ListExpr, ExpressionStmt, ReturnStmt, IfStmt,
1818
WhileStmt, OperatorAssignmentStmt, WithStmt, AssertStmt,
1919
RaiseStmt, TryStmt, ForStmt, DelStmt, CallExpr, IntExpr, StrExpr,
20-
UnicodeExpr, OpExpr, UnaryExpr, LambdaExpr, TempNode, SymbolTableNode,
21-
Context, Decorator, PrintStmt, LITERAL_TYPE, BreakStmt, PassStmt, ContinueStmt,
22-
ComparisonExpr, StarExpr, EllipsisExpr, RefExpr, ImportFrom, ImportAll, ImportBase,
23-
ARG_POS, CONTRAVARIANT, COVARIANT, ExecStmt, GlobalDecl, Import, NonlocalDecl,
24-
MDEF, Node
25-
)
20+
BytesExpr, UnicodeExpr, FloatExpr, OpExpr, UnaryExpr, CastExpr, RevealTypeExpr, SuperExpr,
21+
TypeApplication, DictExpr, SliceExpr, LambdaExpr, TempNode, SymbolTableNode,
22+
Context, ListComprehension, ConditionalExpr, GeneratorExpr,
23+
Decorator, SetExpr, TypeVarExpr, NewTypeExpr, PrintStmt,
24+
LITERAL_TYPE, BreakStmt, PassStmt, ContinueStmt, ComparisonExpr, StarExpr,
25+
YieldFromExpr, NamedTupleExpr, TypedDictExpr, SetComprehension,
26+
DictionaryComprehension, ComplexExpr, EllipsisExpr, TypeAliasExpr,
27+
RefExpr, YieldExpr, BackquoteExpr, Import, ImportFrom, ImportAll, ImportBase,
28+
AwaitExpr, PromoteExpr, Node, EnumCallExpr,
29+
ARG_POS, MDEF,
30+
CONTRAVARIANT, COVARIANT)
2631
from mypy import nodes
2732
from mypy.types import (
2833
Type, AnyType, CallableType, FunctionLike, Overloaded, TupleType, TypedDictType,
@@ -45,7 +50,7 @@
4550
from mypy.semanal import set_callable_name, refers_to_fullname
4651
from mypy.erasetype import erase_typevars
4752
from mypy.expandtype import expand_type, expand_type_by_instance
48-
from mypy.visitor import StatementVisitor
53+
from mypy.visitor import NodeVisitor
4954
from mypy.join import join_types
5055
from mypy.treetransform import TransformVisitor
5156
from mypy.meet import is_overlapping_types
@@ -70,7 +75,7 @@
7075
])
7176

7277

73-
class TypeChecker(StatementVisitor[None]):
78+
class TypeChecker(NodeVisitor[None]):
7479
"""Mypy type checker.
7580
7681
Type check mypy source files that have been semantically analyzed.
@@ -2265,21 +2270,13 @@ def visit_break_stmt(self, s: BreakStmt) -> None:
22652270

22662271
def visit_continue_stmt(self, s: ContinueStmt) -> None:
22672272
self.binder.handle_continue()
2273+
return None
22682274

2269-
def visit_exec_stmt(self, s: ExecStmt) -> None:
2270-
pass
2271-
2272-
def visit_global_decl(self, s: GlobalDecl) -> None:
2273-
pass
2274-
2275-
def visit_nonlocal_decl(self, s: NonlocalDecl) -> None:
2276-
pass
2275+
def visit_typeddict_expr(self, e: TypedDictExpr) -> Type:
2276+
return self.expr_checker.visit_typeddict_expr(e)
22772277

2278-
def visit_var(self, s: Var) -> None:
2279-
pass
2280-
2281-
def visit_pass_stmt(self, s: PassStmt) -> None:
2282-
pass
2278+
def visit_enum_call_expr(self, o: 'mypy.nodes.EnumCallExpr') -> Type:
2279+
return self.expr_checker.visit_enum_call_expr(o)
22832280

22842281
#
22852282
# Helpers

mypy/checkexpr.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
ConditionalExpr, ComparisonExpr, TempNode, SetComprehension,
2121
DictionaryComprehension, ComplexExpr, EllipsisExpr, StarExpr, AwaitExpr, YieldExpr,
2222
YieldFromExpr, TypedDictExpr, PromoteExpr, NewTypeExpr, NamedTupleExpr, TypeVarExpr,
23-
TypeAliasExpr, BackquoteExpr, ARG_POS, ARG_NAMED, ARG_STAR, ARG_STAR2, MODULE_REF,
23+
TypeAliasExpr, BackquoteExpr, EnumCallExpr,
24+
ARG_POS, ARG_NAMED, ARG_STAR, ARG_STAR2, MODULE_REF,
2425
UNBOUND_TVAR, BOUND_TVAR, LITERAL_TYPE
2526
)
2627
from mypy import nodes
@@ -354,6 +355,12 @@ def check_call(self, callee: Type, args: List[Expression],
354355
"""
355356
arg_messages = arg_messages or self.msg
356357
if isinstance(callee, CallableType):
358+
if (isinstance(callable_node, RefExpr)
359+
and callable_node.fullname in ('enum.Enum', 'enum.IntEnum',
360+
'enum.Flag', 'enum.IntFlag')):
361+
# An Enum() call that failed SemanticAnalyzer.check_enum_call().
362+
return callee.ret_type, callee
363+
357364
if (callee.is_type_obj() and callee.type_object().is_abstract
358365
# Exceptions for Type[...] and classmethod first argument
359366
and not callee.from_type_type and not callee.is_classmethod_class):
@@ -2204,6 +2211,22 @@ def visit_namedtuple_expr(self, e: NamedTupleExpr) -> Type:
22042211
# TODO: Perhaps return a type object type?
22052212
return AnyType()
22062213

2214+
def visit_enum_call_expr(self, e: EnumCallExpr) -> Type:
2215+
for name, value in zip(e.items, e.values):
2216+
if value is not None:
2217+
typ = self.accept(value)
2218+
if not isinstance(typ, AnyType):
2219+
var = e.info.names[name].node
2220+
if isinstance(var, Var):
2221+
# Inline TypeCheker.set_inferred_type(),
2222+
# without the lvalue. (This doesn't really do
2223+
# much, since the value attribute is defined
2224+
# to have type Any in the typeshed stub.)
2225+
var.type = typ
2226+
var.is_inferred = True
2227+
# TODO: Perhaps return a type object type?
2228+
return AnyType()
2229+
22072230
def visit_typeddict_expr(self, e: TypedDictExpr) -> Type:
22082231
# TODO: Perhaps return a type object type?
22092232
return AnyType()

mypy/nodes.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1832,6 +1832,25 @@ def accept(self, visitor: ExpressionVisitor[T]) -> T:
18321832
return visitor.visit_typeddict_expr(self)
18331833

18341834

1835+
class EnumCallExpr(Expression):
1836+
"""Named tuple expression Enum('name', 'val1 val2 ...')."""
1837+
1838+
# The class representation of this enumerated type
1839+
info = None # type: TypeInfo
1840+
# The item names (for debugging)
1841+
items = None # type: List[str]
1842+
values = None # type: List[Optional[Expression]]
1843+
1844+
def __init__(self, info: 'TypeInfo', items: List[str],
1845+
values: List[Optional[Expression]]) -> None:
1846+
self.info = info
1847+
self.items = items
1848+
self.values = values
1849+
1850+
def accept(self, visitor: ExpressionVisitor[T]) -> T:
1851+
return visitor.visit_enum_call_expr(self)
1852+
1853+
18351854
class PromoteExpr(Expression):
18361855
"""Ducktype class decorator expression _promote(...)."""
18371856

mypy/semanal.py

Lines changed: 135 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
YieldFromExpr, NamedTupleExpr, TypedDictExpr, NonlocalDecl, SymbolNode,
6565
SetComprehension, DictionaryComprehension, TYPE_ALIAS, TypeAliasExpr,
6666
YieldExpr, ExecStmt, Argument, BackquoteExpr, ImportBase, AwaitExpr,
67-
IntExpr, FloatExpr, UnicodeExpr, EllipsisExpr, TempNode,
67+
IntExpr, FloatExpr, UnicodeExpr, EllipsisExpr, TempNode, EnumCallExpr,
6868
COVARIANT, CONTRAVARIANT, INVARIANT, UNBOUND_IMPORTED, LITERAL_YES, ARG_OPT, nongen_builtins,
6969
collections_type_aliases, get_member_expr_fullname,
7070
)
@@ -1498,6 +1498,7 @@ def visit_assignment_stmt(self, s: AssignmentStmt) -> None:
14981498
self.process_typevar_declaration(s)
14991499
self.process_namedtuple_definition(s)
15001500
self.process_typeddict_definition(s)
1501+
self.process_enum_call(s)
15011502

15021503
if (len(s.lvalues) == 1 and isinstance(s.lvalues[0], NameExpr) and
15031504
s.lvalues[0].name == '__all__' and s.lvalues[0].kind == GDEF and
@@ -2327,6 +2328,139 @@ def is_classvar(self, typ: Type) -> bool:
23272328
def fail_invalid_classvar(self, context: Context) -> None:
23282329
self.fail('ClassVar can only be used for assignments in class body', context)
23292330

2331+
def process_enum_call(self, s: AssignmentStmt) -> None:
2332+
"""Check if s defines an Enum; if yes, store the definition in symbol table."""
2333+
if len(s.lvalues) != 1 or not isinstance(s.lvalues[0], NameExpr):
2334+
return
2335+
lvalue = s.lvalues[0]
2336+
name = lvalue.name
2337+
enum_call = self.check_enum_call(s.rvalue, name)
2338+
if enum_call is None:
2339+
return
2340+
# Yes, it's a valid Enum definition. Add it to the symbol table.
2341+
node = self.lookup(name, s)
2342+
if node:
2343+
node.kind = GDEF # TODO locally defined Enum
2344+
node.node = enum_call
2345+
2346+
def check_enum_call(self, node: Expression, var_name: str = None) -> Optional[TypeInfo]:
2347+
"""Check if a call defines an Enum.
2348+
2349+
Example:
2350+
2351+
A = enum.Enum('A', 'foo bar')
2352+
2353+
is equivalent to:
2354+
2355+
class A(enum.Enum):
2356+
foo = 1
2357+
bar = 2
2358+
"""
2359+
if not isinstance(node, CallExpr):
2360+
return None
2361+
call = node
2362+
callee = call.callee
2363+
if not isinstance(callee, RefExpr):
2364+
return None
2365+
fullname = callee.fullname
2366+
if fullname not in ('enum.Enum', 'enum.IntEnum', 'enum.Flag', 'enum.IntFlag'):
2367+
return None
2368+
items, values, ok = self.parse_enum_call_args(call, fullname.split('.')[-1])
2369+
if not ok:
2370+
# Error. Construct dummy return value.
2371+
return self.build_enum_call_typeinfo('Enum', [], fullname)
2372+
name = cast(StrExpr, call.args[0]).value
2373+
if name != var_name or self.is_func_scope():
2374+
# Give it a unique name derived from the line number.
2375+
name += '@' + str(call.line)
2376+
info = self.build_enum_call_typeinfo(name, items, fullname)
2377+
# Store it as a global just in case it would remain anonymous.
2378+
# (Or in the nearest class if there is one.)
2379+
stnode = SymbolTableNode(GDEF, info, self.cur_mod_id)
2380+
if self.type:
2381+
self.type.names[name] = stnode
2382+
else:
2383+
self.globals[name] = stnode
2384+
call.analyzed = EnumCallExpr(info, items, values)
2385+
call.analyzed.set_line(call.line, call.column)
2386+
return info
2387+
2388+
def build_enum_call_typeinfo(self, name: str, items: List[str], fullname: str) -> TypeInfo:
2389+
base = self.named_type_or_none(fullname)
2390+
assert base is not None
2391+
info = self.basic_new_typeinfo(name, base)
2392+
info.is_enum = True
2393+
for item in items:
2394+
var = Var(item)
2395+
var.info = info
2396+
var.is_property = True
2397+
info.names[item] = SymbolTableNode(MDEF, var)
2398+
return info
2399+
2400+
def parse_enum_call_args(self, call: CallExpr,
2401+
class_name: str) -> Tuple[List[str],
2402+
List[Optional[Expression]], bool]:
2403+
args = call.args
2404+
if len(args) < 2:
2405+
return self.fail_enum_call_arg("Too few arguments for %s()" % class_name, call)
2406+
if len(args) > 2:
2407+
return self.fail_enum_call_arg("Too many arguments for %s()" % class_name, call)
2408+
if call.arg_kinds != [ARG_POS, ARG_POS]:
2409+
return self.fail_enum_call_arg("Unexpected arguments to %s()" % class_name, call)
2410+
if not isinstance(args[0], (StrExpr, UnicodeExpr)):
2411+
return self.fail_enum_call_arg(
2412+
"%s() expects a string literal as the first argument" % class_name, call)
2413+
items = []
2414+
values = [] # type: List[Optional[Expression]]
2415+
if isinstance(args[1], (StrExpr, UnicodeExpr)):
2416+
fields = args[1].value
2417+
for field in fields.replace(',', ' ').split():
2418+
items.append(field)
2419+
elif isinstance(args[1], (TupleExpr, ListExpr)):
2420+
seq_items = args[1].items
2421+
if all(isinstance(seq_item, (StrExpr, UnicodeExpr)) for seq_item in seq_items):
2422+
items = [cast(StrExpr, seq_item).value for seq_item in seq_items]
2423+
elif all(isinstance(seq_item, (TupleExpr, ListExpr))
2424+
and len(seq_item.items) == 2
2425+
and isinstance(seq_item.items[0], (StrExpr, UnicodeExpr))
2426+
for seq_item in seq_items):
2427+
for seq_item in seq_items:
2428+
assert isinstance(seq_item, (TupleExpr, ListExpr))
2429+
name, value = seq_item.items
2430+
assert isinstance(name, (StrExpr, UnicodeExpr))
2431+
items.append(name.value)
2432+
values.append(value)
2433+
else:
2434+
return self.fail_enum_call_arg(
2435+
"%s() with tuple or list expects strings or (name, value) pairs" %
2436+
class_name,
2437+
call)
2438+
elif isinstance(args[1], DictExpr):
2439+
for key, value in args[1].items:
2440+
if not isinstance(key, (StrExpr, UnicodeExpr)):
2441+
return self.fail_enum_call_arg(
2442+
"%s() with dict literal requires string literals" % class_name, call)
2443+
items.append(key.value)
2444+
values.append(value)
2445+
else:
2446+
# TODO: Allow dict(x=1, y=2) as a substitute for {'x': 1, 'y': 2}?
2447+
return self.fail_enum_call_arg(
2448+
"%s() expects a string, tuple, list or dict literal as the second argument" %
2449+
class_name,
2450+
call)
2451+
if len(items) == 0:
2452+
return self.fail_enum_call_arg("%s() needs at least one item" % class_name, call)
2453+
if not values:
2454+
values = [None] * len(items)
2455+
assert len(items) == len(values)
2456+
return items, values, True
2457+
2458+
def fail_enum_call_arg(self, message: str,
2459+
context: Context) -> Tuple[List[str],
2460+
List[Optional[Expression]], bool]:
2461+
self.fail(message, context)
2462+
return [], [], False
2463+
23302464
def visit_decorator(self, dec: Decorator) -> None:
23312465
for d in dec.decorators:
23322466
d.accept(self)

mypy/strconv.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,9 @@ def visit_namedtuple_expr(self, o: 'mypy.nodes.NamedTupleExpr') -> str:
431431
o.info.name(),
432432
o.info.tuple_type)
433433

434+
def visit_enum_call_expr(self, o: 'mypy.nodes.EnumCallExpr') -> str:
435+
return 'EnumCallExpr:{}({}, {})'.format(o.line, o.info.name(), o.items)
436+
434437
def visit_typeddict_expr(self, o: 'mypy.nodes.TypedDictExpr') -> str:
435438
return 'TypedDictExpr:{}({})'.format(o.line,
436439
o.info.name())

mypy/treetransform.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
ComparisonExpr, TempNode, StarExpr, Statement, Expression,
2020
YieldFromExpr, NamedTupleExpr, TypedDictExpr, NonlocalDecl, SetComprehension,
2121
DictionaryComprehension, ComplexExpr, TypeAliasExpr, EllipsisExpr,
22-
YieldExpr, ExecStmt, Argument, BackquoteExpr, AwaitExpr, OverloadPart
22+
YieldExpr, ExecStmt, Argument, BackquoteExpr, AwaitExpr,
23+
OverloadPart, EnumCallExpr,
2324
)
2425
from mypy.types import Type, FunctionLike
2526
from mypy.traverser import TraverserVisitor
@@ -486,6 +487,9 @@ def visit_newtype_expr(self, node: NewTypeExpr) -> NewTypeExpr:
486487
def visit_namedtuple_expr(self, node: NamedTupleExpr) -> NamedTupleExpr:
487488
return NamedTupleExpr(node.info)
488489

490+
def visit_enum_call_expr(self, node: EnumCallExpr) -> EnumCallExpr:
491+
return EnumCallExpr(node.info, node.items, node.values)
492+
489493
def visit_typeddict_expr(self, node: TypedDictExpr) -> Node:
490494
return TypedDictExpr(node.info)
491495

mypy/visitor.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ def visit_type_alias_expr(self, o: 'mypy.nodes.TypeAliasExpr') -> T:
156156
def visit_namedtuple_expr(self, o: 'mypy.nodes.NamedTupleExpr') -> T:
157157
pass
158158

159+
@abstractmethod
160+
def visit_enum_call_expr(self, o: 'mypy.nodes.EnumCallExpr') -> T:
161+
pass
162+
159163
@abstractmethod
160164
def visit_typeddict_expr(self, o: 'mypy.nodes.TypedDictExpr') -> T:
161165
pass
@@ -514,6 +518,9 @@ def visit_type_alias_expr(self, o: 'mypy.nodes.TypeAliasExpr') -> T:
514518
def visit_namedtuple_expr(self, o: 'mypy.nodes.NamedTupleExpr') -> T:
515519
pass
516520

521+
def visit_enum_call_expr(self, o: 'mypy.nodes.EnumCallExpr') -> T:
522+
pass
523+
517524
def visit_typeddict_expr(self, o: 'mypy.nodes.TypedDictExpr') -> T:
518525
pass
519526

0 commit comments

Comments
 (0)