diff options
author | Jacob Walls <jacobtylerwalls@gmail.com> | 2023-05-14 16:33:33 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-14 16:33:33 -0400 |
commit | 835de848ac7cf51525d714f2f6ed07d789e09c54 (patch) | |
tree | 4f8cdb0f7d5b96708efd0c13e217e7e1e36261c8 /astroid/brain/brain_numpy_utils.py | |
parent | 14eeb3fd64826c4a42a4c2f4edbac2476528dedc (diff) | |
download | astroid-git-835de848ac7cf51525d714f2f6ed07d789e09c54.tar.gz |
Improve performance of `looks_like_numpy_member()` (#2178)
Avoids 32% of the calls to isinstance() when linting astroid
Diffstat (limited to 'astroid/brain/brain_numpy_utils.py')
-rw-r--r-- | astroid/brain/brain_numpy_utils.py | 31 |
1 files changed, 13 insertions, 18 deletions
diff --git a/astroid/brain/brain_numpy_utils.py b/astroid/brain/brain_numpy_utils.py index 5867b6f9..47f24433 100644 --- a/astroid/brain/brain_numpy_utils.py +++ b/astroid/brain/brain_numpy_utils.py @@ -8,7 +8,7 @@ from __future__ import annotations from astroid.builder import extract_node from astroid.context import InferenceContext -from astroid.nodes.node_classes import Attribute, Import, Name, NodeNG +from astroid.nodes.node_classes import Attribute, Import, Name # Class subscript is available in numpy starting with version 1.20.0 NUMPY_VERSION_TYPE_HINTS_SUPPORT = ("1", "20", "0") @@ -61,26 +61,21 @@ def _is_a_numpy_module(node: Name) -> bool: ) -def looks_like_numpy_member(member_name: str, node: NodeNG) -> bool: +def name_looks_like_numpy_member(member_name: str, node: Name) -> bool: """ - Returns True if the node is a member of numpy whose + Returns True if the Name is a member of numpy whose name is member_name. + """ + return node.name == member_name and node.root().name.startswith("numpy") - :param member_name: name of the member - :param node: node to test - :return: True if the node is a member of numpy + +def attribute_looks_like_numpy_member(member_name: str, node: Attribute) -> bool: """ - if ( - isinstance(node, Attribute) - and node.attrname == member_name + Returns True if the Attribute is a member of numpy whose + name is member_name. + """ + return ( + node.attrname == member_name and isinstance(node.expr, Name) and _is_a_numpy_module(node.expr) - ): - return True - if ( - isinstance(node, Name) - and node.name == member_name - and node.root().name.startswith("numpy") - ): - return True - return False + ) |