Skip to content

Commit bc31898

Browse files
author
Sylvain MARIE
committed
Fixed the test about formatting, and improved the error message slightly
1 parent 25c96d4 commit bc31898

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

mini_lambda/main.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,9 @@ def __format__(self, *args):
412412
"""
413413
raise FunctionDefinitionError('__format__ is not supported by _LambdaExpression, since python raises an'
414414
' error when its output is not directly an object of the type it expects.'
415-
'Please either use the equivalent x.format() method, or the Format method provided'
415+
'Please either use the equivalent x.format() method if your variable is the '
416+
'string template, or the `Format` (for `format`) or `Str.format` '
417+
'(for `str.format`) methods provided'
416418
' at mini_lambda package level.If you did not use __format__ in your expression, '
417419
'you probably used a standard method such as math.log(x) instead of a method '
418420
' converted to mini_lambda such as Log(x). Please check the documentation for '

mini_lambda/tests/test_mini_lambda.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,24 +182,32 @@ def test_evaluator_bytes():
182182
def test_evaluator_format():
183183
""" Representable Object : tests that format() works """
184184

185-
s = InputVar('s', str)
185+
# (1) the variable is the template
186+
s_tpl = InputVar('s_tpl', str)
186187

187188
# the str operator cannot be overloaded
188-
formatted_string = s.format('yes')
189-
formatted_string = formatted_string.as_function()
189+
format_my_yes = s_tpl.format('yes')
190+
format_my_yes = format_my_yes.as_function()
191+
192+
assert format_my_yes('{} sure') == 'yes sure'
190193

191-
assert formatted_string('{}') == 'yes'
194+
# (2) the variable is the value
195+
s = InputVar('s', str)
192196

193197
# the str operator cannot be overloaded
194198
with pytest.raises(FunctionDefinitionError):
195199
'{} {}'.format(s, s)
196200

197201
# so we provide this equivalent
198-
reasonable_string = Format('{} {}', s, s)
202+
reasonable_string = Str.format('{} {}', s, s)
199203
reasonable_string = reasonable_string.as_function()
200204

201205
assert reasonable_string('hello') == 'hello hello'
202206

207+
# and there is also a Format method replacing `format`
208+
stringify = Format(s).as_function()
209+
assert stringify(12) == '12'
210+
203211

204212
def test_evaluator_sizeof():
205213
""" Object : tests that sys.getsizeof() raises the correct error message and that the equivalent Getsizeof()

0 commit comments

Comments
 (0)