summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniƫl van Noord <13665637+DanielNoord@users.noreply.github.com>2022-01-17 18:24:13 +0100
committerPierre Sassoulas <pierre.sassoulas@gmail.com>2022-01-18 00:06:00 +0100
commit58f9a48347c2bf69fe2ba9824613b0adedcaeb80 (patch)
tree52287e38f1c74faae54262de1dfafd7201426aa4
parent12ada21db077fd5115e613ec6352bde68f48446e (diff)
downloadastroid-git-58f9a48347c2bf69fe2ba9824613b0adedcaeb80.tar.gz
Add ``_extract_single_node`` and use in ``brain_typing``
-rw-r--r--astroid/brain/brain_typing.py11
-rw-r--r--astroid/builder.py8
2 files changed, 14 insertions, 5 deletions
diff --git a/astroid/brain/brain_typing.py b/astroid/brain/brain_typing.py
index d5fab0b9..de89f1aa 100644
--- a/astroid/brain/brain_typing.py
+++ b/astroid/brain/brain_typing.py
@@ -17,6 +17,7 @@ import typing
from functools import partial
from astroid import context, extract_node, inference_tip
+from astroid.builder import _extract_single_node
from astroid.const import PY37_PLUS, PY38_PLUS, PY39_PLUS
from astroid.exceptions import (
AttributeInferenceError,
@@ -171,7 +172,7 @@ def infer_typing_attr(
# With PY37+ typing.Generic and typing.Annotated (PY39) are subscriptable
# through __class_getitem__. Since astroid can't easily
# infer the native methods, replace them for an easy inference tip
- func_to_add = extract_node(CLASS_GETITEM_TEMPLATE)
+ func_to_add = _extract_single_node(CLASS_GETITEM_TEMPLATE)
value.locals["__class_getitem__"] = [func_to_add]
if (
isinstance(node.parent, ClassDef)
@@ -199,7 +200,7 @@ def _looks_like_typedDict( # pylint: disable=invalid-name
def infer_old_typedDict( # pylint: disable=invalid-name
node: ClassDef, ctx: typing.Optional[context.InferenceContext] = None
) -> typing.Iterator[ClassDef]:
- func_to_add = extract_node("dict")
+ func_to_add = _extract_single_node("dict")
node.locals["__call__"] = [func_to_add]
return iter([node])
@@ -215,7 +216,7 @@ def infer_typedDict( # pylint: disable=invalid-name
parent=node.parent,
)
class_def.postinit(bases=[extract_node("dict")], body=[], decorators=None)
- func_to_add = extract_node("dict")
+ func_to_add = _extract_single_node("dict")
class_def.locals["__call__"] = [func_to_add]
return iter([class_def])
@@ -308,7 +309,7 @@ def infer_typing_alias(
and maybe_type_var.value > 0
):
# If typing alias is subscriptable, add `__class_getitem__` to ClassDef
- func_to_add = extract_node(CLASS_GETITEM_TEMPLATE)
+ func_to_add = _extract_single_node(CLASS_GETITEM_TEMPLATE)
class_def.locals["__class_getitem__"] = [func_to_add]
else:
# If not, make sure that `__class_getitem__` access is forbidden.
@@ -373,7 +374,7 @@ def infer_special_alias(
parent=node.parent,
)
class_def.postinit(bases=[res], body=[], decorators=None)
- func_to_add = extract_node(CLASS_GETITEM_TEMPLATE)
+ func_to_add = _extract_single_node(CLASS_GETITEM_TEMPLATE)
class_def.locals["__class_getitem__"] = [func_to_add]
return iter([class_def])
diff --git a/astroid/builder.py b/astroid/builder.py
index 4d68e4f2..273c46e3 100644
--- a/astroid/builder.py
+++ b/astroid/builder.py
@@ -455,6 +455,14 @@ def extract_node(code: str, module_name: str = "") -> Union[NodeNG, List[NodeNG]
return extracted
+def _extract_single_node(code: str, module_name: str = "") -> NodeNG:
+ """Call extract_node while making sure that only one value is returned."""
+ ret = extract_node(code, module_name)
+ if isinstance(ret, list):
+ return ret[0]
+ return ret
+
+
def _parse_string(data, type_comments=True):
parser_module = get_parser_module(type_comments=type_comments)
try: