MCPcopy Index your code
hub / github.com/python/cpython / test_lambda

Method test_lambda

Lib/test/test_fstring.py:1073–1099  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

10714}''', '7')
1072
1073 def test_lambda(self):
1074 x = 5
1075 self.assertEqual(f'{(lambda y:x*y)("8")!r}', "'88888'")
1076 self.assertEqual(f'{(lambda y:x*y)("8")!r:10}', "'88888' ")
1077 self.assertEqual(f'{(lambda y:x*y)("8"):10}', "88888 ")
1078
1079 # lambda doesn't work without parens, because the colon
1080 # makes the parser think it's a format_spec
1081 # emit warning if we can match a format_spec
1082 self.assertAllRaise(SyntaxError,
1083 "f-string: lambda expressions are not allowed "
1084 "without parentheses",
1085 ["f'{lambda x:x}'",
1086 "f'{lambda :x}'",
1087 "f'{lambda *arg, :x}'",
1088 "f'{1, lambda:x}'",
1089 "f'{lambda x:}'",
1090 "f'{lambda :}'",
1091 ])
1092 # Ensure the detection of invalid lambdas doesn't trigger detection
1093 # for valid lambdas in the second error pass
1094 with self.assertRaisesRegex(SyntaxError, "invalid syntax"):
1095 compile("lambda name_3=f'{name_4}': {name_3}\n1 $ 1", "<string>", "exec")
1096
1097 # but don't emit the paren warning in general cases
1098 with self.assertRaisesRegex(SyntaxError, "f-string: expecting a valid expression after '{'"):
1099 eval("f'{+ lambda:None}'")
1100
1101 def test_valid_prefixes(self):
1102 self.assertEqual(F'{1}', "1")

Callers

nothing calls this directly

Calls 4

assertEqualMethod · 0.95
assertAllRaiseMethod · 0.95
assertRaisesRegexMethod · 0.95
compileFunction · 0.50

Tested by

no test coverage detected