(self)
| 1083 | |
| 1084 | @unittest.expectedFailure # TODO: RUSTPYTHON; AssertionError: "f-string: expecting a valid expression after '{'" does not match "invalid syntax (<string>, line 1)" |
| 1085 | def test_lambda(self): |
| 1086 | x = 5 |
| 1087 | self.assertEqual(f'{(lambda y:x*y)("8")!r}', "'88888'") |
| 1088 | self.assertEqual(f'{(lambda y:x*y)("8")!r:10}', "'88888' ") |
| 1089 | self.assertEqual(f'{(lambda y:x*y)("8"):10}', "88888 ") |
| 1090 | |
| 1091 | # lambda doesn't work without parens, because the colon |
| 1092 | # makes the parser think it's a format_spec |
| 1093 | # emit warning if we can match a format_spec |
| 1094 | self.assertAllRaise(SyntaxError, |
| 1095 | "f-string: lambda expressions are not allowed " |
| 1096 | "without parentheses", |
| 1097 | ["f'{lambda x:x}'", |
| 1098 | "f'{lambda :x}'", |
| 1099 | "f'{lambda *arg, :x}'", |
| 1100 | "f'{1, lambda:x}'", |
| 1101 | "f'{lambda x:}'", |
| 1102 | "f'{lambda :}'", |
| 1103 | ]) |
| 1104 | # Ensure the detection of invalid lambdas doesn't trigger detection |
| 1105 | # for valid lambdas in the second error pass |
| 1106 | with self.assertRaisesRegex(SyntaxError, "invalid syntax"): |
| 1107 | compile("lambda name_3=f'{name_4}': {name_3}\n1 $ 1", "<string>", "exec") |
| 1108 | |
| 1109 | # but don't emit the paren warning in general cases |
| 1110 | with self.assertRaisesRegex(SyntaxError, "f-string: expecting a valid expression after '{'"): |
| 1111 | eval("f'{+ lambda:None}'") |
| 1112 | |
| 1113 | def test_valid_prefixes(self): |
| 1114 | self.assertEqual(F'{1}', "1") |
nothing calls this directly
no test coverage detected