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
4
5"""Astroid hooks for various builtins."""
6
7from __future__ import annotations
8
9import itertools
10import re
11from collections.abc import Callable, Iterable, Iterator
12from functools import lru_cache, partial
13from typing import NoReturn, cast
14
15from astroid import arguments, helpers, nodes, objects, util
16from astroid.bases import Instance
17from astroid.builder import AstroidBuilder
18from astroid.context import InferenceContext
19from astroid.exceptions import (
20 AstroidTypeError,
21 AttributeInferenceError,
22 InferenceError,
23 MroError,
24 UseInferenceDefault,
25)
26from astroid.inference_tip import inference_tip
27from astroid.manager import AstroidManager
28from astroid.nodes import scoped_nodes
29from astroid.typing import (
30 ConstFactoryResult,
31 InferenceResult,
32 SuccessfulInferenceResult,
33)
34
35ContainerObjects = (
36 objects.FrozenSet | objects.DictItems | objects.DictKeys | objects.DictValues
37)
38
39BuiltContainers = type[tuple] | type[list] | type[set] | type[frozenset]
40
41CopyResult = nodes.Dict | nodes.List | nodes.Set | objects.FrozenSet
42
43OBJECT_DUNDER_NEW = "object.__new__"
44
45# Largest field width/precision we are willing to materialize when inferring a
46# str.format() call. A crafted template such as "{:>2000000000}" would otherwise
47# eagerly build a multi-gigabyte string during inference.
48_MAX_FORMAT_FIELD_SIZE = int(1e8)
49
50# Largest str/bytes constant we are willing to expand into one node per
51# character when inferring container calls and dict.fromkeys.
52_MAX_INFERABLE_STR_LEN = int(1e8)
53
54
55@lru_cache(maxsize=1)
56def _get_bounded_formatter():
57 """Build the formatter lazily: importing ``string`` is costly at import time."""
58 import string # pylint: disable=import-outside-toplevel
59
60 class _BoundedFormatter(string.Formatter):
61 """A ``str.format`` implementation that refuses oversized width/precision.
62
63 The only multi-digit numbers in a format spec are the width and the
64 precision, so bailing when any digit run exceeds the limit covers both.
65 Nested replacement fields ("{:>{}}") are already resolved by ``Formatter``
66 before ``format_field`` runs, so this also catches sizes passed as
67 arguments.
68 """
69
70 def format_field(self, value: object, format_spec: str) -> str:
71 for size in re.findall(r"\d+", format_spec):
72 if int(size) > _MAX_FORMAT_FIELD_SIZE:
73 raise ValueError("format field size exceeds inference limit")
74 return cast(str, super().format_field(value, format_spec))
75
76 return _BoundedFormatter()
77
78
79STR_CLASS = """
80class whatever(object):
81 def join(self, iterable):
82 return {rvalue}
83 def replace(self, old, new, count=None):
84 return {rvalue}
85 def format(self, *args, **kwargs):
86 return {rvalue}
87 def encode(self, encoding='ascii', errors=None):
88 return b''
89 def decode(self, encoding='ascii', errors=None):
90 return u''
91 def capitalize(self):
92 return {rvalue}
93 def title(self):
94 return {rvalue}
95 def lower(self):
96 return {rvalue}
97 def upper(self):
98 return {rvalue}
99 def swapcase(self):
100 return {rvalue}
101 def index(self, sub, start=None, end=None):
102 return 0
103 def find(self, sub, start=None, end=None):
104 return 0
105 def count(self, sub, start=None, end=None):
106 return 0
107 def strip(self, chars=None):
108 return {rvalue}
109 def lstrip(self, chars=None):
110 return {rvalue}
111 def rstrip(self, chars=None):
112 return {rvalue}
113 def rjust(self, width, fillchar=None):
114 return {rvalue}
115 def center(self, width, fillchar=None):
116 return {rvalue}
117 def ljust(self, width, fillchar=None):
118 return {rvalue}
119"""
120
121
122BYTES_CLASS = """
123class whatever(object):
124 def join(self, iterable):
125 return {rvalue}
126 def replace(self, old, new, count=None):
127 return {rvalue}
128 def decode(self, encoding='ascii', errors=None):
129 return u''
130 def capitalize(self):
131 return {rvalue}
132 def title(self):
133 return {rvalue}
134 def lower(self):
135 return {rvalue}
136 def upper(self):
137 return {rvalue}
138 def swapcase(self):
139 return {rvalue}
140 def index(self, sub, start=None, end=None):
141 return 0
142 def find(self, sub, start=None, end=None):
143 return 0
144 def count(self, sub, start=None, end=None):
145 return 0
146 def strip(self, chars=None):
147 return {rvalue}
148 def lstrip(self, chars=None):
149 return {rvalue}
150 def rstrip(self, chars=None):
151 return {rvalue}
152 def rjust(self, width, fillchar=None):
153 return {rvalue}
154 def center(self, width, fillchar=None):
155 return {rvalue}
156 def ljust(self, width, fillchar=None):
157 return {rvalue}
158"""
159
160
161def _use_default() -> NoReturn: # pragma: no cover
162 raise UseInferenceDefault()
163
164
165def _extend_string_class(class_node, code, rvalue):
166 """Function to extend builtin str/unicode class."""
167 code = code.format(rvalue=rvalue)
168 fake = AstroidBuilder(AstroidManager()).string_build(code)["whatever"]
169 for method in fake.mymethods():
170 method.parent = class_node
171 method.lineno = None
172 method.col_offset = None
173 if "__class__" in method.locals:
174 method.locals["__class__"] = [class_node]
175 class_node.locals[method.name] = [method]
176 method.parent = class_node
177
178
179def _extend_builtins(class_transforms):
180 builtin_ast = AstroidManager().builtins_module
181 for class_name, transform in class_transforms.items():
182 transform(builtin_ast[class_name])
183
184
185def on_bootstrap():
186 """Called by astroid_bootstrapping()."""
187 _extend_builtins(
188 {
189 "bytes": partial(_extend_string_class, code=BYTES_CLASS, rvalue="b''"),
190 "str": partial(_extend_string_class, code=STR_CLASS, rvalue="''"),
191 }
192 )
193
194
195def _builtin_filter_predicate(node, builtin_name) -> bool:
196 # pylint: disable = too-many-boolean-expressions
197 if (
198 builtin_name == "type"
199 and node.root().name == "re"
200 and isinstance(node.func, nodes.Name)
201 and node.func.name == "type"
202 and isinstance(node.parent, nodes.Assign)
203 and len(node.parent.targets) == 1
204 and isinstance(node.parent.targets[0], nodes.AssignName)
205 and node.parent.targets[0].name in {"Pattern", "Match"}
206 ):
207 # Handle re.Pattern and re.Match in brain_re
208 # Match these patterns from stdlib/re.py
209 # ```py
210 # Pattern = type(...)
211 # Match = type(...)
212 # ```
213 return False
214 if isinstance(node.func, nodes.Name):
215 return node.func.name == builtin_name
216 if isinstance(node.func, nodes.Attribute):
217 return (
218 node.func.attrname == "fromkeys"
219 and isinstance(node.func.expr, nodes.Name)
220 and node.func.expr.name == "dict"
221 )
222 return False
223
224
225def register_builtin_transform(
226 manager: AstroidManager, transform, builtin_name
227) -> None:
228 """Register a new transform function for the given *builtin_name*.
229
230 The transform function must accept two parameters, a node and
231 an optional context.
232 """
233
234 def _transform_wrapper(
235 node: nodes.Call, context: InferenceContext | None = None
236 ) -> Iterator:
237 result = transform(node, context=context)
238 if result:
239 if not result.parent:
240 # Let the transformation function determine
241 # the parent for its result. Otherwise,
242 # we set it to be the node we transformed from.
243 result.parent = node
244
245 if result.lineno is None:
246 result.lineno = node.lineno
247 # Can be a 'Module' see https://github.com/pylint-dev/pylint/issues/4671
248 # We don't have a regression test on this one: tread carefully
249 if hasattr(result, "col_offset") and result.col_offset is None:
250 result.col_offset = node.col_offset
251 return iter([result])
252
253 manager.register_transform(
254 nodes.Call,
255 inference_tip(_transform_wrapper),
256 partial(_builtin_filter_predicate, builtin_name=builtin_name),
257 )
258
259
260def _container_generic_inference(
261 node: nodes.Call,
262 context: InferenceContext | None,
263 node_type: type[nodes.BaseContainer],
264 transform: Callable[[SuccessfulInferenceResult], nodes.BaseContainer | None],
265) -> nodes.BaseContainer:
266 args = node.args
267 if not args:
268 return node_type(
269 lineno=node.lineno,
270 col_offset=node.col_offset,
271 parent=node.parent,
272 end_lineno=node.end_lineno,
273 end_col_offset=node.end_col_offset,
274 )
275 if len(node.args) > 1:
276 raise UseInferenceDefault()
277
278 (arg,) = args
279 transformed = transform(arg)
280 if not transformed:
281 try:
282 inferred = next(arg.infer(context=context))
283 except (InferenceError, StopIteration) as exc:
284 raise UseInferenceDefault from exc
285 if isinstance(inferred, util.UninferableBase):
286 raise UseInferenceDefault
287 transformed = transform(inferred)
288 if not transformed or isinstance(transformed, util.UninferableBase):
289 raise UseInferenceDefault
290 return transformed
291
292
293def _container_generic_transform(
294 arg: SuccessfulInferenceResult,
295 context: InferenceContext | None,
296 klass: type[nodes.BaseContainer],
297 iterables: tuple[type[nodes.BaseContainer] | type[ContainerObjects], ...],
298 build_elts: BuiltContainers,
299) -> nodes.BaseContainer | None:
300 elts: Iterable | str | bytes
301
302 if isinstance(arg, klass):
303 return arg
304 if isinstance(arg, iterables):
305 arg = cast((nodes.BaseContainer | ContainerObjects), arg)
306 if all(isinstance(elt, nodes.Const) for elt in arg.elts):
307 elts = [cast(nodes.Const, elt).value for elt in arg.elts]
308 else:
309 # TODO: Does not handle deduplication for sets.
310 elts = []
311 for element in arg.elts:
312 if not element:
313 continue
314 inferred = util.safe_infer(element, context=context)
315 if inferred:
316 evaluated_object = nodes.EvaluatedObject(
317 original=element, value=inferred
318 )
319 elts.append(evaluated_object)
320 elif isinstance(arg, nodes.Dict):
321 # Dicts need to have consts as strings already.
322 elts = [
323 item[0].value if isinstance(item[0], nodes.Const) else _use_default()
324 for item in arg.items
325 ]
326 elif isinstance(arg, nodes.Const) and isinstance(arg.value, (str, bytes)):
327 # Don't expand an oversized string into one Const node per character.
328 if len(arg.value) > _MAX_INFERABLE_STR_LEN:
329 return None
330 elts = arg.value
331 else:
332 return None
333 return klass.from_elements(elts=build_elts(elts))
334
335
336def _infer_builtin_container(
337 node: nodes.Call,
338 context: InferenceContext | None,
339 klass: type[nodes.BaseContainer],
340 iterables: tuple[type[nodes.NodeNG] | type[ContainerObjects], ...],
341 build_elts: BuiltContainers,
342) -> nodes.BaseContainer:
343 transform_func = partial(
344 _container_generic_transform,
345 context=context,
346 klass=klass,
347 iterables=iterables,
348 build_elts=build_elts,
349 )
350
351 return _container_generic_inference(node, context, klass, transform_func)
352
353
354# pylint: disable=invalid-name
355infer_tuple = partial(
356 _infer_builtin_container,
357 klass=nodes.Tuple,
358 iterables=(
359 nodes.List,
360 nodes.Set,
361 objects.FrozenSet,
362 objects.DictItems,
363 objects.DictKeys,
364 objects.DictValues,
365 ),
366 build_elts=tuple,
367)
368
369infer_list = partial(
370 _infer_builtin_container,
371 klass=nodes.List,
372 iterables=(
373 nodes.Tuple,
374 nodes.Set,
375 objects.FrozenSet,
376 objects.DictItems,
377 objects.DictKeys,
378 objects.DictValues,
379 ),
380 build_elts=list,
381)
382
383infer_set = partial(
384 _infer_builtin_container,
385 klass=nodes.Set,
386 iterables=(nodes.List, nodes.Tuple, objects.FrozenSet, objects.DictKeys),
387 build_elts=set,
388)
389
390infer_frozenset = partial(
391 _infer_builtin_container,
392 klass=objects.FrozenSet,
393 iterables=(nodes.List, nodes.Tuple, nodes.Set, objects.FrozenSet, objects.DictKeys),
394 build_elts=frozenset,
395)
396
397
398def _get_elts(arg, context):
399 def is_iterable(n) -> bool:
400 return isinstance(n, (nodes.List, nodes.Tuple, nodes.Set))
401
402 try:
403 inferred = next(arg.infer(context))
404 except (InferenceError, StopIteration) as exc:
405 raise UseInferenceDefault from exc
406 if isinstance(inferred, nodes.Dict):
407 items = inferred.items
408 elif is_iterable(inferred):
409 items = []
410 for elt in inferred.elts:
411 # If an item is not a pair of two items,
412 # then fallback to the default inference.
413 # Also, take in consideration only hashable items,
414 # tuples and consts. We are choosing Names as well.
415 if not is_iterable(elt):
416 raise UseInferenceDefault()
417 if len(elt.elts) != 2:
418 raise UseInferenceDefault()
419 if not isinstance(elt.elts[0], (nodes.Tuple, nodes.Const, nodes.Name)):
420 raise UseInferenceDefault()
421 items.append(tuple(elt.elts))
422 else:
423 raise UseInferenceDefault()
424 return items
425
426
427def infer_dict(node: nodes.Call, context: InferenceContext | None = None) -> nodes.Dict:
428 """Try to infer a dict call to a Dict node.
429
430 The function treats the following cases:
431
432 * dict()
433 * dict(mapping)
434 * dict(iterable)
435 * dict(iterable, **kwargs)
436 * dict(mapping, **kwargs)
437 * dict(**kwargs)
438
439 If a case can't be inferred, we'll fallback to default inference.
440 """
441 call = arguments.CallSite.from_call(node, context=context)
442 if call.has_invalid_arguments() or call.has_invalid_keywords():
443 raise UseInferenceDefault
444
445 args = call.positional_arguments
446 kwargs = list(call.keyword_arguments.items())
447
448 items: list[tuple[InferenceResult, InferenceResult]]
449 if not args and not kwargs:
450 # dict()
451 return nodes.Dict(
452 lineno=node.lineno,
453 col_offset=node.col_offset,
454 parent=node.parent,
455 end_lineno=node.end_lineno,
456 end_col_offset=node.end_col_offset,
457 )
458 if kwargs and not args:
459 # dict(a=1, b=2, c=4)
460 items = [(nodes.Const(key), value) for key, value in kwargs]
461 elif len(args) == 1 and kwargs:
462 # dict(some_iterable, b=2, c=4)
463 elts = _get_elts(args[0], context)
464 keys = [(nodes.Const(key), value) for key, value in kwargs]
465 items = elts + keys
466 elif len(args) == 1:
467 items = _get_elts(args[0], context)
468 else:
469 raise UseInferenceDefault()
470 value = nodes.Dict(
471 col_offset=node.col_offset,
472 lineno=node.lineno,
473 parent=node.parent,
474 end_lineno=node.end_lineno,
475 end_col_offset=node.end_col_offset,
476 )
477 value.postinit(items)
478 return value
479
480
481def _mro_owner(cls: nodes.ClassDef, context: InferenceContext | None) -> nodes.ClassDef:
482 """Return the class whose mro a ``super()`` call with no argument walks.
483
484 ``super()`` with no argument is ``super(__class__, self)``. It starts the
485 lookup after the class the method is written in, but it walks the mro of the
486 object the method was called on, which can be a subclass, or a class that
487 only meets the method's own class in the mro of that subclass (a mixin).
488 ``context.boundnode`` is that object when it is known.
489 """
490 bound = context.boundnode if context is not None else None
491 if isinstance(bound, Instance):
492 bound_cls = bound._proxied
493 elif isinstance(bound, nodes.ClassDef):
494 bound_cls = bound
495 else:
496 return cls
497 try:
498 if cls in bound_cls.mro():
499 return bound_cls
500 except MroError:
501 pass
502 return cls
503
504
505def infer_super(
506 node: nodes.Call, context: InferenceContext | None = None
507) -> objects.Super:
508 """Understand super calls.
509
510 There are some restrictions for what can be understood:
511
512 * unbounded super (one argument form) is not understood.
513
514 * if the super call is not inside a function (classmethod or method),
515 then the default inference will be used.
516
517 * if the super arguments can't be inferred, the default inference
518 will be used.
519 """
520 if len(node.args) == 1:
521 # Ignore unbounded super.
522 raise UseInferenceDefault
523
524 scope = node.scope()
525 if not isinstance(scope, nodes.FunctionDef):
526 # Ignore non-method uses of super.
527 raise UseInferenceDefault
528 if scope.type not in ("classmethod", "method"):
529 # Not interested in staticmethods.
530 raise UseInferenceDefault
531
532 cls = scoped_nodes.get_wrapping_class(scope)
533 if not node.args:
534 if cls is None:
535 # A ``classmethod`` or ``method`` that is not defined inside a
536 # class, e.g. a module level function decorated with
537 # ``@classmethod``: there is nothing for the zero argument
538 # form to refer to.
539 raise UseInferenceDefault
540 mro_pointer = cls
541 mro_owner = _mro_owner(cls, context)
542 # In we are in a classmethod, the interpreter will fill
543 # automatically the class as the second argument, not an instance.
544 if scope.type == "classmethod":
545 mro_type = mro_owner
546 else:
547 mro_type = mro_owner.instantiate_class()
548 else:
549 try:
550 mro_pointer = next(node.args[0].infer(context=context))
551 except (InferenceError, StopIteration) as exc:
552 raise UseInferenceDefault from exc
553 try:
554 mro_type = next(node.args[1].infer(context=context))
555 except (InferenceError, StopIteration) as exc:
556 raise UseInferenceDefault from exc
557
558 if isinstance(mro_pointer, util.UninferableBase) or isinstance(
559 mro_type, util.UninferableBase
560 ):
561 # No way we could understand this.
562 raise UseInferenceDefault
563
564 super_obj = objects.Super(
565 mro_pointer=mro_pointer,
566 mro_type=mro_type,
567 self_class=cls,
568 scope=scope,
569 call=node,
570 )
571 super_obj.parent = node
572 return super_obj
573
574
575def _infer_getattr_args(node, context):
576 if len(node.args) not in (2, 3):
577 # Not a valid getattr call.
578 raise UseInferenceDefault
579
580 try:
581 obj = next(node.args[0].infer(context=context))
582 attr = next(node.args[1].infer(context=context))
583 except (InferenceError, StopIteration) as exc:
584 raise UseInferenceDefault from exc
585
586 if isinstance(obj, util.UninferableBase) or isinstance(attr, util.UninferableBase):
587 # If one of the arguments is something we can't infer,
588 # then also make the result of the getattr call something
589 # which is unknown.
590 return util.Uninferable, util.Uninferable
591
592 is_string = isinstance(attr, nodes.Const) and isinstance(attr.value, str)
593 if not is_string:
594 raise UseInferenceDefault
595
596 return obj, attr.value
597
598
599def infer_getattr(node, context: InferenceContext | None = None):
600 """Understand getattr calls.
601
602 If one of the arguments is an Uninferable object, then the
603 result will be an Uninferable object. Otherwise, the normal attribute
604 lookup will be done.
605 """
606 obj, attr = _infer_getattr_args(node, context)
607 if (
608 isinstance(obj, util.UninferableBase)
609 or isinstance(attr, util.UninferableBase)
610 or not hasattr(obj, "igetattr")
611 ):
612 return util.Uninferable
613
614 try:
615 return next(obj.igetattr(attr, context=context))
616 except (StopIteration, InferenceError, AttributeInferenceError):
617 if len(node.args) == 3:
618 # Try to infer the default and return it instead.
619 try:
620 return next(node.args[2].infer(context=context))
621 except (StopIteration, InferenceError) as exc:
622 raise UseInferenceDefault from exc
623
624 raise UseInferenceDefault
625
626
627def infer_hasattr(node, context: InferenceContext | None = None):
628 """Understand hasattr calls.
629
630 This always guarantees three possible outcomes for calling
631 hasattr: Const(False) when we are sure that the object
632 doesn't have the intended attribute, Const(True) when
633 we know that the object has the attribute and Uninferable
634 when we are unsure of the outcome of the function call.
635 """
636 try:
637 obj, attr = _infer_getattr_args(node, context)
638 if (
639 isinstance(obj, util.UninferableBase)
640 or isinstance(attr, util.UninferableBase)
641 or not hasattr(obj, "getattr")
642 ):
643 return util.Uninferable
644 obj.getattr(attr, context=context)
645 except UseInferenceDefault:
646 # Can't infer something from this function call.
647 return util.Uninferable
648 except AttributeInferenceError:
649 # Doesn't have it.
650 return nodes.Const(False)
651 return nodes.Const(True)
652
653
654def infer_callable(node, context: InferenceContext | None = None):
655 """Understand callable calls.
656
657 This follows Python's semantics, where an object
658 is callable if it provides an attribute __call__,
659 even though that attribute is something which can't be
660 called.
661 """
662 if len(node.args) != 1:
663 # Invalid callable call.
664 raise UseInferenceDefault
665
666 argument = node.args[0]
667 try:
668 inferred = next(argument.infer(context=context))
669 except (InferenceError, StopIteration):
670 return util.Uninferable
671 if isinstance(inferred, util.UninferableBase):
672 return util.Uninferable
673 return nodes.Const(inferred.callable())
674
675
676def infer_property(
677 node: nodes.Call, context: InferenceContext | None = None
678) -> objects.Property:
679 """Understand `property` class.
680
681 This only infers the output of `property`
682 call, not the arguments themselves.
683 """
684 if len(node.args) < 1:
685 # Invalid property call.
686 raise UseInferenceDefault
687
688 getter = node.args[0]
689 try:
690 inferred = next(getter.infer(context=context))
691 except (InferenceError, StopIteration) as exc:
692 raise UseInferenceDefault from exc
693
694 if not isinstance(inferred, (nodes.FunctionDef, nodes.Lambda)):
695 raise UseInferenceDefault
696
697 prop_func = objects.Property(
698 function=inferred,
699 name="<property>",
700 lineno=node.lineno,
701 col_offset=node.col_offset,
702 # ↓ semantically, the definition of the class of property isn't within
703 # node.frame. It's somewhere in the builtins module, but we are special
704 # casing it for each "property()" call, so we are making up the
705 # definition on the spot, ad-hoc.
706 parent=scoped_nodes.SYNTHETIC_ROOT,
707 )
708 prop_func.postinit(
709 body=[],
710 args=inferred.args,
711 doc_node=getattr(inferred, "doc_node", None),
712 )
713 return prop_func
714
715
716def infer_bool(node, context: InferenceContext | None = None):
717 """Understand bool calls."""
718 if len(node.args) > 1:
719 # Invalid bool call.
720 raise UseInferenceDefault
721
722 if not node.args:
723 return nodes.Const(False)
724
725 argument = node.args[0]
726 try:
727 inferred = next(argument.infer(context=context))
728 except (InferenceError, StopIteration):
729 return util.Uninferable
730 if isinstance(inferred, util.UninferableBase):
731 return util.Uninferable
732
733 bool_value = inferred.bool_value(context=context)
734 if isinstance(bool_value, util.UninferableBase):
735 return util.Uninferable
736 return nodes.Const(bool_value)
737
738
739def infer_type(node, context: InferenceContext | None = None):
740 """Understand the one-argument form of *type*."""
741 if len(node.args) != 1:
742 raise UseInferenceDefault
743
744 return helpers.object_type(node.args[0], context)
745
746
747def infer_slice(node, context: InferenceContext | None = None):
748 """Understand `slice` calls."""
749 args = node.args
750 if not 0 < len(args) <= 3:
751 raise UseInferenceDefault
752
753 infer_func = partial(util.safe_infer, context=context)
754 args = [infer_func(arg) for arg in args]
755 for arg in args:
756 if not arg or isinstance(arg, util.UninferableBase):
757 raise UseInferenceDefault
758 if not isinstance(arg, nodes.Const):
759 raise UseInferenceDefault
760 if not isinstance(arg.value, (type(None), int)):
761 raise UseInferenceDefault
762
763 if len(args) < 3:
764 # Make sure we have 3 arguments.
765 args.extend([None] * (3 - len(args)))
766
767 slice_node = nodes.Slice(
768 lineno=node.lineno,
769 col_offset=node.col_offset,
770 parent=node.parent,
771 end_lineno=node.end_lineno,
772 end_col_offset=node.end_col_offset,
773 )
774 slice_node.postinit(*args)
775 return slice_node
776
777
778def _infer_object__new__decorator(
779 node: nodes.ClassDef,
780 context: InferenceContext | None = None,
781) -> Iterator[Instance]:
782 # Instantiate class immediately
783 # since that's what @object.__new__ does
784 return iter((node.instantiate_class(),))
785
786
787def _infer_object__new__decorator_check(node) -> bool:
788 """Predicate before inference_tip.
789
790 Check if the given ClassDef has an @object.__new__ decorator
791 """
792 if not node.decorators:
793 return False
794
795 for decorator in node.decorators.nodes:
796 if isinstance(decorator, nodes.Attribute):
797 if decorator.as_string() == OBJECT_DUNDER_NEW:
798 return True
799 return False
800
801
802def infer_issubclass(
803 callnode: nodes.Call, context: InferenceContext | None = None
804) -> nodes.Const:
805 """Infer issubclass() calls.
806
807 :param callnode: an `issubclass` call
808 :param context: the context for the inference
809 :returns: Boolean Const value of the `issubclass` call
810 :raises UseInferenceDefault: If the node cannot be inferred
811 """
812 call = arguments.CallSite.from_call(callnode, context=context)
813 if call.keyword_arguments:
814 # issubclass doesn't support keyword arguments
815 raise UseInferenceDefault("TypeError: issubclass() takes no keyword arguments")
816 if len(call.positional_arguments) != 2:
817 raise UseInferenceDefault(
818 f"Expected two arguments, got {len(call.positional_arguments)}"
819 )
820 # The left hand argument is the obj to be checked
821 obj_node, class_or_tuple_node = call.positional_arguments
822
823 try:
824 obj_type = next(obj_node.infer(context=context))
825 except (InferenceError, StopIteration) as exc:
826 raise UseInferenceDefault from exc
827 if not isinstance(obj_type, nodes.ClassDef):
828 raise UseInferenceDefault(
829 f"TypeError: arg 1 must be class, not {type(obj_type)!r}"
830 )
831
832 # The right hand argument is the class(es) that the given
833 # object is to be checked against.
834 try:
835 class_container = helpers.class_or_tuple_to_container(
836 class_or_tuple_node, context=context
837 )
838 except InferenceError as exc:
839 raise UseInferenceDefault from exc
840 try:
841 issubclass_bool = helpers.object_issubclass(obj_type, class_container, context)
842 except AstroidTypeError as exc:
843 raise UseInferenceDefault("TypeError: " + str(exc)) from exc
844 except MroError as exc:
845 raise UseInferenceDefault from exc
846 return nodes.Const(issubclass_bool)
847
848
849def infer_isinstance(
850 callnode: nodes.Call, context: InferenceContext | None = None
851) -> nodes.Const:
852 """Infer isinstance calls.
853
854 :param callnode: an isinstance call
855 :raises UseInferenceDefault: If the node cannot be inferred
856 """
857 call = arguments.CallSite.from_call(callnode, context=context)
858 if call.keyword_arguments:
859 # isinstance doesn't support keyword arguments
860 raise UseInferenceDefault("TypeError: isinstance() takes no keyword arguments")
861 if len(call.positional_arguments) != 2:
862 raise UseInferenceDefault(
863 f"Expected two arguments, got {len(call.positional_arguments)}"
864 )
865 # The left hand argument is the obj to be checked
866 obj_node, class_or_tuple_node = call.positional_arguments
867 # The right hand argument is the class(es) that the given
868 # obj is to be check is an instance of
869 try:
870 class_container = helpers.class_or_tuple_to_container(
871 class_or_tuple_node, context=context
872 )
873 except InferenceError as exc:
874 raise UseInferenceDefault from exc
875 try:
876 isinstance_bool = helpers.object_isinstance(obj_node, class_container, context)
877 except AstroidTypeError as exc:
878 raise UseInferenceDefault("TypeError: " + str(exc)) from exc
879 except MroError as exc:
880 raise UseInferenceDefault from exc
881 if isinstance(isinstance_bool, util.UninferableBase):
882 raise UseInferenceDefault
883 return nodes.Const(isinstance_bool)
884
885
886def infer_len(node: nodes.Call, context: InferenceContext | None = None) -> nodes.Const:
887 """Infer length calls.
888
889 :param node: len call to infer
890 :param context: node context
891 :returns: a Const node with the inferred length, if possible
892 """
893 call = arguments.CallSite.from_call(node, context=context)
894 if call.keyword_arguments:
895 raise UseInferenceDefault("TypeError: len() must take no keyword arguments")
896 if len(call.positional_arguments) != 1:
897 raise UseInferenceDefault(
898 "TypeError: len() must take exactly one argument "
899 f"({len(call.positional_arguments)}) given"
900 )
901 [argument_node] = call.positional_arguments
902
903 try:
904 return nodes.Const(helpers.object_len(argument_node, context=context))
905 except (AstroidTypeError, InferenceError) as exc:
906 raise UseInferenceDefault(str(exc)) from exc
907
908
909def infer_str(node: nodes.Call, context: InferenceContext | None = None) -> nodes.Const:
910 """Infer str() calls.
911
912 :param node: str() call to infer
913 :param context: node context
914 :returns:
915 a Const containing a stringified value of str() call if possible, else an empty string
916 """
917 call = arguments.CallSite.from_call(node, context=context)
918 if call.keyword_arguments:
919 raise UseInferenceDefault("TypeError: str() must take no keyword arguments")
920
921 fallback = nodes.Const("")
922
923 if not call.positional_arguments:
924 return fallback
925
926 # Accept only if all inferred values resolve to the same string
927 candidates: set[str] = set()
928 try:
929 for inferred in call.positional_arguments[0].infer(context=context):
930 if not isinstance(inferred, nodes.Const):
931 return fallback
932
933 try:
934 candidates.add(str(inferred.value))
935 except ValueError:
936 return fallback
937 except InferenceError:
938 return fallback
939
940 if len(candidates) == 1:
941 return nodes.Const(next(iter(candidates)))
942 return fallback
943
944
945def infer_int(node: nodes.Call, context: InferenceContext | None = None) -> nodes.Const:
946 """Infer int() calls.
947
948 :param node: int() call to infer
949 :param context: node context
950 :returns: a Const containing the integer value of the int() call
951 """
952 call = arguments.CallSite.from_call(node, context=context)
953 if call.keyword_arguments:
954 raise UseInferenceDefault("TypeError: int() must take no keyword arguments")
955
956 if call.positional_arguments:
957 try:
958 first_value = next(call.positional_arguments[0].infer(context=context))
959 except (InferenceError, StopIteration) as exc:
960 raise UseInferenceDefault(str(exc)) from exc
961
962 if isinstance(first_value, util.UninferableBase):
963 raise UseInferenceDefault
964
965 if isinstance(first_value, nodes.Const) and isinstance(
966 first_value.value, (int, str)
967 ):
968 try:
969 actual_value = int(first_value.value)
970 except ValueError:
971 return nodes.Const(0)
972 return nodes.Const(actual_value)
973
974 return nodes.Const(0)
975
976
977def infer_dict_fromkeys(
978 node: nodes.Call, context: InferenceContext | None = None
979) -> nodes.Dict:
980 """Infer dict.fromkeys.
981
982 :param node: dict.fromkeys() call to infer
983 :param context: node context
984 :returns:
985 a Dictionary containing the values that astroid was able to infer.
986 In case the inference failed for any reason, an empty dictionary
987 will be inferred instead.
988 """
989
990 def _build_dict_with_elements(elements: list) -> nodes.Dict:
991 new_node = nodes.Dict(
992 col_offset=node.col_offset,
993 lineno=node.lineno,
994 parent=node.parent,
995 end_lineno=node.end_lineno,
996 end_col_offset=node.end_col_offset,
997 )
998 new_node.postinit(elements)
999 return new_node
1000
1001 def _unique_const_keys(keys: Iterable[nodes.Const]) -> list[nodes.Const]:
1002 # dict.fromkeys deduplicates its keys, so keep only the first Const seen
1003 # for a given value. Emitting one entry per element is wrong
1004 # (dict.fromkeys("aab") has keys "a", "b") and lets a repeated string
1005 # balloon the inferred dict.
1006 seen: dict[object, nodes.Const] = {}
1007 for key in keys:
1008 seen.setdefault(key.value, key)
1009 return list(seen.values())
1010
1011 call = arguments.CallSite.from_call(node, context=context)
1012 if call.keyword_arguments:
1013 raise UseInferenceDefault("TypeError: int() must take no keyword arguments")
1014 if len(call.positional_arguments) not in {1, 2}:
1015 raise UseInferenceDefault(
1016 "TypeError: Needs between 1 and 2 positional arguments"
1017 )
1018
1019 default = nodes.Const(None)
1020 values = call.positional_arguments[0]
1021 try:
1022 inferred_values = next(values.infer(context=context))
1023 except (InferenceError, StopIteration):
1024 return _build_dict_with_elements([])
1025 if inferred_values is util.Uninferable:
1026 return _build_dict_with_elements([])
1027
1028 # Limit to a couple of potential values, as this can become pretty complicated
1029 accepted_iterable_elements = (nodes.Const,)
1030 if isinstance(inferred_values, (nodes.List, nodes.Set, nodes.Tuple)):
1031 elements = inferred_values.elts
1032 for element in elements:
1033 if not isinstance(element, accepted_iterable_elements):
1034 # Fallback to an empty dict
1035 return _build_dict_with_elements([])
1036
1037 elements_with_value = [
1038 (element, default) for element in _unique_const_keys(elements)
1039 ]
1040 return _build_dict_with_elements(elements_with_value)
1041 if isinstance(inferred_values, nodes.Const) and isinstance(
1042 inferred_values.value, (str, bytes)
1043 ):
1044 # Same cap as the container builders above.
1045 if len(inferred_values.value) > _MAX_INFERABLE_STR_LEN:
1046 return _build_dict_with_elements([])
1047 # Deduplicate the characters/bytes before building Const nodes so that a
1048 # compact but large string, e.g. dict.fromkeys("x" * 10**8), doesn't
1049 # materialize one node per character for what is a single-key dict.
1050 elements_with_value = [
1051 (nodes.Const(element), default)
1052 for element in dict.fromkeys(inferred_values.value)
1053 ]
1054 return _build_dict_with_elements(elements_with_value)
1055 if isinstance(inferred_values, nodes.Dict):
1056 keys = inferred_values.itered()
1057 for key in keys:
1058 if not isinstance(key, accepted_iterable_elements):
1059 # Fallback to an empty dict
1060 return _build_dict_with_elements([])
1061
1062 elements_with_value = [
1063 (element, default) for element in _unique_const_keys(keys)
1064 ]
1065 return _build_dict_with_elements(elements_with_value)
1066
1067 # Fallback to an empty dictionary
1068 return _build_dict_with_elements([])
1069
1070
1071def _infer_copy_method(
1072 node: nodes.Call, context: InferenceContext | None = None
1073) -> Iterator[CopyResult]:
1074 assert isinstance(node.func, nodes.Attribute)
1075 inferred_orig, inferred_copy = itertools.tee(node.func.expr.infer(context=context))
1076 if all(
1077 isinstance(
1078 inferred_node, (nodes.Dict, nodes.List, nodes.Set, objects.FrozenSet)
1079 )
1080 for inferred_node in inferred_orig
1081 ):
1082 return cast(Iterator[CopyResult], inferred_copy)
1083
1084 raise UseInferenceDefault
1085
1086
1087def _is_str_format_call(node: nodes.Call) -> bool:
1088 """Catch calls to str.format()."""
1089 if not (isinstance(node.func, nodes.Attribute) and node.func.attrname == "format"):
1090 return False
1091
1092 if isinstance(node.func.expr, nodes.Name):
1093 value = util.safe_infer(node.func.expr)
1094 else:
1095 value = node.func.expr
1096
1097 return isinstance(value, nodes.Const) and isinstance(value.value, str)
1098
1099
1100def _infer_str_format_call(
1101 node: nodes.Call, context: InferenceContext | None = None
1102) -> Iterator[ConstFactoryResult | util.UninferableBase]:
1103 """Return a Const node based on the template and passed arguments."""
1104 call = arguments.CallSite.from_call(node, context=context)
1105 assert isinstance(node.func, (nodes.Attribute, nodes.AssignAttr, nodes.DelAttr))
1106
1107 value: nodes.Const
1108 if isinstance(node.func.expr, nodes.Name):
1109 if not (
1110 (inferred := util.safe_infer(node.func.expr))
1111 and isinstance(inferred, nodes.Const)
1112 ):
1113 return iter([util.Uninferable])
1114 value = inferred
1115 elif isinstance(node.func.expr, nodes.Const):
1116 value = node.func.expr
1117 else: # pragma: no cover
1118 return iter([util.Uninferable])
1119
1120 format_template = value.value
1121
1122 # Get the positional arguments passed
1123 inferred_positional: list[nodes.Const] = []
1124 for i in call.positional_arguments:
1125 one_inferred = util.safe_infer(i, context)
1126 if not isinstance(one_inferred, nodes.Const):
1127 return iter([util.Uninferable])
1128 inferred_positional.append(one_inferred)
1129
1130 pos_values: list[str] = [i.value for i in inferred_positional]
1131
1132 # Get the keyword arguments passed
1133 inferred_keyword: dict[str, nodes.Const] = {}
1134 for k, v in call.keyword_arguments.items():
1135 one_inferred = util.safe_infer(v, context)
1136 if not isinstance(one_inferred, nodes.Const):
1137 return iter([util.Uninferable])
1138 inferred_keyword[k] = one_inferred
1139
1140 keyword_values: dict[str, str] = {k: v.value for k, v in inferred_keyword.items()}
1141
1142 formatter = _get_bounded_formatter()
1143 try:
1144 fields = list(formatter.parse(format_template))
1145 except ValueError:
1146 return iter([util.Uninferable])
1147 if any(spec and util.format_spec_too_large(spec) for _, _, spec, _ in fields):
1148 return iter([util.Uninferable])
1149
1150 try:
1151 formatted_string = formatter.format(
1152 format_template, *pos_values, **keyword_values
1153 )
1154 except (AttributeError, IndexError, KeyError, TypeError, ValueError, OverflowError):
1155 # AttributeError: named field in format string was not found in the arguments
1156 # IndexError: there are too few arguments to interpolate
1157 # TypeError: Unsupported format string
1158 # ValueError: Unknown format code
1159 # OverflowError: "c" with an out-of-range code point, e.g. "{:c}".format(-1)
1160 return iter([util.Uninferable])
1161
1162 return iter([nodes.const_factory(formatted_string)])
1163
1164
1165def register(manager: AstroidManager) -> None:
1166 # Builtins inference
1167 register_builtin_transform(manager, infer_bool, "bool")
1168 register_builtin_transform(manager, infer_super, "super")
1169 register_builtin_transform(manager, infer_callable, "callable")
1170 register_builtin_transform(manager, infer_property, "property")
1171 register_builtin_transform(manager, infer_getattr, "getattr")
1172 register_builtin_transform(manager, infer_hasattr, "hasattr")
1173 register_builtin_transform(manager, infer_tuple, "tuple")
1174 register_builtin_transform(manager, infer_set, "set")
1175 register_builtin_transform(manager, infer_list, "list")
1176 register_builtin_transform(manager, infer_dict, "dict")
1177 register_builtin_transform(manager, infer_frozenset, "frozenset")
1178 register_builtin_transform(manager, infer_type, "type")
1179 register_builtin_transform(manager, infer_slice, "slice")
1180 register_builtin_transform(manager, infer_isinstance, "isinstance")
1181 register_builtin_transform(manager, infer_issubclass, "issubclass")
1182 register_builtin_transform(manager, infer_len, "len")
1183 register_builtin_transform(manager, infer_str, "str")
1184 register_builtin_transform(manager, infer_int, "int")
1185 register_builtin_transform(manager, infer_dict_fromkeys, "dict.fromkeys")
1186
1187 # Infer object.__new__ calls
1188 manager.register_transform(
1189 nodes.ClassDef,
1190 inference_tip(_infer_object__new__decorator),
1191 _infer_object__new__decorator_check,
1192 )
1193
1194 manager.register_transform(
1195 nodes.Call,
1196 inference_tip(_infer_copy_method),
1197 lambda node: isinstance(node.func, nodes.Attribute)
1198 and node.func.attrname == "copy",
1199 )
1200
1201 manager.register_transform(
1202 nodes.Call,
1203 inference_tip(_infer_str_format_call),
1204 _is_str_format_call,
1205 )