Skip to content

Commit 5159365

Browse files
committed
Fix reconstruction of nodes for FormattedValue. Close pylint-dev/pylint#1582
1 parent e9fe6cb commit 5159365

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

astroid/brain/brain_fstrings.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def _clone_node_with_lineno(node, parent, lineno):
2727
for param in other_fields
2828
})
2929
new_node = cls(**init_params)
30-
if hasattr(node, 'postinit'):
30+
if hasattr(node, 'postinit') and _astroid_fields:
3131
new_node.postinit(**postinit_params)
3232
return new_node
3333

@@ -58,4 +58,3 @@ def _transform_formatted_value(node):
5858
astroid.MANAGER.register_transform(
5959
astroid.FormattedValue,
6060
_transform_formatted_value)
61-

astroid/tests/unittest_brain.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,5 +768,21 @@ def test_regex_flags(self):
768768
self.assertEqual(next(re_ast[name].infer()).value, getattr(re, name))
769769

770770

771+
class BrainFStrings(unittest.TestCase):
772+
773+
def test_no_crash_on_const_reconstruction(self):
774+
node = builder.extract_node('''
775+
max_width = 10
776+
777+
test1 = f'{" ":{max_width+4}}'
778+
print(f'"{test1}"')
779+
780+
test2 = f'[{"7":>{max_width}}:0]'
781+
test2
782+
''')
783+
inferred = next(node.infer())
784+
self.assertIs(inferred, util.Uninferable)
785+
786+
771787
if __name__ == '__main__':
772788
unittest.main()

0 commit comments

Comments
 (0)