Skip to content

Commit 4240f8a

Browse files
authored
Merge pull request #4452 from tybug/lambda-source-error
Remove lambda source code assertion
2 parents 30f5cea + 2c4928b commit 4240f8a

3 files changed

Lines changed: 27 additions & 3 deletions

File tree

hypothesis-python/RELEASE.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
RELEASE_TYPE: patch
2+
3+
Remove an internal assertion which could trigger if (1) a lambda was present in the source code of a test, (2) and the source code file was edited on disk between the start of the python process and when Hypothesis runs the property.

hypothesis-python/src/hypothesis/internal/reflection.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -330,9 +330,13 @@ def _extract_lambda_source(f):
330330
source = LINE_CONTINUATION.sub(" ", source)
331331
source = WHITESPACE.sub(" ", source)
332332
source = source.strip()
333-
if "lambda" not in source and sys.platform == "emscripten": # pragma: no cover
334-
return if_confused # work around Pyodide bug in inspect.getsource()
335-
assert "lambda" in source, source
333+
if "lambda" not in source: # pragma: no cover
334+
# If a user starts a hypothesis process, then edits their code, the lines
335+
# in the parsed source code might not match the live __code__ objects.
336+
#
337+
# (and on sys.platform == "emscripten", this can happen regardless
338+
# due to a pyodide bug in inspect.getsource()).
339+
return if_confused
336340

337341
tree = None
338342

hypothesis-python/tests/cover/test_lambda_formatting.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
99
# obtain one at https://mozilla.org/MPL/2.0/.
1010

11+
import runpy
12+
1113
from hypothesis.internal.conjecture.utils import identity
1214
from hypothesis.internal.reflection import get_pretty_function_description
1315

@@ -175,3 +177,18 @@ def test_can_handle_nested_lambda_in_decorator_argument():
175177
assert (
176178
get_pretty_function_description(decorator_with_wrapper[0]) == "lambda x: x + 1"
177179
)
180+
181+
182+
def test_modifying_lambda_source_code_returns_unknown(tmp_path):
183+
# see https://github.com/HypothesisWorks/hypothesis/pull/4452
184+
test_module = tmp_path / "test_module.py"
185+
test_module.write_text(
186+
"# line one\n\ntest_lambda = lambda x: x * 2", encoding="utf-8"
187+
)
188+
189+
module_globals = runpy.run_path(str(test_module))
190+
test_module.write_text("# line one\n\n# line two", encoding="utf-8")
191+
assert (
192+
get_pretty_function_description(module_globals["test_lambda"])
193+
== "lambda x: <unknown>"
194+
)

0 commit comments

Comments
 (0)