Commit 7d8e5ed0 authored by Stefan Behnel's avatar Stefan Behnel

Prevent None assignments from disrupting safe type inference.

parent 6d2b5074
...@@ -415,6 +415,22 @@ class SimpleAssignmentTypeInferer(object): ...@@ -415,6 +415,22 @@ class SimpleAssignmentTypeInferer(object):
entry = node.entry entry = node.entry
return spanning_type(types, entry.might_overflow, entry.pos, scope) return spanning_type(types, entry.might_overflow, entry.pos, scope)
def inferred_types(entry):
has_none = False
has_pyobjects = False
types = []
for assmt in entry.cf_assignments:
if assmt.rhs.is_none:
has_none = True
else:
rhs_type = assmt.inferred_type
if rhs_type and rhs_type.is_pyobject:
has_pyobjects = True
types.append(rhs_type)
if has_none and not has_pyobjects:
types.append(py_object_type)
return types
def resolve_assignments(assignments): def resolve_assignments(assignments):
resolved = set() resolved = set()
for assmt in assignments: for assmt in assignments:
...@@ -467,7 +483,7 @@ class SimpleAssignmentTypeInferer(object): ...@@ -467,7 +483,7 @@ class SimpleAssignmentTypeInferer(object):
continue continue
entry_type = py_object_type entry_type = py_object_type
if assmts_resolved.issuperset(entry.cf_assignments): if assmts_resolved.issuperset(entry.cf_assignments):
types = [assmt.inferred_type for assmt in entry.cf_assignments] types = inferred_types(entry)
if types and all(types): if types and all(types):
entry_type = spanning_type( entry_type = spanning_type(
types, entry.might_overflow, entry.pos, scope) types, entry.might_overflow, entry.pos, scope)
...@@ -477,8 +493,9 @@ class SimpleAssignmentTypeInferer(object): ...@@ -477,8 +493,9 @@ class SimpleAssignmentTypeInferer(object):
def reinfer(): def reinfer():
dirty = False dirty = False
for entry in inferred: for entry in inferred:
types = [assmt.infer_type() for assmt in entry.cf_assignments:
for assmt in entry.cf_assignments] assmt.infer_type()
types = inferred_types(entry)
new_type = spanning_type(types, entry.might_overflow, entry.pos, scope) new_type = spanning_type(types, entry.might_overflow, entry.pos, scope)
if new_type != entry.type: if new_type != entry.type:
self.set_entry_type(entry, new_type) self.set_entry_type(entry, new_type)
......
...@@ -137,15 +137,21 @@ def multiple_assignments(): ...@@ -137,15 +137,21 @@ def multiple_assignments():
a = 3 a = 3
a = 4 a = 4
a = 5 a = 5
assert typeof(a) == "long" assert typeof(a) == "long", typeof(a)
b = a b = a
b = 3.1 b = 3.1
b = 3.14159 b = 3.14159
assert typeof(b) == "double" assert typeof(b) == "double", typeof(b)
c = a c = a
c = b c = b
c = [1,2,3] c = [1,2,3]
assert typeof(c) == "Python object" assert typeof(c) == "Python object", typeof(c)
d = b'abc'
d = bytes()
d = bytes(b'xyz')
d = None
assert typeof(d) == "bytes object", typeof(d)
def arithmetic(): def arithmetic():
""" """
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment