Skip to content

Sum and multiply clean #884

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jan 24, 2021
Merged
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
astroid's ChangeLog
===================

* The ``context.path`` is now a ``dict`` and the ``context.push`` method
returns ``True`` if the node has been visited a certain amount of times.

Close #669

What's New in astroid 2.5.0?
============================
Expand Down
2 changes: 2 additions & 0 deletions astroid/brain/brain_numpy_core_umath.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ def __call__(self, x1, x2, {opt_args:s}):
trunc = FakeUfuncOneArg()

# Two args functions with optional kwargs
add = FakeUfuncTwoArgs()
bitwise_and = FakeUfuncTwoArgs()
bitwise_or = FakeUfuncTwoArgs()
bitwise_xor = FakeUfuncTwoArgs()
Expand Down Expand Up @@ -133,6 +134,7 @@ def __call__(self, x1, x2, {opt_args:s}):
logical_xor = FakeUfuncTwoArgs()
maximum = FakeUfuncTwoArgs()
minimum = FakeUfuncTwoArgs()
multiply = FakeUfuncTwoArgs()
nextafter = FakeUfuncTwoArgs()
not_equal = FakeUfuncTwoArgs()
power = FakeUfuncTwoArgs()
Expand Down
10 changes: 6 additions & 4 deletions astroid/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ class InferenceContext:
"extra_context",
)

maximum_path_visit = 3

def __init__(self, path=None, inferred=None):
self.path = path or set()
self.path = path or dict()
"""
:type: set(tuple(NodeNG, optional(str)))

Expand Down Expand Up @@ -87,10 +89,10 @@ def push(self, node):
Allows one to see if the given node has already
been looked at for this inference context"""
name = self.lookupname
if (node, name) in self.path:
if self.path.get((node, name), 0) >= self.maximum_path_visit:
return True

self.path.add((node, name))
self.path[(node, name)] = self.path.setdefault((node, name), 0) + 1
return False

def clone(self):
Expand All @@ -108,7 +110,7 @@ def clone(self):

@contextlib.contextmanager
def restore_path(self):
path = set(self.path)
path = dict(self.path)
yield
self.path = path

Expand Down
8 changes: 4 additions & 4 deletions tests/unittest_brain_numpy_core_umath.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class NumpyBrainCoreUmathTest(unittest.TestCase):
)

two_args_ufunc = (
"add",
"bitwise_and",
"bitwise_or",
"bitwise_xor",
Expand Down Expand Up @@ -92,6 +93,7 @@ class NumpyBrainCoreUmathTest(unittest.TestCase):
"logical_xor",
"maximum",
"minimum",
"multiply",
"nextafter",
"not_equal",
"power",
Expand Down Expand Up @@ -224,11 +226,9 @@ def test_numpy_core_umath_functions_return_type(self):
with self.subTest(typ=func_):
inferred_values = list(self._inferred_numpy_func_call(func_))
self.assertTrue(
len(inferred_values) == 1
or len(inferred_values) == 2
and inferred_values[-1].pytype() is util.Uninferable,
len(inferred_values) == 1,
msg="Too much inferred values ({}) for {:s}".format(
inferred_values[-1].pytype(), func_
inferred_values, func_
),
)
self.assertTrue(
Expand Down
5 changes: 3 additions & 2 deletions tests/unittest_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -1301,7 +1301,7 @@ def get_context_data(self, **kwargs):
result = node.inferred()
assert len(result) == 2
assert isinstance(result[0], nodes.Dict)
assert result[1] is util.Uninferable
assert isinstance(result[1], nodes.Dict)

def test_python25_no_relative_import(self):
ast = resources.build_file("data/package/absimport.py")
Expand Down Expand Up @@ -3656,7 +3656,8 @@ def __getitem__(self, name):
flow = AttributeDict()
flow['app'] = AttributeDict()
flow['app']['config'] = AttributeDict()
flow['app']['config']['doffing'] = AttributeDict() #@
flow['app']['config']['doffing'] = AttributeDict()
flow['app']['config']['doffing']['thinkto'] = AttributeDict() #@
"""
)
self.assertIsNone(helpers.safe_infer(ast_node.targets[0]))
Expand Down
2 changes: 1 addition & 1 deletion tests/unittest_regrtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def test_numpy_crash(self):
data = """
from numpy import multiply

multiply(1, 2, 3)
multiply([1, 2], [3, 4])
"""
astroid = builder.string_build(data, __name__, __file__)
callfunc = astroid.body[1].value.func
Expand Down