Skip to content

Commit abf9d0e

Browse files
authored
Merge pull request #884 from hippo91/sum_and_multiply_clean
Sum and multiply clean
2 parents 7c7848b + 7c614b3 commit abf9d0e

File tree

6 files changed

+20
-11
lines changed

6 files changed

+20
-11
lines changed

ChangeLog

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
astroid's ChangeLog
33
===================
44

5+
* The ``context.path`` is now a ``dict`` and the ``context.push`` method
6+
returns ``True`` if the node has been visited a certain amount of times.
7+
8+
Close #669
59

610
What's New in astroid 2.5.0?
711
============================

astroid/brain/brain_numpy_core_umath.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ def __call__(self, x1, x2, {opt_args:s}):
106106
trunc = FakeUfuncOneArg()
107107
108108
# Two args functions with optional kwargs
109+
add = FakeUfuncTwoArgs()
109110
bitwise_and = FakeUfuncTwoArgs()
110111
bitwise_or = FakeUfuncTwoArgs()
111112
bitwise_xor = FakeUfuncTwoArgs()
@@ -133,6 +134,7 @@ def __call__(self, x1, x2, {opt_args:s}):
133134
logical_xor = FakeUfuncTwoArgs()
134135
maximum = FakeUfuncTwoArgs()
135136
minimum = FakeUfuncTwoArgs()
137+
multiply = FakeUfuncTwoArgs()
136138
nextafter = FakeUfuncTwoArgs()
137139
not_equal = FakeUfuncTwoArgs()
138140
power = FakeUfuncTwoArgs()

astroid/context.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ class InferenceContext:
2929
"extra_context",
3030
)
3131

32+
maximum_path_visit = 3
33+
3234
def __init__(self, path=None, inferred=None):
33-
self.path = path or set()
35+
self.path = path or dict()
3436
"""
3537
:type: set(tuple(NodeNG, optional(str)))
3638
@@ -87,10 +89,10 @@ def push(self, node):
8789
Allows one to see if the given node has already
8890
been looked at for this inference context"""
8991
name = self.lookupname
90-
if (node, name) in self.path:
92+
if self.path.get((node, name), 0) >= self.maximum_path_visit:
9193
return True
9294

93-
self.path.add((node, name))
95+
self.path[(node, name)] = self.path.setdefault((node, name), 0) + 1
9496
return False
9597

9698
def clone(self):
@@ -108,7 +110,7 @@ def clone(self):
108110

109111
@contextlib.contextmanager
110112
def restore_path(self):
111-
path = set(self.path)
113+
path = dict(self.path)
112114
yield
113115
self.path = path
114116

tests/unittest_brain_numpy_core_umath.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class NumpyBrainCoreUmathTest(unittest.TestCase):
6565
)
6666

6767
two_args_ufunc = (
68+
"add",
6869
"bitwise_and",
6970
"bitwise_or",
7071
"bitwise_xor",
@@ -92,6 +93,7 @@ class NumpyBrainCoreUmathTest(unittest.TestCase):
9293
"logical_xor",
9394
"maximum",
9495
"minimum",
96+
"multiply",
9597
"nextafter",
9698
"not_equal",
9799
"power",
@@ -224,11 +226,9 @@ def test_numpy_core_umath_functions_return_type(self):
224226
with self.subTest(typ=func_):
225227
inferred_values = list(self._inferred_numpy_func_call(func_))
226228
self.assertTrue(
227-
len(inferred_values) == 1
228-
or len(inferred_values) == 2
229-
and inferred_values[-1].pytype() is util.Uninferable,
229+
len(inferred_values) == 1,
230230
msg="Too much inferred values ({}) for {:s}".format(
231-
inferred_values[-1].pytype(), func_
231+
inferred_values, func_
232232
),
233233
)
234234
self.assertTrue(

tests/unittest_inference.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,7 +1301,7 @@ def get_context_data(self, **kwargs):
13011301
result = node.inferred()
13021302
assert len(result) == 2
13031303
assert isinstance(result[0], nodes.Dict)
1304-
assert result[1] is util.Uninferable
1304+
assert isinstance(result[1], nodes.Dict)
13051305

13061306
def test_python25_no_relative_import(self):
13071307
ast = resources.build_file("data/package/absimport.py")
@@ -3656,7 +3656,8 @@ def __getitem__(self, name):
36563656
flow = AttributeDict()
36573657
flow['app'] = AttributeDict()
36583658
flow['app']['config'] = AttributeDict()
3659-
flow['app']['config']['doffing'] = AttributeDict() #@
3659+
flow['app']['config']['doffing'] = AttributeDict()
3660+
flow['app']['config']['doffing']['thinkto'] = AttributeDict() #@
36603661
"""
36613662
)
36623663
self.assertIsNone(helpers.safe_infer(ast_node.targets[0]))

tests/unittest_regrtest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def test_numpy_crash(self):
9393
data = """
9494
from numpy import multiply
9595
96-
multiply(1, 2, 3)
96+
multiply([1, 2], [3, 4])
9797
"""
9898
astroid = builder.string_build(data, __name__, __file__)
9999
callfunc = astroid.body[1].value.func

0 commit comments

Comments
 (0)