Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/astroid/brain/brain_namedtuple_enum.py: 17%
282 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:53 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:53 +0000
1# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
2# For details: https://github.com/pylint-dev/astroid/blob/main/LICENSE
3# Copyright (c) https://github.com/pylint-dev/astroid/blob/main/CONTRIBUTORS.txt
5"""Astroid hooks for the Python standard library."""
7from __future__ import annotations
9import functools
10import keyword
11from collections.abc import Iterator
12from textwrap import dedent
13from typing import Final
15import astroid
16from astroid import arguments, bases, inference_tip, nodes, util
17from astroid.builder import AstroidBuilder, _extract_single_node, extract_node
18from astroid.context import InferenceContext
19from astroid.exceptions import (
20 AstroidTypeError,
21 AstroidValueError,
22 InferenceError,
23 MroError,
24 UseInferenceDefault,
25)
26from astroid.manager import AstroidManager
28ENUM_BASE_NAMES = {
29 "Enum",
30 "IntEnum",
31 "enum.Enum",
32 "enum.IntEnum",
33 "IntFlag",
34 "enum.IntFlag",
35}
36ENUM_QNAME: Final[str] = "enum.Enum"
37TYPING_NAMEDTUPLE_QUALIFIED: Final = {
38 "typing.NamedTuple",
39 "typing_extensions.NamedTuple",
40}
41TYPING_NAMEDTUPLE_BASENAMES: Final = {
42 "NamedTuple",
43 "typing.NamedTuple",
44 "typing_extensions.NamedTuple",
45}
48def _infer_first(node, context):
49 if isinstance(node, util.UninferableBase):
50 raise UseInferenceDefault
51 try:
52 value = next(node.infer(context=context))
53 except StopIteration as exc:
54 raise InferenceError from exc
55 if isinstance(value, util.UninferableBase):
56 raise UseInferenceDefault()
57 return value
60def _find_func_form_arguments(node, context):
61 def _extract_namedtuple_arg_or_keyword( # pylint: disable=inconsistent-return-statements
62 position, key_name=None
63 ):
64 if len(args) > position:
65 return _infer_first(args[position], context)
66 if key_name and key_name in found_keywords:
67 return _infer_first(found_keywords[key_name], context)
69 args = node.args
70 keywords = node.keywords
71 found_keywords = (
72 {keyword.arg: keyword.value for keyword in keywords} if keywords else {}
73 )
75 name = _extract_namedtuple_arg_or_keyword(position=0, key_name="typename")
76 names = _extract_namedtuple_arg_or_keyword(position=1, key_name="field_names")
77 if name and names:
78 return name.value, names
80 raise UseInferenceDefault()
83def infer_func_form(
84 node: nodes.Call,
85 base_type: list[nodes.NodeNG],
86 context: InferenceContext | None = None,
87 enum: bool = False,
88) -> tuple[nodes.ClassDef, str, list[str]]:
89 """Specific inference function for namedtuple or Python 3 enum."""
90 # node is a Call node, class name as first argument and generated class
91 # attributes as second argument
93 # namedtuple or enums list of attributes can be a list of strings or a
94 # whitespace-separate string
95 try:
96 name, names = _find_func_form_arguments(node, context)
97 try:
98 attributes: list[str] = names.value.replace(",", " ").split()
99 except AttributeError as exc:
100 # Handle attributes of NamedTuples
101 if not enum:
102 attributes = []
103 fields = _get_namedtuple_fields(node)
104 if fields:
105 fields_node = extract_node(fields)
106 attributes = [
107 _infer_first(const, context).value for const in fields_node.elts
108 ]
110 # Handle attributes of Enums
111 else:
112 # Enums supports either iterator of (name, value) pairs
113 # or mappings.
114 if hasattr(names, "items") and isinstance(names.items, list):
115 attributes = [
116 _infer_first(const[0], context).value
117 for const in names.items
118 if isinstance(const[0], nodes.Const)
119 ]
120 elif hasattr(names, "elts"):
121 # Enums can support either ["a", "b", "c"]
122 # or [("a", 1), ("b", 2), ...], but they can't
123 # be mixed.
124 if all(isinstance(const, nodes.Tuple) for const in names.elts):
125 attributes = [
126 _infer_first(const.elts[0], context).value
127 for const in names.elts
128 if isinstance(const, nodes.Tuple)
129 ]
130 else:
131 attributes = [
132 _infer_first(const, context).value for const in names.elts
133 ]
134 else:
135 raise AttributeError from exc
136 if not attributes:
137 raise AttributeError from exc
138 except (AttributeError, InferenceError) as exc:
139 raise UseInferenceDefault from exc
141 if not enum:
142 # namedtuple maps sys.intern(str()) over over field_names
143 attributes = [str(attr) for attr in attributes]
144 # XXX this should succeed *unless* __str__/__repr__ is incorrect or throws
145 # in which case we should not have inferred these values and raised earlier
146 attributes = [attr for attr in attributes if " " not in attr]
148 # If we can't infer the name of the class, don't crash, up to this point
149 # we know it is a namedtuple anyway.
150 name = name or "Uninferable"
151 # we want to return a Class node instance with proper attributes set
152 class_node = nodes.ClassDef(
153 name,
154 lineno=node.lineno,
155 col_offset=node.col_offset,
156 end_lineno=node.end_lineno,
157 end_col_offset=node.end_col_offset,
158 parent=nodes.Unknown(),
159 )
160 # A typical ClassDef automatically adds its name to the parent scope,
161 # but doing so causes problems, so defer setting parent until after init
162 # see: https://github.com/pylint-dev/pylint/issues/5982
163 class_node.parent = node.parent
164 class_node.postinit(
165 # set base class=tuple
166 bases=base_type,
167 body=[],
168 decorators=None,
169 )
170 # XXX add __init__(*attributes) method
171 for attr in attributes:
172 fake_node = nodes.EmptyNode()
173 fake_node.parent = class_node
174 fake_node.attrname = attr
175 class_node.instance_attrs[attr] = [fake_node]
176 return class_node, name, attributes
179def _has_namedtuple_base(node):
180 """Predicate for class inference tip.
182 :type node: ClassDef
183 :rtype: bool
184 """
185 return set(node.basenames) & TYPING_NAMEDTUPLE_BASENAMES
188def _looks_like(node, name) -> bool:
189 func = node.func
190 if isinstance(func, nodes.Attribute):
191 return func.attrname == name
192 if isinstance(func, nodes.Name):
193 return func.name == name
194 return False
197_looks_like_namedtuple = functools.partial(_looks_like, name="namedtuple")
198_looks_like_enum = functools.partial(_looks_like, name="Enum")
199_looks_like_typing_namedtuple = functools.partial(_looks_like, name="NamedTuple")
202def infer_named_tuple(
203 node: nodes.Call, context: InferenceContext | None = None
204) -> Iterator[nodes.ClassDef]:
205 """Specific inference function for namedtuple Call node."""
206 tuple_base_name: list[nodes.NodeNG] = [
207 nodes.Name(
208 name="tuple",
209 parent=node.root(),
210 lineno=0,
211 col_offset=0,
212 end_lineno=None,
213 end_col_offset=None,
214 )
215 ]
216 class_node, name, attributes = infer_func_form(
217 node, tuple_base_name, context=context
218 )
219 call_site = arguments.CallSite.from_call(node, context=context)
220 node = extract_node("import collections; collections.namedtuple")
221 try:
222 func = next(node.infer())
223 except StopIteration as e:
224 raise InferenceError(node=node) from e
225 try:
226 rename = next(
227 call_site.infer_argument(func, "rename", context or InferenceContext())
228 ).bool_value()
229 except (InferenceError, StopIteration):
230 rename = False
232 try:
233 attributes = _check_namedtuple_attributes(name, attributes, rename)
234 except AstroidTypeError as exc:
235 raise UseInferenceDefault("TypeError: " + str(exc)) from exc
236 except AstroidValueError as exc:
237 raise UseInferenceDefault("ValueError: " + str(exc)) from exc
239 replace_args = ", ".join(f"{arg}=None" for arg in attributes)
240 field_def = (
241 " {name} = property(lambda self: self[{index:d}], "
242 "doc='Alias for field number {index:d}')"
243 )
244 field_defs = "\n".join(
245 field_def.format(name=name, index=index)
246 for index, name in enumerate(attributes)
247 )
248 fake = AstroidBuilder(AstroidManager()).string_build(
249 f"""
250class {name}(tuple):
251 __slots__ = ()
252 _fields = {attributes!r}
253 def _asdict(self):
254 return self.__dict__
255 @classmethod
256 def _make(cls, iterable, new=tuple.__new__, len=len):
257 return new(cls, iterable)
258 def _replace(self, {replace_args}):
259 return self
260 def __getnewargs__(self):
261 return tuple(self)
262{field_defs}
263 """
264 )
265 class_node.locals["_asdict"] = fake.body[0].locals["_asdict"]
266 class_node.locals["_make"] = fake.body[0].locals["_make"]
267 class_node.locals["_replace"] = fake.body[0].locals["_replace"]
268 class_node.locals["_fields"] = fake.body[0].locals["_fields"]
269 for attr in attributes:
270 class_node.locals[attr] = fake.body[0].locals[attr]
271 # we use UseInferenceDefault, we can't be a generator so return an iterator
272 return iter([class_node])
275def _get_renamed_namedtuple_attributes(field_names):
276 names = list(field_names)
277 seen = set()
278 for i, name in enumerate(field_names):
279 if (
280 not all(c.isalnum() or c == "_" for c in name)
281 or keyword.iskeyword(name)
282 or not name
283 or name[0].isdigit()
284 or name.startswith("_")
285 or name in seen
286 ):
287 names[i] = "_%d" % i
288 seen.add(name)
289 return tuple(names)
292def _check_namedtuple_attributes(typename, attributes, rename=False):
293 attributes = tuple(attributes)
294 if rename:
295 attributes = _get_renamed_namedtuple_attributes(attributes)
297 # The following snippet is derived from the CPython Lib/collections/__init__.py sources
298 # <snippet>
299 for name in (typename, *attributes):
300 if not isinstance(name, str):
301 raise AstroidTypeError("Type names and field names must be strings")
302 if not name.isidentifier():
303 raise AstroidValueError(
304 "Type names and field names must be valid" + f"identifiers: {name!r}"
305 )
306 if keyword.iskeyword(name):
307 raise AstroidValueError(
308 f"Type names and field names cannot be a keyword: {name!r}"
309 )
311 seen = set()
312 for name in attributes:
313 if name.startswith("_") and not rename:
314 raise AstroidValueError(
315 f"Field names cannot start with an underscore: {name!r}"
316 )
317 if name in seen:
318 raise AstroidValueError(f"Encountered duplicate field name: {name!r}")
319 seen.add(name)
320 # </snippet>
322 return attributes
325def infer_enum(
326 node: nodes.Call, context: InferenceContext | None = None
327) -> Iterator[bases.Instance]:
328 """Specific inference function for enum Call node."""
329 # Raise `UseInferenceDefault` if `node` is a call to a a user-defined Enum.
330 try:
331 inferred = node.func.infer(context)
332 except (InferenceError, StopIteration) as exc:
333 raise UseInferenceDefault from exc
335 if not any(
336 isinstance(item, nodes.ClassDef) and item.qname() == ENUM_QNAME
337 for item in inferred
338 ):
339 raise UseInferenceDefault
341 enum_meta = _extract_single_node(
342 """
343 class EnumMeta(object):
344 'docstring'
345 def __call__(self, node):
346 class EnumAttribute(object):
347 name = ''
348 value = 0
349 return EnumAttribute()
350 def __iter__(self):
351 class EnumAttribute(object):
352 name = ''
353 value = 0
354 return [EnumAttribute()]
355 def __reversed__(self):
356 class EnumAttribute(object):
357 name = ''
358 value = 0
359 return (EnumAttribute, )
360 def __next__(self):
361 return next(iter(self))
362 def __getitem__(self, attr):
363 class Value(object):
364 @property
365 def name(self):
366 return ''
367 @property
368 def value(self):
369 return attr
371 return Value()
372 __members__ = ['']
373 """
374 )
375 class_node = infer_func_form(node, [enum_meta], context=context, enum=True)[0]
376 return iter([class_node.instantiate_class()])
379INT_FLAG_ADDITION_METHODS = """
380 def __or__(self, other):
381 return {name}(self.value | other.value)
382 def __and__(self, other):
383 return {name}(self.value & other.value)
384 def __xor__(self, other):
385 return {name}(self.value ^ other.value)
386 def __add__(self, other):
387 return {name}(self.value + other.value)
388 def __div__(self, other):
389 return {name}(self.value / other.value)
390 def __invert__(self):
391 return {name}(~self.value)
392 def __mul__(self, other):
393 return {name}(self.value * other.value)
394"""
397def infer_enum_class(node: nodes.ClassDef) -> nodes.ClassDef:
398 """Specific inference for enums."""
399 for basename in (b for cls in node.mro() for b in cls.basenames):
400 if node.root().name == "enum":
401 # Skip if the class is directly from enum module.
402 break
403 dunder_members = {}
404 target_names = set()
405 for local, values in node.locals.items():
406 if any(not isinstance(value, nodes.AssignName) for value in values):
407 continue
409 stmt = values[0].statement(future=True)
410 if isinstance(stmt, nodes.Assign):
411 if isinstance(stmt.targets[0], nodes.Tuple):
412 targets = stmt.targets[0].itered()
413 else:
414 targets = stmt.targets
415 elif isinstance(stmt, nodes.AnnAssign):
416 targets = [stmt.target]
417 else:
418 continue
420 inferred_return_value = None
421 if stmt.value is not None:
422 if isinstance(stmt.value, nodes.Const):
423 if isinstance(stmt.value.value, str):
424 inferred_return_value = repr(stmt.value.value)
425 else:
426 inferred_return_value = stmt.value.value
427 else:
428 inferred_return_value = stmt.value.as_string()
430 new_targets = []
431 for target in targets:
432 if isinstance(target, nodes.Starred):
433 continue
434 target_names.add(target.name)
435 # Replace all the assignments with our mocked class.
436 classdef = dedent(
437 """
438 class {name}({types}):
439 @property
440 def value(self):
441 return {return_value}
442 @property
443 def name(self):
444 return "{name}"
445 """.format(
446 name=target.name,
447 types=", ".join(node.basenames),
448 return_value=inferred_return_value,
449 )
450 )
451 if "IntFlag" in basename:
452 # Alright, we need to add some additional methods.
453 # Unfortunately we still can't infer the resulting objects as
454 # Enum members, but once we'll be able to do that, the following
455 # should result in some nice symbolic execution
456 classdef += INT_FLAG_ADDITION_METHODS.format(name=target.name)
458 fake = AstroidBuilder(
459 AstroidManager(), apply_transforms=False
460 ).string_build(classdef)[target.name]
461 fake.parent = target.parent
462 for method in node.mymethods():
463 fake.locals[method.name] = [method]
464 new_targets.append(fake.instantiate_class())
465 dunder_members[local] = fake
466 node.locals[local] = new_targets
468 # The undocumented `_value2member_map_` member:
469 node.locals["_value2member_map_"] = [
470 nodes.Dict(
471 parent=node,
472 lineno=node.lineno,
473 col_offset=node.col_offset,
474 end_lineno=node.end_lineno,
475 end_col_offset=node.end_col_offset,
476 )
477 ]
479 members = nodes.Dict(
480 parent=node,
481 lineno=node.lineno,
482 col_offset=node.col_offset,
483 end_lineno=node.end_lineno,
484 end_col_offset=node.end_col_offset,
485 )
486 members.postinit(
487 [
488 (
489 nodes.Const(k, parent=members),
490 nodes.Name(
491 v.name,
492 parent=members,
493 lineno=v.lineno,
494 col_offset=v.col_offset,
495 end_lineno=v.end_lineno,
496 end_col_offset=v.end_col_offset,
497 ),
498 )
499 for k, v in dunder_members.items()
500 ]
501 )
502 node.locals["__members__"] = [members]
503 # The enum.Enum class itself defines two @DynamicClassAttribute data-descriptors
504 # "name" and "value" (which we override in the mocked class for each enum member
505 # above). When dealing with inference of an arbitrary instance of the enum
506 # class, e.g. in a method defined in the class body like:
507 # class SomeEnum(enum.Enum):
508 # def method(self):
509 # self.name # <- here
510 # In the absence of an enum member called "name" or "value", these attributes
511 # should resolve to the descriptor on that particular instance, i.e. enum member.
512 # For "value", we have no idea what that should be, but for "name", we at least
513 # know that it should be a string, so infer that as a guess.
514 if "name" not in target_names:
515 code = dedent(
516 """
517 @property
518 def name(self):
519 return ''
520 """
521 )
522 name_dynamicclassattr = AstroidBuilder(AstroidManager()).string_build(code)[
523 "name"
524 ]
525 node.locals["name"] = [name_dynamicclassattr]
526 break
527 return node
530def infer_typing_namedtuple_class(class_node, context: InferenceContext | None = None):
531 """Infer a subclass of typing.NamedTuple."""
532 # Check if it has the corresponding bases
533 annassigns_fields = [
534 annassign.target.name
535 for annassign in class_node.body
536 if isinstance(annassign, nodes.AnnAssign)
537 ]
538 code = dedent(
539 """
540 from collections import namedtuple
541 namedtuple({typename!r}, {fields!r})
542 """
543 ).format(typename=class_node.name, fields=",".join(annassigns_fields))
544 node = extract_node(code)
545 try:
546 generated_class_node = next(infer_named_tuple(node, context))
547 except StopIteration as e:
548 raise InferenceError(node=node, context=context) from e
549 for method in class_node.mymethods():
550 generated_class_node.locals[method.name] = [method]
552 for body_node in class_node.body:
553 if isinstance(body_node, nodes.Assign):
554 for target in body_node.targets:
555 attr = target.name
556 generated_class_node.locals[attr] = class_node.locals[attr]
557 elif isinstance(body_node, nodes.ClassDef):
558 generated_class_node.locals[body_node.name] = [body_node]
560 return iter((generated_class_node,))
563def infer_typing_namedtuple_function(node, context: InferenceContext | None = None):
564 """
565 Starting with python3.9, NamedTuple is a function of the typing module.
566 The class NamedTuple is build dynamically through a call to `type` during
567 initialization of the `_NamedTuple` variable.
568 """
569 klass = extract_node(
570 """
571 from typing import _NamedTuple
572 _NamedTuple
573 """
574 )
575 return klass.infer(context)
578def infer_typing_namedtuple(
579 node: nodes.Call, context: InferenceContext | None = None
580) -> Iterator[nodes.ClassDef]:
581 """Infer a typing.NamedTuple(...) call."""
582 # This is essentially a namedtuple with different arguments
583 # so we extract the args and infer a named tuple.
584 try:
585 func = next(node.func.infer())
586 except (InferenceError, StopIteration) as exc:
587 raise UseInferenceDefault from exc
589 if func.qname() not in TYPING_NAMEDTUPLE_QUALIFIED:
590 raise UseInferenceDefault
592 if len(node.args) != 2:
593 raise UseInferenceDefault
595 if not isinstance(node.args[1], (nodes.List, nodes.Tuple)):
596 raise UseInferenceDefault
598 return infer_named_tuple(node, context)
601def _get_namedtuple_fields(node: nodes.Call) -> str:
602 """Get and return fields of a NamedTuple in code-as-a-string.
604 Because the fields are represented in their code form we can
605 extract a node from them later on.
606 """
607 names = []
608 container = None
609 try:
610 container = next(node.args[1].infer())
611 except (InferenceError, StopIteration) as exc:
612 raise UseInferenceDefault from exc
613 # We pass on IndexError as we'll try to infer 'field_names' from the keywords
614 except IndexError:
615 pass
616 if not container:
617 for keyword_node in node.keywords:
618 if keyword_node.arg == "field_names":
619 try:
620 container = next(keyword_node.value.infer())
621 except (InferenceError, StopIteration) as exc:
622 raise UseInferenceDefault from exc
623 break
624 if not isinstance(container, nodes.BaseContainer):
625 raise UseInferenceDefault
626 for elt in container.elts:
627 if isinstance(elt, nodes.Const):
628 names.append(elt.as_string())
629 continue
630 if not isinstance(elt, (nodes.List, nodes.Tuple)):
631 raise UseInferenceDefault
632 if len(elt.elts) != 2:
633 raise UseInferenceDefault
634 names.append(elt.elts[0].as_string())
636 if names:
637 field_names = f"({','.join(names)},)"
638 else:
639 field_names = ""
640 return field_names
643def _is_enum_subclass(cls: astroid.ClassDef) -> bool:
644 """Return whether cls is a subclass of an Enum."""
645 try:
646 return any(
647 klass.name in ENUM_BASE_NAMES
648 and getattr(klass.root(), "name", None) == "enum"
649 for klass in cls.mro()
650 )
651 except MroError:
652 return False
655AstroidManager().register_transform(
656 nodes.Call, inference_tip(infer_named_tuple), _looks_like_namedtuple
657)
658AstroidManager().register_transform(
659 nodes.Call, inference_tip(infer_enum), _looks_like_enum
660)
661AstroidManager().register_transform(
662 nodes.ClassDef, infer_enum_class, predicate=_is_enum_subclass
663)
664AstroidManager().register_transform(
665 nodes.ClassDef, inference_tip(infer_typing_namedtuple_class), _has_namedtuple_base
666)
667AstroidManager().register_transform(
668 nodes.FunctionDef,
669 inference_tip(infer_typing_namedtuple_function),
670 lambda node: node.name == "NamedTuple"
671 and getattr(node.root(), "name", None) == "typing",
672)
673AstroidManager().register_transform(
674 nodes.Call, inference_tip(infer_typing_namedtuple), _looks_like_typing_namedtuple
675)