Skip to content

Commit ad2b32e

Browse files
gvanrossumJukkaL
authored andcommitted
Get rid of ErrorType (#3103)
Fixes #3063
1 parent 5b2c626 commit ad2b32e

12 files changed

+32
-126
lines changed

mypy/checker.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
from mypy import nodes
3232
from mypy.types import (
3333
Type, AnyType, CallableType, FunctionLike, Overloaded, TupleType, TypedDictType,
34-
Instance, NoneTyp, ErrorType, strip_type, TypeType,
34+
Instance, NoneTyp, strip_type, TypeType,
3535
UnionType, TypeVarId, TypeVarType, PartialType, DeletedType, UninhabitedType, TypeVarDef,
3636
true_only, false_only, function_type, is_named_instance
3737
)
@@ -2133,9 +2133,6 @@ def analyze_iterable_item_type(self, expr: Expression) -> Type:
21332133
joined = UninhabitedType() # type: Type
21342134
for item in iterable.items:
21352135
joined = join_types(joined, item)
2136-
if isinstance(joined, ErrorType):
2137-
self.fail(messages.CANNOT_INFER_ITEM_TYPE, expr)
2138-
return AnyType()
21392136
return joined
21402137
else:
21412138
# Non-tuple iterable.

mypy/erasetype.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from typing import Optional, Container, Callable
22

33
from mypy.types import (
4-
Type, TypeVisitor, UnboundType, ErrorType, AnyType, NoneTyp, TypeVarId,
4+
Type, TypeVisitor, UnboundType, AnyType, NoneTyp, TypeVarId,
55
Instance, TypeVarType, CallableType, TupleType, TypedDictType, UnionType, Overloaded,
66
ErasedType, PartialType, DeletedType, TypeTranslator, TypeList, UninhabitedType, TypeType
77
)
@@ -29,9 +29,6 @@ class EraseTypeVisitor(TypeVisitor[Type]):
2929
def visit_unbound_type(self, t: UnboundType) -> Type:
3030
assert False, 'Not supported'
3131

32-
def visit_error_type(self, t: ErrorType) -> Type:
33-
return t
34-
3532
def visit_type_list(self, t: TypeList) -> Type:
3633
assert False, 'Not supported'
3734

mypy/expandtype.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from typing import Dict, Iterable, List, TypeVar, Mapping, cast
22

33
from mypy.types import (
4-
Type, Instance, CallableType, TypeVisitor, UnboundType, ErrorType, AnyType,
4+
Type, Instance, CallableType, TypeVisitor, UnboundType, AnyType,
55
NoneTyp, TypeVarType, Overloaded, TupleType, TypedDictType, UnionType,
66
ErasedType, TypeList, PartialType, DeletedType, UninhabitedType, TypeType, TypeVarId,
77
FunctionLike, TypeVarDef
@@ -63,9 +63,6 @@ def __init__(self, variables: Mapping[TypeVarId, Type]) -> None:
6363
def visit_unbound_type(self, t: UnboundType) -> Type:
6464
return t
6565

66-
def visit_error_type(self, t: ErrorType) -> Type:
67-
return t
68-
6966
def visit_type_list(self, t: TypeList) -> Type:
7067
assert False, 'Not supported'
7168

mypy/indirection.py

-3
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,6 @@ def visit_unbound_type(self, t: types.UnboundType) -> Set[str]:
4545
def visit_type_list(self, t: types.TypeList) -> Set[str]:
4646
return self._visit(*t.items)
4747

48-
def visit_error_type(self, t: types.ErrorType) -> Set[str]:
49-
return set()
50-
5148
def visit_any(self, t: types.AnyType) -> Set[str]:
5249
return set()
5350

mypy/join.py

+2-16
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from mypy.types import (
77
Type, AnyType, NoneTyp, TypeVisitor, Instance, UnboundType,
8-
ErrorType, TypeVarType, CallableType, TupleType, TypedDictType, ErasedType, TypeList,
8+
TypeVarType, CallableType, TupleType, TypedDictType, ErasedType, TypeList,
99
UnionType, FunctionLike, Overloaded, PartialType, DeletedType,
1010
UninhabitedType, TypeType, true_or_false
1111
)
@@ -63,8 +63,6 @@ def join_types(s: Type, t: Type) -> Type:
6363
"""Return the least upper bound of s and t.
6464
6565
For example, the join of 'int' and 'object' is 'object'.
66-
67-
If the join does not exist, return an ErrorType instance.
6866
"""
6967
if (s.can_be_true, s.can_be_false) != (t.can_be_true, t.can_be_false):
7068
# if types are restricted in different ways, use the more general versions
@@ -101,20 +99,14 @@ def __init__(self, s: Type) -> None:
10199
self.s = s
102100

103101
def visit_unbound_type(self, t: UnboundType) -> Type:
104-
if isinstance(self.s, ErrorType):
105-
return ErrorType()
106-
else:
107-
return AnyType()
102+
return AnyType()
108103

109104
def visit_union_type(self, t: UnionType) -> Type:
110105
if is_subtype(self.s, t):
111106
return t
112107
else:
113108
return UnionType.make_simplified_union([self.s, t])
114109

115-
def visit_error_type(self, t: ErrorType) -> Type:
116-
return t
117-
118110
def visit_type_list(self, t: TypeList) -> Type:
119111
assert False, 'Not supported'
120112

@@ -127,8 +119,6 @@ def visit_none_type(self, t: NoneTyp) -> Type:
127119
return t
128120
elif isinstance(self.s, UnboundType):
129121
return AnyType()
130-
elif isinstance(self.s, ErrorType):
131-
return ErrorType()
132122
else:
133123
return UnionType.make_simplified_union([self.s, t])
134124
else:
@@ -264,8 +254,6 @@ def default(self, typ: Type) -> Type:
264254
return object_from_instance(typ)
265255
elif isinstance(typ, UnboundType):
266256
return AnyType()
267-
elif isinstance(typ, ErrorType):
268-
return ErrorType()
269257
elif isinstance(typ, TupleType):
270258
return self.default(typ.fallback)
271259
elif isinstance(typ, TypedDictType):
@@ -280,8 +268,6 @@ def default(self, typ: Type) -> Type:
280268

281269
def join_instances(t: Instance, s: Instance) -> Type:
282270
"""Calculate the join of two instance types.
283-
284-
Return ErrorType if the result is ambiguous.
285271
"""
286272
if t.type == s.type:
287273
# Simplest case: join two types with the same base type (but

mypy/meet.py

+11-27
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from mypy.join import is_similar_callables, combine_similar_callables, join_type_list
55
from mypy.types import (
6-
Type, AnyType, TypeVisitor, UnboundType, ErrorType, NoneTyp, TypeVarType,
6+
Type, AnyType, TypeVisitor, UnboundType, NoneTyp, TypeVarType,
77
Instance, CallableType, TupleType, TypedDictType, ErasedType, TypeList, UnionType, PartialType,
88
DeletedType, UninhabitedType, TypeType
99
)
@@ -124,9 +124,7 @@ def __init__(self, s: Type) -> None:
124124
self.s = s
125125

126126
def visit_unbound_type(self, t: UnboundType) -> Type:
127-
if isinstance(self.s, ErrorType):
128-
return ErrorType()
129-
elif isinstance(self.s, NoneTyp):
127+
if isinstance(self.s, NoneTyp):
130128
if experiments.STRICT_OPTIONAL:
131129
return AnyType()
132130
else:
@@ -136,9 +134,6 @@ def visit_unbound_type(self, t: UnboundType) -> Type:
136134
else:
137135
return AnyType()
138136

139-
def visit_error_type(self, t: ErrorType) -> Type:
140-
return t
141-
142137
def visit_type_list(self, t: TypeList) -> Type:
143138
assert False, 'Not supported'
144139

@@ -164,30 +159,21 @@ def visit_none_type(self, t: NoneTyp) -> Type:
164159
else:
165160
return UninhabitedType()
166161
else:
167-
if not isinstance(self.s, ErrorType):
168-
return t
169-
else:
170-
return ErrorType()
162+
return t
171163

172164
def visit_uninhabited_type(self, t: UninhabitedType) -> Type:
173-
if not isinstance(self.s, ErrorType):
174-
return t
175-
else:
176-
return ErrorType()
165+
return t
177166

178167
def visit_deleted_type(self, t: DeletedType) -> Type:
179-
if not isinstance(self.s, ErrorType):
180-
if isinstance(self.s, NoneTyp):
181-
if experiments.STRICT_OPTIONAL:
182-
return t
183-
else:
184-
return self.s
185-
elif isinstance(self.s, UninhabitedType):
186-
return self.s
187-
else:
168+
if isinstance(self.s, NoneTyp):
169+
if experiments.STRICT_OPTIONAL:
188170
return t
171+
else:
172+
return self.s
173+
elif isinstance(self.s, UninhabitedType):
174+
return self.s
189175
else:
190-
return ErrorType()
176+
return t
191177

192178
def visit_erased_type(self, t: ErasedType) -> Type:
193179
return self.s
@@ -288,8 +274,6 @@ def meet(self, s: Type, t: Type) -> Type:
288274
def default(self, typ: Type) -> Type:
289275
if isinstance(typ, UnboundType):
290276
return AnyType()
291-
elif isinstance(typ, ErrorType):
292-
return ErrorType()
293277
else:
294278
if experiments.STRICT_OPTIONAL:
295279
return UninhabitedType()

mypy/sametypes.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from typing import Sequence
22

33
from mypy.types import (
4-
Type, UnboundType, ErrorType, AnyType, NoneTyp, TupleType, TypedDictType,
4+
Type, UnboundType, AnyType, NoneTyp, TupleType, TypedDictType,
55
UnionType, CallableType, TypeVarType, Instance, TypeVisitor, ErasedType,
66
TypeList, Overloaded, PartialType, DeletedType, UninhabitedType, TypeType
77
)
@@ -55,9 +55,6 @@ def __init__(self, right: Type) -> None:
5555
def visit_unbound_type(self, left: UnboundType) -> bool:
5656
return True
5757

58-
def visit_error_type(self, left: ErrorType) -> bool:
59-
return False
60-
6158
def visit_type_list(self, t: TypeList) -> bool:
6259
assert False, 'Not supported'
6360

mypy/solve.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from typing import List, Dict
44
from collections import defaultdict
55

6-
from mypy.types import Type, NoneTyp, AnyType, ErrorType, UninhabitedType, TypeVarId
6+
from mypy.types import Type, NoneTyp, AnyType, UninhabitedType, TypeVarId
77
from mypy.constraints import Constraint, SUPERTYPE_OF
88
from mypy.join import join_types
99
from mypy.meet import meet_types
@@ -68,9 +68,6 @@ def solve_constraints(vars: List[TypeVarId], constraints: List[Constraint],
6868
candidate = bottom
6969
else:
7070
candidate = None
71-
if isinstance(candidate, ErrorType):
72-
res.append(None)
73-
else:
74-
res.append(candidate)
71+
res.append(candidate)
7572

7673
return res

mypy/subtypes.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from typing import List, Optional, Dict, Callable
22

33
from mypy.types import (
4-
Type, AnyType, UnboundType, TypeVisitor, ErrorType, FormalArgument, NoneTyp,
4+
Type, AnyType, UnboundType, TypeVisitor, FormalArgument, NoneTyp,
55
Instance, TypeVarType, CallableType, TupleType, TypedDictType, UnionType, Overloaded,
66
ErasedType, TypeList, PartialType, DeletedType, UninhabitedType, TypeType, is_named_instance
77
)
@@ -94,9 +94,6 @@ def __init__(self, right: Type,
9494
def visit_unbound_type(self, left: UnboundType) -> bool:
9595
return True
9696

97-
def visit_error_type(self, left: ErrorType) -> bool:
98-
return False
99-
10097
def visit_type_list(self, t: TypeList) -> bool:
10198
assert False, 'Not supported'
10299

mypy/test/testtypes.py

+11-34
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from mypy.meet import meet_types
1212
from mypy.types import (
1313
UnboundType, AnyType, CallableType, TupleType, TypeVarDef, Type,
14-
Instance, NoneTyp, ErrorType, Overloaded, TypeType, UnionType, UninhabitedType,
14+
Instance, NoneTyp, Overloaded, TypeType, UnionType, UninhabitedType,
1515
true_only, false_only, TypeVarId
1616
)
1717
from mypy.nodes import ARG_POS, ARG_OPT, ARG_STAR, CONTRAVARIANT, INVARIANT, COVARIANT
@@ -136,8 +136,7 @@ def assert_expand(self,
136136
# erase_type
137137

138138
def test_trivial_erase(self) -> None:
139-
for t in (self.fx.a, self.fx.o, self.fx.nonet,
140-
self.fx.anyt, self.fx.err):
139+
for t in (self.fx.a, self.fx.o, self.fx.nonet, self.fx.anyt):
141140
self.assert_erase(t, t)
142141

143142
def test_erase_with_type_variable(self) -> None:
@@ -456,15 +455,6 @@ def test_other_mixed_types(self) -> None:
456455
if str(t1) != str(t2):
457456
self.assert_join(t1, t2, self.fx.o)
458457

459-
def test_error_type(self) -> None:
460-
self.assert_join(self.fx.err, self.fx.anyt, self.fx.anyt)
461-
462-
# Meet against any type except dynamic results in ErrorType.
463-
for t in [self.fx.a, self.fx.o, NoneTyp(), UnboundType('x'),
464-
self.fx.t, self.tuple(),
465-
self.callable(self.fx.a, self.fx.b)]:
466-
self.assert_join(t, self.fx.err, self.fx.err)
467-
468458
def test_simple_generics(self) -> None:
469459
self.assert_join(self.fx.ga, self.fx.ga, self.fx.ga)
470460
self.assert_join(self.fx.ga, self.fx.gb, self.fx.ga)
@@ -553,7 +543,7 @@ def test_join_class_types_with_interface_result(self) -> None:
553543
self.assert_join(self.fx.e, self.fx.e2, self.fx.f)
554544

555545
# Ambiguous result
556-
self.assert_join(self.fx.e2, self.fx.e3, self.fx.err)
546+
self.assert_join(self.fx.e2, self.fx.e3, self.fx.anyt)
557547

558548
def test_generic_interfaces(self) -> None:
559549
self.skip() # FIX
@@ -604,12 +594,10 @@ def assert_simple_join(self, s: Type, t: Type, join: Type) -> None:
604594
expected = str(join)
605595
assert_equal(actual, expected,
606596
'join({}, {}) == {{}} ({{}} expected)'.format(s, t))
607-
if not isinstance(s, ErrorType) and not isinstance(result, ErrorType):
608-
assert_true(is_subtype(s, result),
609-
'{} not subtype of {}'.format(s, result))
610-
if not isinstance(t, ErrorType) and not isinstance(result, ErrorType):
611-
assert_true(is_subtype(t, result),
612-
'{} not subtype of {}'.format(t, result))
597+
assert_true(is_subtype(s, result),
598+
'{} not subtype of {}'.format(s, result))
599+
assert_true(is_subtype(t, result),
600+
'{} not subtype of {}'.format(t, result))
613601

614602
def tuple(self, *a: Type) -> TupleType:
615603
return TupleType(list(a), self.fx.std_tuple)
@@ -710,15 +698,6 @@ def test_dynamic_type(self) -> None:
710698
self.callable(self.fx.a, self.fx.b)]:
711699
self.assert_meet(t, self.fx.anyt, t)
712700

713-
def test_error_type(self) -> None:
714-
self.assert_meet(self.fx.err, self.fx.anyt, self.fx.err)
715-
716-
# Meet against any type except dynamic results in ErrorType.
717-
for t in [self.fx.a, self.fx.o, NoneTyp(), UnboundType('x'),
718-
self.fx.t, self.tuple(),
719-
self.callable(self.fx.a, self.fx.b)]:
720-
self.assert_meet(t, self.fx.err, self.fx.err)
721-
722701
def test_simple_generics(self) -> None:
723702
self.assert_meet(self.fx.ga, self.fx.ga, self.fx.ga)
724703
self.assert_meet(self.fx.ga, self.fx.o, self.fx.ga)
@@ -805,12 +784,10 @@ def assert_simple_meet(self, s: Type, t: Type, meet: Type) -> None:
805784
expected = str(meet)
806785
assert_equal(actual, expected,
807786
'meet({}, {}) == {{}} ({{}} expected)'.format(s, t))
808-
if not isinstance(s, ErrorType) and not isinstance(result, ErrorType):
809-
assert_true(is_subtype(result, s),
810-
'{} not subtype of {}'.format(result, s))
811-
if not isinstance(t, ErrorType) and not isinstance(result, ErrorType):
812-
assert_true(is_subtype(result, t),
813-
'{} not subtype of {}'.format(result, t))
787+
assert_true(is_subtype(result, s),
788+
'{} not subtype of {}'.format(result, s))
789+
assert_true(is_subtype(result, t),
790+
'{} not subtype of {}'.format(result, t))
814791

815792
def tuple(self, *a: Type) -> TupleType:
816793
return TupleType(list(a), self.fx.std_tuple)

mypy/typefixture.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from typing import List
77

88
from mypy.types import (
9-
Type, TypeVarType, AnyType, ErrorType, NoneTyp,
9+
Type, TypeVarType, AnyType, NoneTyp,
1010
Instance, CallableType, TypeVarDef, TypeType, UninhabitedType
1111
)
1212
from mypy.nodes import (
@@ -41,7 +41,6 @@ def make_type_var(name: str, id: int, values: List[Type], upper_bound: Type,
4141

4242
# Simple types
4343
self.anyt = AnyType()
44-
self.err = ErrorType()
4544
self.nonet = NoneTyp()
4645
self.uninhabited = UninhabitedType()
4746

0 commit comments

Comments
 (0)