diff --git a/ChangeLog b/ChangeLog index 9ee995159..bfe12c794 100644 --- a/ChangeLog +++ b/ChangeLog @@ -24,6 +24,11 @@ Release date: TBA Refs PyCQA/pylint#5099 +* Prevent a crash when inferring calls to ``str.format`` with inferred arguments + that would be invalid. + + Closes #1856 + * Infer the `length` argument of the ``random.sample`` function. Refs PyCQA/pylint#7706 diff --git a/astroid/brain/brain_builtin_inference.py b/astroid/brain/brain_builtin_inference.py index af1ddf4d2..e84f5bc02 100644 --- a/astroid/brain/brain_builtin_inference.py +++ b/astroid/brain/brain_builtin_inference.py @@ -954,8 +954,10 @@ def _infer_str_format_call( try: formatted_string = format_template.format(*pos_values, **keyword_values) - except (IndexError, KeyError): - # If there is an IndexError there are too few arguments to interpolate + except (IndexError, KeyError, TypeError, ValueError): + # IndexError: there are too few arguments to interpolate + # TypeError: Unsupported format string + # ValueError: Unknown format code return iter([util.Uninferable]) return iter([nodes.const_factory(formatted_string)]) diff --git a/tests/unittest_brain_builtin.py b/tests/unittest_brain_builtin.py index dd99444b1..6f7038fb9 100644 --- a/tests/unittest_brain_builtin.py +++ b/tests/unittest_brain_builtin.py @@ -103,6 +103,12 @@ def test_string_format(self, format_string: str) -> None: """ "My name is {fname}, I'm {age}".format(fsname = "Daniel", age = 12) """, + """ + "My unicode character is {:c}".format(None) + """, + """ + "My hex format is {:4x}".format('1') + """, ], ) def test_string_format_uninferable(self, format_string: str) -> None: