diff options
| author | github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> | 2023-02-12 16:58:42 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-02-12 16:58:42 +0000 |
| commit | 4af956c9115fe8912fa4ee8799c6e5aedd2f6c96 (patch) | |
| tree | 0b1c14f52a85385576bb9b5d6a4a34dc4fa2c82c | |
| parent | c47d3b95a1dd173fb44fdba2d8dcc46eb88559a9 (diff) | |
| download | astroid-git-4af956c9115fe8912fa4ee8799c6e5aedd2f6c96.tar.gz | |
Fix '_infer_str_format_call' crash when the string it analyses are uninferable (#2024) (#2025)
Closes PyCQA/pylint#8109
(cherry picked from commit ccbdd3c128af9a6e8f9018fb8db5991a28c72db0)
Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
| -rw-r--r-- | ChangeLog | 3 | ||||
| -rw-r--r-- | astroid/brain/brain_builtin_inference.py | 4 | ||||
| -rw-r--r-- | tests/unittest_brain_builtin.py | 21 |
3 files changed, 26 insertions, 2 deletions
@@ -12,6 +12,9 @@ What's New in astroid 2.14.2? ============================= Release date: TBA +* '_infer_str_format_call' won't crash anymore when the string it analyses are uninferable. + + Closes PyCQA/pylint#8109 What's New in astroid 2.14.1? diff --git a/astroid/brain/brain_builtin_inference.py b/astroid/brain/brain_builtin_inference.py index b51d63a5..02e0ecc0 100644 --- a/astroid/brain/brain_builtin_inference.py +++ b/astroid/brain/brain_builtin_inference.py @@ -928,7 +928,9 @@ def _infer_str_format_call( """Return a Const node based on the template and passed arguments.""" call = arguments.CallSite.from_call(node, context=context) if isinstance(node.func.expr, nodes.Name): - value: nodes.Const = helpers.safe_infer(node.func.expr) + value: nodes.Const | None = helpers.safe_infer(node.func.expr) + if value is None: + return iter([util.Uninferable]) else: value = node.func.expr diff --git a/tests/unittest_brain_builtin.py b/tests/unittest_brain_builtin.py index d14c72f8..a1439b06 100644 --- a/tests/unittest_brain_builtin.py +++ b/tests/unittest_brain_builtin.py @@ -9,7 +9,7 @@ import unittest import pytest from astroid import nodes, objects, util -from astroid.builder import _extract_single_node +from astroid.builder import _extract_single_node, extract_node class BuiltinsTest(unittest.TestCase): @@ -127,3 +127,22 @@ class TestStringNodes: inferred = next(node.infer()) assert isinstance(inferred, nodes.Const) assert inferred.value == "My name is Daniel, I'm 12.00" + + def test_string_format_in_dataclass_pylint8109(self) -> None: + """https://github.com/PyCQA/pylint/issues/8109""" + function_def = extract_node( + """ +from dataclasses import dataclass + +@dataclass +class Number: + amount: int | float + round: int = 2 + + def __str__(self): #@ + number_format = "{:,.%sf}" % self.round + return number_format.format(self.amount).rstrip("0").rstrip(".") +""" + ) + inferit = function_def.infer_call_result(function_def, context=None) + assert [a.name for a in inferit] == [util.Uninferable] |
