Skip to content

Commit 4c731ab

Browse files
author
Guido van Rossum
committed
Support functional API for Enum.
Fixes #2306.
1 parent 5434979 commit 4c731ab

File tree

8 files changed

+298
-24
lines changed

8 files changed

+298
-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,
@@ -44,7 +49,7 @@
4449
from mypy.semanal import set_callable_name, refers_to_fullname
4550
from mypy.erasetype import erase_typevars
4651
from mypy.expandtype import expand_type, expand_type_by_instance
47-
from mypy.visitor import StatementVisitor
52+
from mypy.visitor import NodeVisitor
4853
from mypy.join import join_types
4954
from mypy.treetransform import TransformVisitor
5055
from mypy.meet import is_overlapping_types
@@ -69,7 +74,7 @@
6974
])
7075

7176

72-
class TypeChecker(StatementVisitor[None]):
77+
class TypeChecker(NodeVisitor[None]):
7378
"""Mypy type checker.
7479
7580
Type check mypy source files that have been semantically analyzed.
@@ -2212,21 +2217,13 @@ def visit_break_stmt(self, s: BreakStmt) -> None:
22122217

22132218
def visit_continue_stmt(self, s: ContinueStmt) -> None:
22142219
self.binder.handle_continue()
2220+
return None
22152221

2216-
def visit_exec_stmt(self, s: ExecStmt) -> None:
2217-
pass
2218-
2219-
def visit_global_decl(self, s: GlobalDecl) -> None:
2220-
pass
2221-
2222-
def visit_nonlocal_decl(self, s: NonlocalDecl) -> None:
2223-
pass
2222+
def visit_typeddict_expr(self, e: TypedDictExpr) -> Type:
2223+
return self.expr_checker.visit_typeddict_expr(e)
22242224

2225-
def visit_var(self, s: Var) -> None:
2226-
pass
2227-
2228-
def visit_pass_stmt(self, s: PassStmt) -> None:
2229-
pass
2225+
def visit_enum_call_expr(self, o: 'mypy.nodes.EnumCallExpr') -> Type:
2226+
return self.expr_checker.visit_enum_call_expr(o)
22302227

22312228
#
22322229
# 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_concrete_type_obj() and callee.type_object().is_abstract:
358365
type = callee.type_object()
359366
self.msg.cannot_instantiate_abstract_class(
@@ -2191,6 +2198,22 @@ def visit_namedtuple_expr(self, e: NamedTupleExpr) -> Type:
21912198
# TODO: Perhaps return a type object type?
21922199
return AnyType()
21932200

2201+
def visit_enum_call_expr(self, e: EnumCallExpr) -> Type:
2202+
for name, value in zip(e.items, e.values):
2203+
if value is not None:
2204+
typ = self.accept(value)
2205+
if not isinstance(typ, AnyType):
2206+
var = e.info.names[name].node
2207+
if isinstance(var, Var):
2208+
# Inline TypeCheker.set_inferred_type(),
2209+
# without the lvalue. (This doesn't really do
2210+
# much, since the value attribute is defined
2211+
# to have type Any in the typeshed stub.)
2212+
var.type = typ
2213+
var.is_inferred = True
2214+
# TODO: Perhaps return a type object type?
2215+
return AnyType()
2216+
21942217
def visit_typeddict_expr(self, e: TypedDictExpr) -> Type:
21952218
# TODO: Perhaps return a type object type?
21962219
return AnyType()

mypy/nodes.py

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

18201820

1821+
class EnumCallExpr(Expression):
1822+
"""Named tuple expression Enum('name', 'val1 val2 ...')."""
1823+
1824+
# The class representation of this enumerated type
1825+
info = None # type: TypeInfo
1826+
# The item names (for debugging)
1827+
items = None # type: List[str]
1828+
values = None # type: List[Optional[Expression]]
1829+
1830+
def __init__(self, info: 'TypeInfo', items: List[str],
1831+
values: List[Optional[Expression]]) -> None:
1832+
self.info = info
1833+
self.items = items
1834+
self.values = values
1835+
1836+
def accept(self, visitor: ExpressionVisitor[T]) -> T:
1837+
return visitor.visit_enum_call_expr(self)
1838+
1839+
18211840
class PromoteExpr(Expression):
18221841
"""Ducktype class decorator expression _promote(...)."""
18231842

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
)
@@ -1434,6 +1434,7 @@ def visit_assignment_stmt(self, s: AssignmentStmt) -> None:
14341434
self.process_typevar_declaration(s)
14351435
self.process_namedtuple_definition(s)
14361436
self.process_typeddict_definition(s)
1437+
self.process_enum_call(s)
14371438

14381439
if (len(s.lvalues) == 1 and isinstance(s.lvalues[0], NameExpr) and
14391440
s.lvalues[0].name == '__all__' and s.lvalues[0].kind == GDEF and
@@ -2263,6 +2264,139 @@ def is_classvar(self, typ: Type) -> bool:
22632264
def fail_invalid_classvar(self, context: Context) -> None:
22642265
self.fail('ClassVar can only be used for assignments in class body', context)
22652266

2267+
def process_enum_call(self, s: AssignmentStmt) -> None:
2268+
"""Check if s defines an Enum; if yes, store the definition in symbol table."""
2269+
if len(s.lvalues) != 1 or not isinstance(s.lvalues[0], NameExpr):
2270+
return
2271+
lvalue = s.lvalues[0]
2272+
name = lvalue.name
2273+
enum_call = self.check_enum_call(s.rvalue, name)
2274+
if enum_call is None:
2275+
return
2276+
# Yes, it's a valid Enum definition. Add it to the symbol table.
2277+
node = self.lookup(name, s)
2278+
if node:
2279+
node.kind = GDEF # TODO locally defined Enum
2280+
node.node = enum_call
2281+
2282+
def check_enum_call(self, node: Expression, var_name: str = None) -> Optional[TypeInfo]:
2283+
"""Check if a call defines an Enum.
2284+
2285+
Example:
2286+
2287+
A = enum.Enum('A', 'foo bar')
2288+
2289+
is equivalent to:
2290+
2291+
class A(enum.Enum):
2292+
foo = 1
2293+
bar = 2
2294+
"""
2295+
if not isinstance(node, CallExpr):
2296+
return None
2297+
call = node
2298+
callee = call.callee
2299+
if not isinstance(callee, RefExpr):
2300+
return None
2301+
fullname = callee.fullname
2302+
if fullname not in ('enum.Enum', 'enum.IntEnum', 'enum.Flag', 'enum.IntFlag'):
2303+
return None
2304+
items, values, ok = self.parse_enum_call_args(call, fullname.split('.')[-1])
2305+
if not ok:
2306+
# Error. Construct dummy return value.
2307+
return self.build_enum_call_typeinfo('Enum', [], fullname)
2308+
name = cast(StrExpr, call.args[0]).value
2309+
if name != var_name or self.is_func_scope():
2310+
# Give it a unique name derived from the line number.
2311+
name += '@' + str(call.line)
2312+
info = self.build_enum_call_typeinfo(name, items, fullname)
2313+
# Store it as a global just in case it would remain anonymous.
2314+
# (Or in the nearest class if there is one.)
2315+
stnode = SymbolTableNode(GDEF, info, self.cur_mod_id)
2316+
if self.type:
2317+
self.type.names[name] = stnode
2318+
else:
2319+
self.globals[name] = stnode
2320+
call.analyzed = EnumCallExpr(info, items, values)
2321+
call.analyzed.set_line(call.line, call.column)
2322+
return info
2323+
2324+
def build_enum_call_typeinfo(self, name: str, items: List[str], fullname: str) -> TypeInfo:
2325+
base = self.named_type_or_none(fullname)
2326+
assert base is not None
2327+
info = self.basic_new_typeinfo(name, base)
2328+
info.is_enum = True
2329+
for item in items:
2330+
var = Var(item)
2331+
var.info = info
2332+
var.is_property = True
2333+
info.names[item] = SymbolTableNode(MDEF, var)
2334+
return info
2335+
2336+
def parse_enum_call_args(self, call: CallExpr,
2337+
class_name: str) -> Tuple[List[str],
2338+
List[Optional[Expression]], bool]:
2339+
args = call.args
2340+
if len(args) < 2:
2341+
return self.fail_enum_call_arg("Too few arguments for %s()" % class_name, call)
2342+
if len(args) > 2:
2343+
return self.fail_enum_call_arg("Too many arguments for %s()" % class_name, call)
2344+
if call.arg_kinds != [ARG_POS, ARG_POS]:
2345+
return self.fail_enum_call_arg("Unexpected arguments to %s()" % class_name, call)
2346+
if not isinstance(args[0], (StrExpr, UnicodeExpr)):
2347+
return self.fail_enum_call_arg(
2348+
"%s() expects a string literal as the first argument" % class_name, call)
2349+
items = []
2350+
values = [] # type: List[Optional[Expression]]
2351+
if isinstance(args[1], (StrExpr, UnicodeExpr)):
2352+
fields = args[1].value
2353+
for field in fields.replace(',', ' ').split():
2354+
items.append(field)
2355+
elif isinstance(args[1], (TupleExpr, ListExpr)):
2356+
seq_items = args[1].items
2357+
if all(isinstance(seq_item, (StrExpr, UnicodeExpr)) for seq_item in seq_items):
2358+
items = [cast(StrExpr, seq_item).value for seq_item in seq_items]
2359+
elif all(isinstance(seq_item, (TupleExpr, ListExpr))
2360+
and len(seq_item.items) == 2
2361+
and isinstance(seq_item.items[0], (StrExpr, UnicodeExpr))
2362+
for seq_item in seq_items):
2363+
for seq_item in seq_items:
2364+
assert isinstance(seq_item, (TupleExpr, ListExpr))
2365+
name, value = seq_item.items
2366+
assert isinstance(name, (StrExpr, UnicodeExpr))
2367+
items.append(name.value)
2368+
values.append(value)
2369+
else:
2370+
return self.fail_enum_call_arg(
2371+
"%s() with tuple or list expects strings or (name, value) pairs" %
2372+
class_name,
2373+
call)
2374+
elif isinstance(args[1], DictExpr):
2375+
for key, value in args[1].items:
2376+
if not isinstance(key, (StrExpr, UnicodeExpr)):
2377+
return self.fail_enum_call_arg(
2378+
"%s() with dict literal requires string literals" % class_name, call)
2379+
items.append(key.value)
2380+
values.append(value)
2381+
else:
2382+
# TODO: Allow dict(x=1, y=2) as a substitute for {'x': 1, 'y': 2}?
2383+
return self.fail_enum_call_arg(
2384+
"%s() expects a string, tuple, list or dict literal as the second argument" %
2385+
class_name,
2386+
call)
2387+
if len(items) == 0:
2388+
return self.fail_enum_call_arg("%s() needs at least one item" % class_name, call)
2389+
if not values:
2390+
values = [None] * len(items)
2391+
assert len(items) == len(values)
2392+
return items, values, True
2393+
2394+
def fail_enum_call_arg(self, message: str,
2395+
context: Context) -> Tuple[List[str],
2396+
List[Optional[Expression]], bool]:
2397+
self.fail(message, context)
2398+
return [], [], False
2399+
22662400
def visit_decorator(self, dec: Decorator) -> None:
22672401
for d in dec.decorators:
22682402
d.accept(self)

mypy/strconv.py

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

432+
def visit_enum_call_expr(self, o: 'mypy.nodes.EnumCallExpr') -> str:
433+
return 'EnumCallExpr:{}({}, {})'.format(o.line, o.info.name(), o.items)
434+
432435
def visit_typeddict_expr(self, o: 'mypy.nodes.TypedDictExpr') -> str:
433436
return 'TypedDictExpr:{}({})'.format(o.line,
434437
o.info.name())

mypy/treetransform.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
ComparisonExpr, TempNode, StarExpr, Statement, Expression,
2020
YieldFromExpr, NamedTupleExpr, TypedDictExpr, NonlocalDecl, SetComprehension,
2121
DictionaryComprehension, ComplexExpr, TypeAliasExpr, EllipsisExpr,
22-
YieldExpr, ExecStmt, Argument, BackquoteExpr, AwaitExpr,
22+
YieldExpr, ExecStmt, Argument, BackquoteExpr, AwaitExpr, EnumCallExpr,
2323
)
2424
from mypy.types import Type, FunctionLike
2525
from mypy.traverser import TraverserVisitor
@@ -485,6 +485,9 @@ def visit_newtype_expr(self, node: NewTypeExpr) -> NewTypeExpr:
485485
def visit_namedtuple_expr(self, node: NamedTupleExpr) -> NamedTupleExpr:
486486
return NamedTupleExpr(node.info)
487487

488+
def visit_enum_call_expr(self, node: EnumCallExpr) -> EnumCallExpr:
489+
return EnumCallExpr(node.info, node.items, node.values)
490+
488491
def visit_typeddict_expr(self, node: TypedDictExpr) -> Node:
489492
return TypedDictExpr(node.info)
490493

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)