summaryrefslogtreecommitdiff
path: root/astroid/util.py
diff options
context:
space:
mode:
Diffstat (limited to 'astroid/util.py')
-rw-r--r--astroid/util.py24
1 files changed, 24 insertions, 0 deletions
diff --git a/astroid/util.py b/astroid/util.py
index a5310970..8497b34a 100644
--- a/astroid/util.py
+++ b/astroid/util.py
@@ -5,6 +5,7 @@
# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER
import warnings
+from itertools import islice
import importlib
import lazy_object_proxy
@@ -126,5 +127,28 @@ def proxy_alias(alias_name, node_type):
return proxy(lambda: node_type)
+def limit_inference(iterator, size):
+ """Limit inference amount.
+
+ Limit inference amount to help with performance issues with
+ exponentially exploding possible results.
+
+ :param iterator: Inference generator to limit
+ :type iterator: Iterator(NodeNG)
+
+ :param size: Maximum mount of nodes yielded plus an
+ Uninferable at the end if limit reached
+ :type size: int
+
+ :yields: A possibly modified generator
+ :rtype param: Iterable
+ """
+ yield from islice(iterator, size)
+ has_more = next(iterator, False)
+ if has_more is not False:
+ yield Uninferable
+ return
+
+
# Backwards-compatibility aliases
YES = Uninferable