Skip to content

Commit

Permalink
Improve performance of looks_like_numpy_member() (#2178)
Browse files Browse the repository at this point in the history
Avoids 32% of the calls to isinstance() when linting astroid
  • Loading branch information
jacobtylerwalls authored May 14, 2023
1 parent 14eeb3f commit 835de84
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 25 deletions.
7 changes: 5 additions & 2 deletions astroid/brain/brain_numpy_core_function_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

import functools

from astroid.brain.brain_numpy_utils import infer_numpy_member, looks_like_numpy_member
from astroid.brain.brain_numpy_utils import (
attribute_looks_like_numpy_member,
infer_numpy_member,
)
from astroid.inference_tip import inference_tip
from astroid.manager import AstroidManager
from astroid.nodes.node_classes import Attribute
Expand All @@ -25,5 +28,5 @@
AstroidManager().register_transform(
Attribute,
inference_tip(inference_function),
functools.partial(looks_like_numpy_member, func_name),
functools.partial(attribute_looks_like_numpy_member, func_name),
)
10 changes: 7 additions & 3 deletions astroid/brain/brain_numpy_core_multiarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@

import functools

from astroid.brain.brain_numpy_utils import infer_numpy_member, looks_like_numpy_member
from astroid.brain.brain_numpy_utils import (
attribute_looks_like_numpy_member,
infer_numpy_member,
name_looks_like_numpy_member,
)
from astroid.brain.helpers import register_module_extender
from astroid.builder import parse
from astroid.inference_tip import inference_tip
Expand Down Expand Up @@ -91,10 +95,10 @@ def vdot(a, b):
AstroidManager().register_transform(
Attribute,
inference_tip(inference_function),
functools.partial(looks_like_numpy_member, method_name),
functools.partial(attribute_looks_like_numpy_member, method_name),
)
AstroidManager().register_transform(
Name,
inference_tip(inference_function),
functools.partial(looks_like_numpy_member, method_name),
functools.partial(name_looks_like_numpy_member, method_name),
)
7 changes: 5 additions & 2 deletions astroid/brain/brain_numpy_core_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

import functools

from astroid.brain.brain_numpy_utils import infer_numpy_member, looks_like_numpy_member
from astroid.brain.brain_numpy_utils import (
attribute_looks_like_numpy_member,
infer_numpy_member,
)
from astroid.brain.helpers import register_module_extender
from astroid.builder import parse
from astroid.inference_tip import inference_tip
Expand Down Expand Up @@ -42,5 +45,5 @@ def full_like(a, fill_value, dtype=None, order='K', subok=True, shape=None): ret
AstroidManager().register_transform(
Attribute,
inference_tip(inference_function),
functools.partial(looks_like_numpy_member, method_name),
functools.partial(attribute_looks_like_numpy_member, method_name),
)
31 changes: 13 additions & 18 deletions astroid/brain/brain_numpy_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

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")
Expand Down Expand Up @@ -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
)

0 comments on commit 835de84

Please sign in to comment.