summaryrefslogtreecommitdiff
path: root/astroid
diff options
context:
space:
mode:
authorPierre Sassoulas <pierre.sassoulas@gmail.com>2021-06-15 17:54:29 +0200
committerPierre Sassoulas <pierre.sassoulas@gmail.com>2021-06-15 18:34:45 +0200
commit778d1813913ed56945e1fc0250cbcd42a02fd885 (patch)
treea099efa03112ad4a0ec9cc857dc7ad305899eb29 /astroid
parent9649c98cdcea2ff2c14942ceb7fd1e9fd0307c45 (diff)
downloadastroid-git-778d1813913ed56945e1fc0250cbcd42a02fd885.tar.gz
Create a file for inference tip
This permit to not have to import wrapt in __init__.py It's a dependencie and we want to be able to use attr: astroid.__version__ for packaging without installing depdencies first.
Diffstat (limited to 'astroid')
-rw-r--r--astroid/__init__.py67
-rw-r--r--astroid/inference_tip.py71
2 files changed, 73 insertions, 65 deletions
diff --git a/astroid/__init__.py b/astroid/__init__.py
index f48cdcc4..087282d0 100644
--- a/astroid/__init__.py
+++ b/astroid/__init__.py
@@ -40,12 +40,11 @@ Main modules are:
"""
import enum
-import itertools
+
import os
from importlib import import_module
from pathlib import Path
-import wrapt
from .__pkginfo__ import __version__, version
@@ -70,6 +69,7 @@ from astroid import inference
# more stuff available
from astroid import raw_building
+from astroid.inference_tip import _inference_tip_cached, inference_tip
from astroid.bases import BaseInstance, Instance, BoundMethod, UnboundMethod
from astroid.node_classes import are_exclusive, unpack_infer
from astroid.scoped_nodes import builtin_lookup
@@ -82,69 +82,6 @@ from astroid.manager import AstroidManager
MANAGER = AstroidManager()
del AstroidManager
-# transform utilities (filters and decorator)
-
-
-# pylint: disable=dangerous-default-value
-@wrapt.decorator
-def _inference_tip_cached(func, instance, args, kwargs, _cache={}):
- """Cache decorator used for inference tips"""
- node = args[0]
- try:
- return iter(_cache[func, node])
- except KeyError:
- result = func(*args, **kwargs)
- # Need to keep an iterator around
- original, copy = itertools.tee(result)
- _cache[func, node] = list(copy)
- return original
-
-
-# pylint: enable=dangerous-default-value
-
-
-def inference_tip(infer_function, raise_on_overwrite=False):
- """Given an instance specific inference function, return a function to be
- given to MANAGER.register_transform to set this inference function.
-
- :param bool raise_on_overwrite: Raise an `InferenceOverwriteError`
- if the inference tip will overwrite another. Used for debugging
-
- Typical usage
-
- .. sourcecode:: python
-
- MANAGER.register_transform(Call, inference_tip(infer_named_tuple),
- predicate)
-
- .. Note::
-
- Using an inference tip will override
- any previously set inference tip for the given
- node. Use a predicate in the transform to prevent
- excess overwrites.
- """
-
- def transform(node, infer_function=infer_function):
- if (
- raise_on_overwrite
- and node._explicit_inference is not None
- and node._explicit_inference is not infer_function
- ):
- raise InferenceOverwriteError(
- "Inference already set to {existing_inference}. "
- "Trying to overwrite with {new_inference} for {node}".format(
- existing_inference=infer_function,
- new_inference=node._explicit_inference,
- node=node,
- )
- )
- # pylint: disable=no-value-for-parameter
- node._explicit_inference = _inference_tip_cached(infer_function)
- return node
-
- return transform
-
def register_module_extender(manager, module_name, get_extension_mod):
def transform(node):
diff --git a/astroid/inference_tip.py b/astroid/inference_tip.py
new file mode 100644
index 00000000..dc43aa8e
--- /dev/null
+++ b/astroid/inference_tip.py
@@ -0,0 +1,71 @@
+# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
+# For details: https://github.com/PyCQA/astroid/blob/master/LICENSE
+
+"""Transform utilities (filters and decorator)"""
+
+import itertools
+
+import wrapt
+
+# pylint: disable=dangerous-default-value
+from astroid.exceptions import InferenceOverwriteError
+
+
+@wrapt.decorator
+def _inference_tip_cached(func, instance, args, kwargs, _cache={}): # noqa:B006
+ """Cache decorator used for inference tips"""
+ node = args[0]
+ try:
+ return iter(_cache[func, node])
+ except KeyError:
+ result = func(*args, **kwargs)
+ # Need to keep an iterator around
+ original, copy = itertools.tee(result)
+ _cache[func, node] = list(copy)
+ return original
+
+
+# pylint: enable=dangerous-default-value
+
+
+def inference_tip(infer_function, raise_on_overwrite=False):
+ """Given an instance specific inference function, return a function to be
+ given to MANAGER.register_transform to set this inference function.
+
+ :param bool raise_on_overwrite: Raise an `InferenceOverwriteError`
+ if the inference tip will overwrite another. Used for debugging
+
+ Typical usage
+
+ .. sourcecode:: python
+
+ MANAGER.register_transform(Call, inference_tip(infer_named_tuple),
+ predicate)
+
+ .. Note::
+
+ Using an inference tip will override
+ any previously set inference tip for the given
+ node. Use a predicate in the transform to prevent
+ excess overwrites.
+ """
+
+ def transform(node, infer_function=infer_function):
+ if (
+ raise_on_overwrite
+ and node._explicit_inference is not None
+ and node._explicit_inference is not infer_function
+ ):
+ raise InferenceOverwriteError(
+ "Inference already set to {existing_inference}. "
+ "Trying to overwrite with {new_inference} for {node}".format(
+ existing_inference=infer_function,
+ new_inference=node._explicit_inference,
+ node=node,
+ )
+ )
+ # pylint: disable=no-value-for-parameter
+ node._explicit_inference = _inference_tip_cached(infer_function)
+ return node
+
+ return transform