Skip to content

Commit

Permalink
Don't call inference in the functools transform
Browse files Browse the repository at this point in the history
  • Loading branch information
PCManticore committed Oct 11, 2018
1 parent 8230d73 commit cab26ec
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions astroid/brain/brain_functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Copyright (c) 2018 Bryce Guinta <bryce.paul.guinta@gmail.com>

"""Astroid hooks for understanding functools library module."""
from functools import partial
from itertools import chain

import astroid
Expand Down Expand Up @@ -143,32 +144,29 @@ def _looks_like_lru_cache(node):
"""Check if the given function node is decorated with lru_cache."""
if not node.decorators:
return False

for decorator in node.decorators.nodes:
if not isinstance(decorator, astroid.Call):
continue

func = helpers.safe_infer(decorator.func)
if not func:
continue

if isinstance(func, astroid.FunctionDef) and func.qname() == LRU_CACHE:
if _looks_like_functools_member(decorator, "lru_cache"):
return True
return False


def _looks_like_functools_partial(node):
def _looks_like_functools_member(node, member):
"""Check if the given Call node is a functools.partial call"""
if isinstance(node.func, astroid.Name):
return node.func.name == "partial"
return node.func.name == member
elif isinstance(node.func, astroid.Attribute):
return (
node.func.attrname == "partial"
node.func.attrname == member
and isinstance(node.func.expr, astroid.Name)
and node.func.expr.name == "functools"
)


_looks_like_partial = partial(_looks_like_functools_member, member="partial")


MANAGER.register_transform(
astroid.FunctionDef, _transform_lru_cache, _looks_like_lru_cache
)
Expand All @@ -177,5 +175,5 @@ def _looks_like_functools_partial(node):
MANAGER.register_transform(
astroid.Call,
astroid.inference_tip(_functools_partial_inference),
_looks_like_functools_partial,
_looks_like_partial,
)

0 comments on commit cab26ec

Please sign in to comment.