Skip to content

Commit 58f9a48

Browse files
DanielNoordPierre-Sassoulas
authored andcommitted
Add _extract_single_node and use in brain_typing
1 parent 12ada21 commit 58f9a48

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

astroid/brain/brain_typing.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from functools import partial
1818

1919
from astroid import context, extract_node, inference_tip
20+
from astroid.builder import _extract_single_node
2021
from astroid.const import PY37_PLUS, PY38_PLUS, PY39_PLUS
2122
from astroid.exceptions import (
2223
AttributeInferenceError,
@@ -171,7 +172,7 @@ def infer_typing_attr(
171172
# With PY37+ typing.Generic and typing.Annotated (PY39) are subscriptable
172173
# through __class_getitem__. Since astroid can't easily
173174
# infer the native methods, replace them for an easy inference tip
174-
func_to_add = extract_node(CLASS_GETITEM_TEMPLATE)
175+
func_to_add = _extract_single_node(CLASS_GETITEM_TEMPLATE)
175176
value.locals["__class_getitem__"] = [func_to_add]
176177
if (
177178
isinstance(node.parent, ClassDef)
@@ -199,7 +200,7 @@ def _looks_like_typedDict( # pylint: disable=invalid-name
199200
def infer_old_typedDict( # pylint: disable=invalid-name
200201
node: ClassDef, ctx: typing.Optional[context.InferenceContext] = None
201202
) -> typing.Iterator[ClassDef]:
202-
func_to_add = extract_node("dict")
203+
func_to_add = _extract_single_node("dict")
203204
node.locals["__call__"] = [func_to_add]
204205
return iter([node])
205206

@@ -215,7 +216,7 @@ def infer_typedDict( # pylint: disable=invalid-name
215216
parent=node.parent,
216217
)
217218
class_def.postinit(bases=[extract_node("dict")], body=[], decorators=None)
218-
func_to_add = extract_node("dict")
219+
func_to_add = _extract_single_node("dict")
219220
class_def.locals["__call__"] = [func_to_add]
220221
return iter([class_def])
221222

@@ -308,7 +309,7 @@ def infer_typing_alias(
308309
and maybe_type_var.value > 0
309310
):
310311
# If typing alias is subscriptable, add `__class_getitem__` to ClassDef
311-
func_to_add = extract_node(CLASS_GETITEM_TEMPLATE)
312+
func_to_add = _extract_single_node(CLASS_GETITEM_TEMPLATE)
312313
class_def.locals["__class_getitem__"] = [func_to_add]
313314
else:
314315
# If not, make sure that `__class_getitem__` access is forbidden.
@@ -373,7 +374,7 @@ def infer_special_alias(
373374
parent=node.parent,
374375
)
375376
class_def.postinit(bases=[res], body=[], decorators=None)
376-
func_to_add = extract_node(CLASS_GETITEM_TEMPLATE)
377+
func_to_add = _extract_single_node(CLASS_GETITEM_TEMPLATE)
377378
class_def.locals["__class_getitem__"] = [func_to_add]
378379
return iter([class_def])
379380

astroid/builder.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,14 @@ def _extract(node):
455455
return extracted
456456

457457

458+
def _extract_single_node(code: str, module_name: str = "") -> NodeNG:
459+
"""Call extract_node while making sure that only one value is returned."""
460+
ret = extract_node(code, module_name)
461+
if isinstance(ret, list):
462+
return ret[0]
463+
return ret
464+
465+
458466
def _parse_string(data, type_comments=True):
459467
parser_module = get_parser_module(type_comments=type_comments)
460468
try:

0 commit comments

Comments
 (0)