Skip to content

Commit 520ba33

Browse files
Fix '_infer_str_format_call' crash when the string it analyses are uninferable
Closes pylint-dev/pylint#8109
1 parent 56a65da commit 520ba33

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

ChangeLog

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ What's New in astroid 2.14.2?
2323
=============================
2424
Release date: TBA
2525

26+
* '_infer_str_format_call' won't crash anymore when the string it analyses are uninferable.
27+
28+
Closes PyCQA/pylint#8109
2629

2730

2831
What's New in astroid 2.14.1?

astroid/brain/brain_builtin_inference.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -930,7 +930,9 @@ def _infer_str_format_call(
930930
"""Return a Const node based on the template and passed arguments."""
931931
call = arguments.CallSite.from_call(node, context=context)
932932
if isinstance(node.func.expr, nodes.Name):
933-
value: nodes.Const = helpers.safe_infer(node.func.expr)
933+
value: nodes.Const | None = helpers.safe_infer(node.func.expr)
934+
if value is None:
935+
return iter([util.Uninferable])
934936
else:
935937
value = node.func.expr
936938

tests/brain/test_dataclasses.py

+20
Original file line numberDiff line numberDiff line change
@@ -1350,3 +1350,23 @@ def attr(self, value: int) -> None:
13501350
fourth_init: bases.UnboundMethod = next(fourth.infer())
13511351
assert [a.name for a in fourth_init.args.args] == ["self", "other_attr", "attr"]
13521352
assert [a.name for a in fourth_init.args.defaults] == ["Uninferable"]
1353+
1354+
1355+
def test_dataclass_pylint_8109() -> None:
1356+
"""https://github.com/PyCQA/pylint/issues/8109"""
1357+
function_def = astroid.extract_node(
1358+
"""
1359+
from dataclasses import dataclass
1360+
1361+
@dataclass
1362+
class Number:
1363+
amount: int | float
1364+
round: int = 2
1365+
1366+
def __str__(self): #@
1367+
number_format = "{:,.%sf}" % self.round
1368+
return number_format.format(self.amount).rstrip("0").rstrip(".")
1369+
"""
1370+
)
1371+
inferit = function_def.infer_call_result(function_def, context=None)
1372+
assert [a.name for a in inferit] == [Uninferable]

0 commit comments

Comments
 (0)