From b697d54da14b9ae978e9fb166a52af70bce0693c Mon Sep 17 00:00:00 2001 From: hippo91 Date: Thu, 26 Dec 2019 10:52:36 +0100 Subject: [PATCH 1/4] The method looks_like_numpy_member is able to deal with astroid.Name nodes --- astroid/brain/brain_numpy_utils.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/astroid/brain/brain_numpy_utils.py b/astroid/brain/brain_numpy_utils.py index 2bad01eed4..250f489378 100644 --- a/astroid/brain/brain_numpy_utils.py +++ b/astroid/brain/brain_numpy_utils.py @@ -48,9 +48,13 @@ def looks_like_numpy_member( :param node: node to test :return: True if the node is a member of numpy """ - return ( - isinstance(node, astroid.Attribute) + if (isinstance(node, astroid.Attribute) and node.attrname == member_name and isinstance(node.expr, astroid.Name) - and _is_a_numpy_module(node.expr) - ) + 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 From c640008059134005e513eb66ac98693a77e4e90b Mon Sep 17 00:00:00 2001 From: hippo91 Date: Thu, 26 Dec 2019 10:55:00 +0100 Subject: [PATCH 2/4] Adds a call to register_transform for each function in case the current node is an astroid.Name instance --- astroid/brain/brain_numpy_core_multiarray.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/astroid/brain/brain_numpy_core_multiarray.py b/astroid/brain/brain_numpy_core_multiarray.py index 182184366e..8f5e32cc02 100644 --- a/astroid/brain/brain_numpy_core_multiarray.py +++ b/astroid/brain/brain_numpy_core_multiarray.py @@ -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), + ) From 3f9185503322486bc4e4bc31db864933b7253fc8 Mon Sep 17 00:00:00 2001 From: hippo91 Date: Sat, 28 Dec 2019 14:33:50 +0100 Subject: [PATCH 3/4] Reformatting according to black --- astroid/brain/brain_numpy_utils.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/astroid/brain/brain_numpy_utils.py b/astroid/brain/brain_numpy_utils.py index 250f489378..c51922c38d 100644 --- a/astroid/brain/brain_numpy_utils.py +++ b/astroid/brain/brain_numpy_utils.py @@ -48,13 +48,17 @@ def looks_like_numpy_member( :param node: node to test :return: True if the node is a member of numpy """ - if (isinstance(node, astroid.Attribute) + if ( + isinstance(node, astroid.Attribute) and node.attrname == member_name and isinstance(node.expr, astroid.Name) - and _is_a_numpy_module(node.expr)): + 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')): + if ( + isinstance(node, astroid.Name) + and node.name == member_name + and node.root().name.startswith("numpy") + ): return True return False From d487e0ab65234016881a364665232f14c4c803ba Mon Sep 17 00:00:00 2001 From: hippo91 Date: Fri, 27 Dec 2019 14:51:02 +0100 Subject: [PATCH 4/4] Add an entry --- ChangeLog | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/ChangeLog b/ChangeLog index b612aee083..54826817ea 100644 --- a/ChangeLog +++ b/ChangeLog @@ -6,6 +6,11 @@ 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 of the ``numpy.core.multiarray`` module Close PyCQA/pylint#3208