Move some code from SequenceLiteralValue to DictLiteralValue

This commit is contained in:
Dave Halter
2019-08-28 23:39:23 +02:00
parent 4572503c9f
commit 9c950321df

View File

@@ -323,19 +323,6 @@ class SequenceLiteralValue(Sequence):
def py__simple_getitem__(self, index): def py__simple_getitem__(self, index):
"""Here the index is an int/str. Raises IndexError/KeyError.""" """Here the index is an int/str. Raises IndexError/KeyError."""
if self.array_type == u'dict':
compiled_obj_index = compiled.create_simple_object(self.inference_state, index)
for key, value in self.get_tree_entries():
for k in self._defining_context.infer_node(key):
try:
method = k.execute_operation
except AttributeError:
pass
else:
if method(compiled_obj_index, u'==').get_safe_value():
return self._defining_context.infer_node(value)
raise SimpleGetItemNotFound('No key found in dictionary %s.' % self)
if isinstance(index, slice): if isinstance(index, slice):
return ValueSet([self]) return ValueSet([self])
else: else:
@@ -348,16 +335,6 @@ class SequenceLiteralValue(Sequence):
While values returns the possible values for any array field, this While values returns the possible values for any array field, this
function returns the value for a certain index. function returns the value for a certain index.
""" """
if self.array_type == u'dict':
# Get keys.
types = NO_VALUES
for k, _ in self.get_tree_entries():
types |= self._defining_context.infer_node(k)
# We don't know which dict index comes first, therefore always
# yield all the types.
for _ in types:
yield LazyKnownValues(types)
else:
for node in self.get_tree_entries(): for node in self.get_tree_entries():
if node == ':' or node.type == 'subscript': if node == ':' or node.type == 'subscript':
# TODO this should probably use at least part of the code # TODO this should probably use at least part of the code
@@ -372,12 +349,6 @@ class SequenceLiteralValue(Sequence):
# This function is not really used often. It's more of a try. # This function is not really used often. It's more of a try.
return len(self.get_tree_entries()) return len(self.get_tree_entries())
def _dict_values(self):
return ValueSet.from_sets(
self._defining_context.infer_node(v)
for k, v in self.get_tree_entries()
)
def get_tree_entries(self): def get_tree_entries(self):
c = self.atom.children c = self.atom.children
@@ -445,6 +416,34 @@ class DictLiteralValue(_DictMixin, SequenceLiteralValue):
self._defining_context = defining_context self._defining_context = defining_context
self.atom = atom self.atom = atom
def py__simple_getitem__(self, index):
"""Here the index is an int/str. Raises IndexError/KeyError."""
compiled_obj_index = compiled.create_simple_object(self.inference_state, index)
for key, value in self.get_tree_entries():
for k in self._defining_context.infer_node(key):
try:
method = k.execute_operation
except AttributeError:
pass
else:
if method(compiled_obj_index, u'==').get_safe_value():
return self._defining_context.infer_node(value)
raise SimpleGetItemNotFound('No key found in dictionary %s.' % self)
def py__iter__(self, contextualized_node=None):
"""
While values returns the possible values for any array field, this
function returns the value for a certain index.
"""
# Get keys.
types = NO_VALUES
for k, _ in self.get_tree_entries():
types |= self._defining_context.infer_node(k)
# We don't know which dict index comes first, therefore always
# yield all the types.
for _ in types:
yield LazyKnownValues(types)
@publish_method('values') @publish_method('values')
def _imitate_values(self): def _imitate_values(self):
lazy_value = LazyKnownValues(self._dict_values()) lazy_value = LazyKnownValues(self._dict_values())
@@ -462,6 +461,12 @@ class DictLiteralValue(_DictMixin, SequenceLiteralValue):
return ValueSet([FakeList(self.inference_state, lazy_values)]) return ValueSet([FakeList(self.inference_state, lazy_values)])
def _dict_values(self):
return ValueSet.from_sets(
self._defining_context.infer_node(v)
for k, v in self.get_tree_entries()
)
def _dict_keys(self): def _dict_keys(self):
return ValueSet.from_sets( return ValueSet.from_sets(
self._defining_context.infer_node(k) self._defining_context.infer_node(k)