File tree 3 files changed +26
-2
lines changed
3 files changed +26
-2
lines changed Original file line number Diff line number Diff line change @@ -12,6 +12,9 @@ What's New in astroid 2.14.2?
12
12
=============================
13
13
Release date: TBA
14
14
15
+ * '_infer_str_format_call' won't crash anymore when the string it analyses are uninferable.
16
+
17
+ Closes PyCQA/pylint#8109
15
18
16
19
17
20
What's New in astroid 2.14.1?
Original file line number Diff line number Diff line change @@ -928,7 +928,9 @@ def _infer_str_format_call(
928
928
"""Return a Const node based on the template and passed arguments."""
929
929
call = arguments .CallSite .from_call (node , context = context )
930
930
if isinstance (node .func .expr , nodes .Name ):
931
- value : nodes .Const = helpers .safe_infer (node .func .expr )
931
+ value : nodes .Const | None = helpers .safe_infer (node .func .expr )
932
+ if value is None :
933
+ return iter ([util .Uninferable ])
932
934
else :
933
935
value = node .func .expr
934
936
Original file line number Diff line number Diff line change 9
9
import pytest
10
10
11
11
from astroid import nodes , objects , util
12
- from astroid .builder import _extract_single_node
12
+ from astroid .builder import _extract_single_node , extract_node
13
13
14
14
15
15
class BuiltinsTest (unittest .TestCase ):
@@ -127,3 +127,22 @@ def test_string_format_with_specs(self) -> None:
127
127
inferred = next (node .infer ())
128
128
assert isinstance (inferred , nodes .Const )
129
129
assert inferred .value == "My name is Daniel, I'm 12.00"
130
+
131
+ def test_string_format_in_dataclass_pylint8109 (self ) -> None :
132
+ """https://github.com/PyCQA/pylint/issues/8109"""
133
+ function_def = extract_node (
134
+ """
135
+ from dataclasses import dataclass
136
+
137
+ @dataclass
138
+ class Number:
139
+ amount: int | float
140
+ round: int = 2
141
+
142
+ def __str__(self): #@
143
+ number_format = "{:,.%sf}" % self.round
144
+ return number_format.format(self.amount).rstrip("0").rstrip(".")
145
+ """
146
+ )
147
+ inferit = function_def .infer_call_result (function_def , context = None )
148
+ assert [a .name for a in inferit ] == [util .Uninferable ]
You can’t perform that action at this time.
0 commit comments