Skip to content

Commit

Permalink
Add a call to register_transform for each numpy function in case the …
Browse files Browse the repository at this point in the history
…current node is an astroid.Name instance

The problem was that astroid could not infer the result of a call to `numpy.append`
because this function calls the `concatenate` function.
This last function is inferred thanks to the `brain_numpy_core_multiarray` module
but only when the corresponding node is an `astroid.Attribute` (for example numpy.concatenate). 
It turns out that in the source of the append function the node that realises the call to concatenate is a `astroid.Name`. Thus the correction proposed here is to register
the concatenate inference tip function in order to apply it, also, to `astroid.Name`.

Close #666
  • Loading branch information
hippo91 authored and PCManticore committed Jan 5, 2020
1 parent 67321ee commit 59f0be7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
6 changes: 6 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ What's New in astroid 2.4.0?
============================
Release Date: TBA


* Added a call to ``register_transform`` for all functions of the ``brain_numpy_core_multiarray``
module in case the current node is an instance of ``astroid.Name``

Close #666

* Added some functions to the ``brain_numpy_core_umath`` module

Close PyCQA/pylint#3319
Expand Down
5 changes: 5 additions & 0 deletions astroid/brain/brain_numpy_core_multiarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,8 @@ def vdot(a, b):
astroid.inference_tip(inference_function),
functools.partial(looks_like_numpy_member, method_name),
)
astroid.MANAGER.register_transform(
astroid.Name,
astroid.inference_tip(inference_function),
functools.partial(looks_like_numpy_member, method_name),
)
12 changes: 10 additions & 2 deletions astroid/brain/brain_numpy_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,17 @@ def looks_like_numpy_member(
:param node: node to test
:return: True if the node is a member of numpy
"""
return (
if (
isinstance(node, astroid.Attribute)
and node.attrname == member_name
and isinstance(node.expr, astroid.Name)
and _is_a_numpy_module(node.expr)
)
):
return True
if (
isinstance(node, astroid.Name)
and node.name == member_name
and node.root().name.startswith("numpy")
):
return True
return False

0 comments on commit 59f0be7

Please sign in to comment.