From 51690756956af66b9c61b447c2ccbf030c926ad1 Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Sat, 12 Nov 2022 14:19:28 -0500 Subject: [PATCH] Prevent a crash when inferring calls to `str.format` with invalid args --- ChangeLog | 5 +++++ astroid/brain/brain_builtin_inference.py | 6 ++++-- tests/unittest_brain_builtin.py | 6 ++++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index d1b64c71e..89cd00f01 100644 --- a/ChangeLog +++ b/ChangeLog @@ -29,6 +29,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: