Skip to content

Commit fa0a95b

Browse files
committed
Remove Undefined and UndefinedExpr from mypy
closes #680
1 parent ade686c commit fa0a95b

File tree

8 files changed

+10
-95
lines changed

8 files changed

+10
-95
lines changed

mypy/checker.py

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
BytesExpr, UnicodeExpr, FloatExpr, OpExpr, UnaryExpr, CastExpr, SuperExpr,
1616
TypeApplication, DictExpr, SliceExpr, FuncExpr, TempNode, SymbolTableNode,
1717
Context, ListComprehension, ConditionalExpr, GeneratorExpr,
18-
Decorator, SetExpr, PassStmt, TypeVarExpr, UndefinedExpr, PrintStmt,
18+
Decorator, SetExpr, PassStmt, TypeVarExpr, PrintStmt,
1919
LITERAL_TYPE, BreakStmt, ContinueStmt, ComparisonExpr, StarExpr,
2020
YieldFromExpr, YieldFromStmt, NamedTupleExpr, SetComprehension,
2121
DictionaryComprehension, ComplexExpr, EllipsisNode, TypeAliasExpr,
@@ -1004,13 +1004,9 @@ def check_multi_assignment(self, lvalues: List[Node],
10041004
if not msg:
10051005
msg = messages.INCOMPATIBLE_TYPES_IN_ASSIGNMENT
10061006

1007-
# First handle case where rvalue is of form Undefined, ...
1008-
rvalue_type = get_undefined_tuple(rvalue, self.named_type('builtins.tuple'))
1009-
undefined_rvalue = True
1010-
if not rvalue_type:
1011-
# Infer the type of an ordinary rvalue expression.
1012-
rvalue_type = self.accept(rvalue) # TODO maybe elsewhere; redundant
1013-
undefined_rvalue = False
1007+
# Infer the type of an ordinary rvalue expression.
1008+
rvalue_type = self.accept(rvalue) # TODO maybe elsewhere; redundant
1009+
undefined_rvalue = False
10141010

10151011
if isinstance(rvalue_type, AnyType):
10161012
for lv in lvalues:
@@ -1243,13 +1239,7 @@ def narrow_type_from_binder(self, expr: Node, known_type: Type) -> Type:
12431239
def check_simple_assignment(self, lvalue_type: Type, rvalue: Node,
12441240
context: Node,
12451241
msg: str = messages.INCOMPATIBLE_TYPES_IN_ASSIGNMENT) -> Type:
1246-
"""Checks the assignment of rvalue to a lvalue of type lvalue_type."""
1247-
if refers_to_fullname(rvalue, 'typing.Undefined'):
1248-
# The rvalue is just 'Undefined'; this is always valid.
1249-
# Infer the type of 'Undefined' from the lvalue type.
1250-
self.store_type(rvalue, lvalue_type)
1251-
return None
1252-
elif self.is_stub and isinstance(rvalue, EllipsisNode):
1242+
if self.is_stub and isinstance(rvalue, EllipsisNode):
12531243
# '...' is always a valid initializer in a stub.
12541244
return AnyType()
12551245
else:
@@ -1797,9 +1787,6 @@ def visit_generator_expr(self, e: GeneratorExpr) -> Type:
17971787
def visit_dictionary_comprehension(self, e: DictionaryComprehension) -> Type:
17981788
return self.expr_checker.visit_dictionary_comprehension(e)
17991789

1800-
def visit_undefined_expr(self, e: UndefinedExpr) -> Type:
1801-
return self.expr_checker.visit_undefined_expr(e)
1802-
18031790
def visit_temp_node(self, e: TempNode) -> Type:
18041791
return e.type
18051792

@@ -1981,21 +1968,6 @@ def map_type_from_supertype(typ: Type, sub_info: TypeInfo,
19811968
return expand_type_by_instance(typ, inst_type)
19821969

19831970

1984-
def get_undefined_tuple(rvalue: Node, tuple_type: Instance) -> Type:
1985-
"""Get tuple type corresponding to a tuple of Undefined values.
1986-
1987-
The type is Tuple[Any, ...]. If rvalue is not of the right form, return
1988-
None.
1989-
"""
1990-
if isinstance(rvalue, TupleExpr):
1991-
for item in rvalue.items:
1992-
if not refers_to_fullname(item, 'typing.Undefined'):
1993-
break
1994-
else:
1995-
return TupleType([AnyType()] * len(rvalue.items), tuple_type)
1996-
return None
1997-
1998-
19991971
def find_isinstance_check(node: Node,
20001972
type_map: Dict[Node, Type]) -> Tuple[Node, Type, Type, int]:
20011973
"""Check if node is an isinstance(variable, type) check.

mypy/checkexpr.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
OpExpr, UnaryExpr, IndexExpr, CastExpr, TypeApplication, ListExpr,
1313
TupleExpr, DictExpr, FuncExpr, SuperExpr, SliceExpr, Context,
1414
ListComprehension, GeneratorExpr, SetExpr, MypyFile, Decorator,
15-
UndefinedExpr, ConditionalExpr, ComparisonExpr, TempNode, SetComprehension,
15+
ConditionalExpr, ComparisonExpr, TempNode, SetComprehension,
1616
DictionaryComprehension, ComplexExpr, EllipsisNode, LITERAL_TYPE,
1717
TypeAliasExpr
1818
)
@@ -1197,9 +1197,6 @@ def check_for_comp(self, e: Union[GeneratorExpr, DictionaryComprehension]) -> No
11971197
self.accept(condition)
11981198
self.chk.binder.pop_frame()
11991199

1200-
def visit_undefined_expr(self, e: UndefinedExpr) -> Type:
1201-
return e.type
1202-
12031200
def visit_conditional_expr(self, e: ConditionalExpr) -> Type:
12041201
cond_type = self.accept(e.cond)
12051202
self.check_not_void(cond_type, e)

mypy/nodes.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,22 +1278,6 @@ def accept(self, visitor: NodeVisitor[T]) -> T:
12781278
return visitor.visit_conditional_expr(self)
12791279

12801280

1281-
class UndefinedExpr(Node):
1282-
"""Expression None # type: type, used as an initializer.
1283-
1284-
This is used to declare the type of a variable without initializing with
1285-
a proper value. For example:
1286-
1287-
x = None # type: List[int]
1288-
"""
1289-
1290-
def __init__(self, type: 'mypy.types.Type') -> None:
1291-
self.type = type
1292-
1293-
def accept(self, visitor: NodeVisitor[T]) -> T:
1294-
return visitor.visit_undefined_expr(self)
1295-
1296-
12971281
class TypeApplication(Node):
12981282
"""Type application expr[type, ...]"""
12991283

mypy/semanal.py

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
GlobalDecl, SuperExpr, DictExpr, CallExpr, RefExpr, OpExpr, UnaryExpr,
5555
SliceExpr, CastExpr, TypeApplication, Context, SymbolTable,
5656
SymbolTableNode, BOUND_TVAR, UNBOUND_TVAR, ListComprehension, GeneratorExpr,
57-
FuncExpr, MDEF, FuncBase, Decorator, SetExpr, UndefinedExpr, TypeVarExpr,
57+
FuncExpr, MDEF, FuncBase, Decorator, SetExpr, TypeVarExpr,
5858
StrExpr, PrintStmt, ConditionalExpr, PromoteExpr,
5959
ComparisonExpr, StarExpr, ARG_POS, ARG_NAMED, MroError, type_aliases,
6060
YieldFromStmt, YieldFromExpr, NamedTupleExpr, NonlocalDecl,
@@ -821,7 +821,6 @@ def visit_assignment_stmt(self, s: AssignmentStmt) -> None:
821821
if s.type:
822822
s.type = self.anal_type(s.type)
823823
else:
824-
s.type = self.infer_type_from_undefined(s.rvalue)
825824
# For simple assignments, allow binding type aliases.
826825
if (s.type is None and len(s.lvalues) == 1 and
827826
isinstance(s.lvalues[0], NameExpr)):
@@ -988,13 +987,6 @@ def check_lvalue_validity(self, node: Node, ctx: Context) -> None:
988987
if isinstance(node, (FuncDef, TypeInfo, TypeVarExpr)):
989988
self.fail('Invalid assignment target', ctx)
990989

991-
def infer_type_from_undefined(self, rvalue: Node) -> Type:
992-
if isinstance(rvalue, CallExpr):
993-
if isinstance(rvalue.analyzed, UndefinedExpr):
994-
undef = cast(UndefinedExpr, rvalue.analyzed)
995-
return undef.type
996-
return None
997-
998990
def store_declared_types(self, lvalue: Node, typ: Type) -> None:
999991
if isinstance(typ, StarType) and not isinstance(lvalue, StarExpr):
1000992
self.fail('Star type only allowed for starred expressions', lvalue)
@@ -1476,7 +1468,7 @@ def visit_call_expr(self, expr: CallExpr) -> None:
14761468
"""Analyze a call expression.
14771469
14781470
Some call expressions are recognized as special forms, including
1479-
cast(...), Undefined(...) and Any(...).
1471+
cast(...) and Any(...).
14801472
"""
14811473
expr.callee.accept(self)
14821474
if refers_to_fullname(expr.callee, 'typing.cast'):
@@ -1501,18 +1493,6 @@ def visit_call_expr(self, expr: CallExpr) -> None:
15011493
expr.analyzed = CastExpr(expr.args[0], AnyType())
15021494
expr.analyzed.line = expr.line
15031495
expr.analyzed.accept(self)
1504-
elif refers_to_fullname(expr.callee, 'typing.Undefined'):
1505-
# Special form Undefined(...).
1506-
if not self.check_fixed_args(expr, 1, 'Undefined'):
1507-
return
1508-
try:
1509-
type = expr_to_unanalyzed_type(expr.args[0])
1510-
except TypeTranslationError:
1511-
self.fail('Argument to Undefined is not a type', expr)
1512-
return
1513-
expr.analyzed = UndefinedExpr(type)
1514-
expr.analyzed.line = expr.line
1515-
expr.analyzed.accept(self)
15161496
elif refers_to_fullname(expr.callee, 'typing._promote'):
15171497
# Special form _promote(...).
15181498
if not self.check_fixed_args(expr, 1, '_promote'):
@@ -1625,9 +1605,6 @@ def visit_cast_expr(self, expr: CastExpr) -> None:
16251605
expr.expr.accept(self)
16261606
expr.type = self.anal_type(expr.type)
16271607

1628-
def visit_undefined_expr(self, expr: UndefinedExpr) -> None:
1629-
expr.type = self.anal_type(expr.type)
1630-
16311608
def visit_type_application(self, expr: TypeApplication) -> None:
16321609
expr.expr.accept(self)
16331610
for i in range(len(expr.types)):
@@ -2006,9 +1983,6 @@ def visit_assignment_stmt(self, s: AssignmentStmt) -> None:
20061983
self.analyze(s.type)
20071984
super().visit_assignment_stmt(s)
20081985

2009-
def visit_undefined_expr(self, e: UndefinedExpr) -> None:
2010-
self.analyze(e.type)
2011-
20121986
def visit_cast_expr(self, e: CastExpr) -> None:
20131987
self.analyze(e.type)
20141988
super().visit_cast_expr(e)

mypy/strconv.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -378,9 +378,6 @@ def visit_index_expr(self, o):
378378
def visit_super_expr(self, o):
379379
return self.dump([o.name], o)
380380

381-
def visit_undefined_expr(self, o):
382-
return 'UndefinedExpr:{}({})'.format(o.line, o.type)
383-
384381
def visit_type_application(self, o):
385382
return self.dump([o.expr, ('Types', o.types)], o)
386383

mypy/test/testtypegen.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def run_test(self, testcase):
4545
nodes = map.keys()
4646

4747
# Ignore NameExpr nodes of variables with explicit (trivial) types
48-
# to simplify output. Also ignore 'Undefined' nodes.
48+
# to simplify output.
4949
searcher = VariableDefinitionNodeSearcher()
5050
for file in result.files.values():
5151
file.accept(searcher)
@@ -86,9 +86,6 @@ def visit_assignment_stmt(self, s):
8686
for lvalue in s.lvalues:
8787
if isinstance(lvalue, NameExpr):
8888
self.nodes.add(lvalue)
89-
if (isinstance(s.rvalue, NameExpr)
90-
and s.rvalue.fullname == 'typing.Undefined'):
91-
self.nodes.add(s.rvalue)
9289

9390

9491
def ignore_node(node):

mypy/treetransform.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
ConditionalExpr, DictExpr, SetExpr, NameExpr, IntExpr, StrExpr, BytesExpr,
1616
UnicodeExpr, FloatExpr, CallExpr, SuperExpr, MemberExpr, IndexExpr,
1717
SliceExpr, OpExpr, UnaryExpr, FuncExpr, TypeApplication, PrintStmt,
18-
SymbolTable, RefExpr, UndefinedExpr, TypeVarExpr, PromoteExpr,
18+
SymbolTable, RefExpr, TypeVarExpr, PromoteExpr,
1919
ComparisonExpr, TempNode, StarExpr, YieldFromStmt,
2020
YieldFromExpr, NamedTupleExpr, NonlocalDecl, SetComprehension,
2121
DictionaryComprehension, ComplexExpr, TypeAliasExpr
@@ -367,9 +367,6 @@ def visit_index_expr(self, node: IndexExpr) -> Node:
367367
new.analyzed.set_line(node.analyzed.line)
368368
return new
369369

370-
def visit_undefined_expr(self, node: UndefinedExpr) -> Node:
371-
return UndefinedExpr(self.type(node.type))
372-
373370
def visit_type_application(self, node: TypeApplication) -> TypeApplication:
374371
return TypeApplication(self.node(node.expr),
375372
self.types(node.types))

mypy/visitor.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,6 @@ def visit_set_expr(self, o: 'mypy.nodes.SetExpr') -> T:
184184
def visit_index_expr(self, o: 'mypy.nodes.IndexExpr') -> T:
185185
pass
186186

187-
def visit_undefined_expr(self, o: 'mypy.nodes.UndefinedExpr') -> T:
188-
pass
189-
190187
def visit_type_application(self, o: 'mypy.nodes.TypeApplication') -> T:
191188
pass
192189

0 commit comments

Comments
 (0)