Skip to content

Commit 557cef2

Browse files
committed
Don't fail horribly on destructuring lambdas
1 parent 1d5ece1 commit 557cef2

File tree

5 files changed

+45
-1
lines changed

5 files changed

+45
-1
lines changed

scripts/basic-test.sh

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ for k, v in sorted(dict(os.environ).items()):
1717

1818
python -m pytest tests/cover
1919

20+
if [ "$(python -c 'import sys; print(sys.version_info.major == 2')" = "True" ] ; then
21+
python -m pytest tests/py2
22+
fi
23+
2024
if [ "$DARWIN" != true ]; then
2125
for f in tests/nocover/*.py; do
2226
python -m pytest $f

src/hypothesis/internal/reflection.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,19 @@ def extract_lambda_source(f):
205205
206206
"""
207207
args = inspect.getargspec(f).args
208-
if_confused = 'lambda %s: <unknown>' % (', '.join(args),)
208+
arg_strings = []
209+
bad_lambda = False
210+
for a in args:
211+
if isinstance(a, (tuple, list)):
212+
arg_strings.append('(%s)' % (', '.join(a),))
213+
bad_lambda = True
214+
else:
215+
assert isinstance(a, str)
216+
arg_strings.append(a)
217+
218+
if_confused = 'lambda %s: <unknown>' % (', '.join(arg_strings),)
219+
if bad_lambda:
220+
return if_confused
209221
try:
210222
source = inspect.getsource(f)
211223
except IOError:

src/hypothesis/searchstrategy/reprwrapper.py

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from __future__ import division, print_function, absolute_import, \
1818
unicode_literals
1919

20+
import inspect
2021
from hypothesis.searchstrategy.wrappers import WrapperStrategy
2122

2223

@@ -34,6 +35,8 @@ def __init__(self, strategy, representation):
3435
self.representation = representation
3536

3637
def __repr__(self):
38+
if inspect.isfunction(self.representation):
39+
self.representation = self.representation()
3740
return self.representation
3841

3942
def draw_parameter(self, random):

tests/py2/__init__.py

Whitespace-only changes.

tests/py2/test_lambdas.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# coding=utf-8
2+
3+
# This file is part of Hypothesis (https://github.com/DRMacIver/hypothesis)
4+
5+
# Most of this work is copyright (C) 2013-2015 David R. MacIver
6+
# (david@drmaciver.com), but it contains contributions by other. See
7+
# https://github.com/DRMacIver/hypothesis/blob/master/CONTRIBUTING.rst for a
8+
# full list of people who may hold copyright, and consult the git log if you
9+
# need to determine who owns an individual contribution.
10+
11+
# This Source Code Form is subject to the terms of the Mozilla Public License,
12+
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
13+
# obtain one at http://mozilla.org/MPL/2.0/.
14+
15+
# END HEADER
16+
17+
from __future__ import division, print_function, absolute_import, \
18+
unicode_literals
19+
20+
from hypothesis.internal.reflection import get_pretty_function_description
21+
22+
23+
def test_destructuring_lambdas():
24+
assert get_pretty_function_description(lambda (x, y): 1) == \
25+
'lambda (x, y): <unknown>'

0 commit comments

Comments
 (0)