Skip to content

Commit 8a08a7f

Browse files
SynromPatcherPierre-Sassoulas
authored
[fix] Fix crash when inferring namedtuple with invalid field name (#2806)
Co-authored-by: Patcher <[email protected]> Co-authored-by: Pierre Sassoulas <[email protected]>
1 parent 2400311 commit 8a08a7f

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

ChangeLog

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ What's New in astroid 4.0.0?
77
============================
88
Release date: TBA
99

10+
* Fix crash when inferring namedtuple with invalid field name looking like f-string formatting.
11+
12+
Closes #2519
13+
1014
* Fix false positive no-member in except * handler.
1115

1216
Closes pylint-dev/pylint#9056

astroid/exceptions.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ def __init__(self, message: str = "", **kws: Any) -> None:
6464
setattr(self, key, value)
6565

6666
def __str__(self) -> str:
67-
return self.message.format(**vars(self))
67+
try:
68+
return self.message.format(**vars(self))
69+
except ValueError:
70+
return self.message # Return raw message if formatting fails
6871

6972

7073
class AstroidBuildingError(AstroidError):

tests/test_regrtest.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,3 +550,14 @@ def test_regression_infer_dict_literal_comparison_uninferable() -> None:
550550
node = extract_node("{{}}>0")
551551
inferred = next(node.infer())
552552
assert inferred.value == Uninferable
553+
554+
555+
def test_regression_infer_namedtuple_invalid_fieldname_error() -> None:
556+
"""Regression test for issue #2519."""
557+
code = """
558+
from collections import namedtuple
559+
namedtuple('a','}')
560+
"""
561+
node = extract_node(code)
562+
inferred = next(node.infer())
563+
assert inferred.value == Uninferable

0 commit comments

Comments
 (0)