| 1 |
# This file is part of Hypothesis, which may be found at |
| 2 |
# https://github.com/HypothesisWorks/hypothesis/ |
| 3 |
# |
| 4 |
# Copyright the Hypothesis Authors. |
| 5 |
# Individual contributors are listed in AUTHORS.rst and the git log. |
| 6 |
# |
| 7 |
# This Source Code Form is subject to the terms of the Mozilla Public License, |
| 8 |
# v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 9 |
# obtain one at https://mozilla.org/MPL/2.0/. |
| 10 |
|
| 11 |
import runpy |
| 12 |
import sys |
| 13 |
|
| 14 |
import pytest |
| 15 |
|
| 16 |
from hypothesis.internal import lambda_sources |
| 17 |
from hypothesis.internal.conjecture.utils import identity |
| 18 |
from hypothesis.internal.reflection import get_pretty_function_description |
| 19 |
|
| 20 |
from tests.common.utils import skipif_threading |
| 21 |
|
| 22 |
|
| 23 |
@pytest.fixture(autouse=True, params=[True, False]) |
| 24 |
def clear_lambda_caches(request, monkeypatch): |
| 25 |
# Run all tests in this file twice, once using cache and once forcing |
| 26 |
# from-scratch generation |
| 27 |
if request.param: |
| 28 |
monkeypatch.setattr(lambda_sources, "LAMBDA_DESCRIPTION_CACHE", {}) |
| 29 |
monkeypatch.setattr(lambda_sources, "LAMBDA_DIGEST_DESCRIPTION_CACHE", {}) |
| 30 |
monkeypatch.setattr(lambda_sources, "AST_LAMBDAS_CACHE", {}) |
| 31 |
return request.param |
| 32 |
|
| 33 |
|
| 34 |
def test_bracket_whitespace_is_stripped(): |
| 35 |
# fmt: off |
| 36 |
t = lambda x: (x + 1 ) |
| 37 |
# fmt: on |
| 38 |
assert get_pretty_function_description(t) == "lambda x: x + 1" |
| 39 |
|
| 40 |
|
| 41 |
def test_can_have_unicode_in_lambda_sources(): |
| 42 |
t = lambda x: "é" not in x |
| 43 |
assert get_pretty_function_description(t) == "lambda x: 'é' not in x" |
| 44 |
|
| 45 |
|
| 46 |
@pytest.mark.skipif( |
| 47 |
sys.version_info < (3, 11), reason="nested lambdas fail compile-test" |
| 48 |
) |
| 49 |
def test_can_get_descriptions_of_nested_lambdas_with_different_names(): |
| 50 |
# fmt: off |
| 51 |
ordered_pair = ( |
| 52 |
lambda right: [].map( |
| 53 |
lambda length: ())) |
| 54 |
# fmt: on |
| 55 |
assert ( |
| 56 |
get_pretty_function_description(ordered_pair) |
| 57 |
== "lambda right: [].map(lambda length: ())" |
| 58 |
) |
| 59 |
|
| 60 |
|
| 61 |
def test_does_not_error_on_unparsable_source(): |
| 62 |
# fmt: off |
| 63 |
t = [ |
| 64 |
lambda x: \ |
| 65 |
# This will break ast.parse, but the brackets are needed for the real |
| 66 |
# parser to accept this lambda |
| 67 |
x][0] |
| 68 |
# fmt: on |
| 69 |
assert get_pretty_function_description(t) == "lambda x: x" |
| 70 |
|
| 71 |
|
| 72 |
@pytest.mark.skipif( |
| 73 |
sys.version_info < (3, 11), reason="nested lambdas fail compile-test" |
| 74 |
) |
| 75 |
def test_separate_line_map_filter(): |
| 76 |
# this isn't intentionally testing nested lambdas, but hey, it's a nice bonus. |
| 77 |
# fmt: off |
| 78 |
f1 = ( |
| 79 |
lambda x: x |
| 80 |
.map(lambda y: y) |
| 81 |
) |
| 82 |
f2 = ( |
| 83 |
lambda x: x |
| 84 |
.filter(lambda y: y) |
| 85 |
) |
| 86 |
# fmt: on |
| 87 |
|
| 88 |
assert get_pretty_function_description(f1) == "lambda x: x.map(lambda y: y)" |
| 89 |
assert get_pretty_function_description(f2) == "lambda x: x.filter(lambda y: y)" |
| 90 |
|
| 91 |
|
| 92 |
def test_source_of_lambda_is_pretty(): |
| 93 |
assert get_pretty_function_description(lambda x: True) == "lambda x: True" |
| 94 |
|
| 95 |
|
| 96 |
def test_variable_names_are_not_pretty(): |
| 97 |
t = lambda x: True |
| 98 |
assert get_pretty_function_description(t) == "lambda x: True" |
| 99 |
|
| 100 |
|
| 101 |
def test_does_not_error_on_dynamically_defined_functions(): |
| 102 |
x = eval("lambda t: 1") |
| 103 |
assert get_pretty_function_description(x) == "lambda t: <unknown>" |
| 104 |
|
| 105 |
|
| 106 |
def test_collapses_whitespace_nicely(): |
| 107 |
# fmt: off |
| 108 |
t = ( |
| 109 |
lambda x, y: 1 |
| 110 |
) |
| 111 |
# fmt: on |
| 112 |
assert get_pretty_function_description(t) == "lambda x, y: 1" |
| 113 |
|
| 114 |
|
| 115 |
def test_is_not_confused_by_tuples(): |
| 116 |
p = (lambda x: x > 1, 2)[0] |
| 117 |
|
| 118 |
assert get_pretty_function_description(p) == "lambda x: x > 1" |
| 119 |
|
| 120 |
|
| 121 |
def test_strips_comments_from_the_end(): |
| 122 |
t = lambda x: 1 # A lambda comment |
| 123 |
assert get_pretty_function_description(t) == "lambda x: 1" |
| 124 |
|
| 125 |
|
| 126 |
def test_does_not_strip_hashes_within_a_string(): |
| 127 |
t = lambda x: "#" |
| 128 |
assert get_pretty_function_description(t) == "lambda x: '#'" |
| 129 |
|
| 130 |
|
| 131 |
def test_can_distinguish_between_two_lambdas_with_different_args(): |
| 132 |
a, b = (lambda x: 1, lambda y: 2) |
| 133 |
assert get_pretty_function_description(a) == "lambda x: 1" |
| 134 |
assert get_pretty_function_description(b) == "lambda y: 2" |
| 135 |
|
| 136 |
|
| 137 |
def test_can_distinguish_between_two_lambdas_with_different_constants(): |
| 138 |
a, b = (lambda x: 1, lambda x: 2) |
| 139 |
assert get_pretty_function_description(a) == "lambda x: 1" |
| 140 |
assert get_pretty_function_description(b) == "lambda x: 2" |
| 141 |
|
| 142 |
|
| 143 |
def test_can_distinguish_between_two_lambdas_with_different_signatures(): |
| 144 |
a, b = (lambda x: x, lambda y: y) |
| 145 |
assert get_pretty_function_description(a) == "lambda x: x" |
| 146 |
assert get_pretty_function_description(b) == "lambda y: y" |
| 147 |
|
| 148 |
|
| 149 |
def test_can_distinguish_between_two_lambdas_where_one_fails_localparse(): |
| 150 |
# fmt: off |
| 151 |
a, b = ( |
| 152 |
lambda x: x, |
| 153 |
lambda x: x |
| 154 |
or None, |
| 155 |
) |
| 156 |
# fmt: on |
| 157 |
assert get_pretty_function_description(a) == "lambda x: x" |
| 158 |
assert get_pretty_function_description(b) == "lambda x: x or None" |
| 159 |
|
| 160 |
|
| 161 |
def test_does_not_get_confused_by_identical_lambdas(): |
| 162 |
a, b = (lambda x: 1, lambda x: 1) |
| 163 |
assert get_pretty_function_description(a) == "lambda x: 1" |
| 164 |
assert get_pretty_function_description(b) == "lambda x: 1" |
| 165 |
|
| 166 |
|
| 167 |
c = 1 |
| 168 |
lambda_capturing_globals = (lambda: c, lambda: 1) |
| 169 |
|
| 170 |
|
| 171 |
def test_lambda_capturing_globals(): |
| 172 |
assert get_pretty_function_description(lambda_capturing_globals[0]) == "lambda: c" |
| 173 |
assert get_pretty_function_description(lambda_capturing_globals[1]) == "lambda: 1" |
| 174 |
|
| 175 |
|
| 176 |
def test_lambda_capturing_locals(): |
| 177 |
const = 1 |
| 178 |
a, b = (lambda: const, lambda: 1) |
| 179 |
assert get_pretty_function_description(a) == "lambda: const" |
| 180 |
assert get_pretty_function_description(b) == "lambda: 1" |
| 181 |
|
| 182 |
|
| 183 |
def test_can_distinguish_between_two_lambdas_with_different_captures(): |
| 184 |
# fmt: off |
| 185 |
a = 1; f1 = lambda x=a: x; a=2; f2=lambda x=a: x # noqa: E702 |
| 186 |
# fmt: on |
| 187 |
|
| 188 |
assert get_pretty_function_description(f1) == "lambda x=1: x" |
| 189 |
assert get_pretty_function_description(f2) == "lambda x=2: x" |
| 190 |
|
| 191 |
|
| 192 |
def test_lambda_source_break_after_bracket(): |
| 193 |
# Issue #4498 regression test, inspect.getsource only sees the first line |
| 194 |
# fmt: off |
| 195 |
f = ( |
| 196 |
lambda x: x |
| 197 |
or None |
| 198 |
) |
| 199 |
# fmt: on |
| 200 |
|
| 201 |
source = get_pretty_function_description(f) |
| 202 |
assert source == "lambda x: x or None" |
| 203 |
|
| 204 |
|
| 205 |
def test_lambda_source_break_after_def_with_brackets(): |
| 206 |
# fmt: off |
| 207 |
f = (lambda n: |
| 208 |
'aaa') |
| 209 |
# fmt: on |
| 210 |
|
| 211 |
source = get_pretty_function_description(f) |
| 212 |
assert source == "lambda n: 'aaa'" |
| 213 |
|
| 214 |
|
| 215 |
def test_lambda_source_break_after_def_with_line_continuation(): |
| 216 |
# fmt: off |
| 217 |
f = lambda n:\ |
| 218 |
'aaa' |
| 219 |
# fmt: on |
| 220 |
|
| 221 |
source = get_pretty_function_description(f) |
| 222 |
assert source == "lambda n: 'aaa'" |
| 223 |
|
| 224 |
|
| 225 |
def arg_decorator(*s): |
| 226 |
def accept(f): |
| 227 |
return s |
| 228 |
|
| 229 |
return accept |
| 230 |
|
| 231 |
|
| 232 |
@arg_decorator(lambda x: x + 1) |
| 233 |
def plus_one(): |
| 234 |
pass |
| 235 |
|
| 236 |
|
| 237 |
@arg_decorator(lambda x: x + 1, lambda y: y * 2) |
| 238 |
def two_decorators(): |
| 239 |
pass |
| 240 |
|
| 241 |
|
| 242 |
def test_can_extract_lambda_repr_in_a_decorator(): |
| 243 |
assert get_pretty_function_description(plus_one[0]) == "lambda x: x + 1" |
| 244 |
|
| 245 |
|
| 246 |
def test_can_extract_two_lambdas_from_a_decorator_if_args_differ(): |
| 247 |
a, b = two_decorators |
| 248 |
assert get_pretty_function_description(a) == "lambda x: x + 1" |
| 249 |
assert get_pretty_function_description(b) == "lambda y: y * 2" |
| 250 |
|
| 251 |
|
| 252 |
@arg_decorator(lambda: ()) |
| 253 |
def to_brackets(): |
| 254 |
pass |
| 255 |
|
| 256 |
|
| 257 |
def test_can_handle_brackets_in_decorator_argument(): |
| 258 |
assert get_pretty_function_description(to_brackets[0]) == "lambda: ()" |
| 259 |
|
| 260 |
|
| 261 |
@arg_decorator(identity(lambda x: x + 1)) |
| 262 |
def decorator_with_wrapper(): |
| 263 |
pass |
| 264 |
|
| 265 |
|
| 266 |
def test_can_handle_nested_lambda_in_decorator_argument(): |
| 267 |
assert ( |
| 268 |
get_pretty_function_description(decorator_with_wrapper[0]) == "lambda x: x + 1" |
| 269 |
) |
| 270 |
|
| 271 |
|
| 272 |
@skipif_threading # concurrent writes to the same file |
| 273 |
def test_modifying_lambda_source_code_returns_unknown(tmp_path): |
| 274 |
# see https://github.com/HypothesisWorks/hypothesis/pull/4452 |
| 275 |
test_module = tmp_path / "test_module.py" |
| 276 |
test_module.write_text( |
| 277 |
"# line one\n\ntest_lambda = lambda x: x * 2", encoding="utf-8" |
| 278 |
) |
| 279 |
|
| 280 |
module_globals = runpy.run_path(str(test_module)) |
| 281 |
test_module.write_text("# line one\n\n# line two", encoding="utf-8") |
| 282 |
assert ( |
| 283 |
get_pretty_function_description(module_globals["test_lambda"]) |
| 284 |
== "lambda x: <unknown>" |
| 285 |
) |
| 286 |
|
| 287 |
|
| 288 |
@skipif_threading # concurrent writes to the same file |
| 289 |
def test_adding_other_lambda_does_not_confuse(tmp_path): |
| 290 |
test_module = tmp_path / "test_module.py" |
| 291 |
test_module.write_text( |
| 292 |
"# line one\ntest_lambda = lambda x: x * 2", encoding="utf-8" |
| 293 |
) |
| 294 |
module_globals = runpy.run_path(str(test_module)) |
| 295 |
|
| 296 |
test_module.write_text( |
| 297 |
"# line one\nlambda x: x\n\n\ntest_lambda = lambda x: x * 2", encoding="utf-8" |
| 298 |
) |
| 299 |
f1 = module_globals["test_lambda"] |
| 300 |
assert get_pretty_function_description(f1) == "lambda x: x * 2" |
| 301 |
|
| 302 |
|
| 303 |
@skipif_threading # concurrent writes to the same file |
| 304 |
def test_changing_lambda_confuses(tmp_path, allow_unknown_lambdas, clear_lambda_caches): |
| 305 |
if not clear_lambda_caches: |
| 306 |
pytest.skip("requires clean cache") |
| 307 |
test_module = tmp_path / "test_module.py" |
| 308 |
test_module.write_text("test_lambda = lambda x: x * 2", encoding="utf-8") |
| 309 |
module_globals = runpy.run_path(str(test_module)) |
| 310 |
|
| 311 |
test_module.write_text("lambda x: x", encoding="utf-8") |
| 312 |
f1 = module_globals["test_lambda"] |
| 313 |
assert get_pretty_function_description(f1) == "lambda x: <unknown>" |
| 314 |
|
| 315 |
|
| 316 |
@skipif_threading # concurrent writes to the same file |
| 317 |
@pytest.mark.skipif(sys.version_info[:2] < (3, 11), reason="not checked before 3.11") |
| 318 |
def test_that_test_harness_raises_on_unknown_lambda(tmp_path): |
| 319 |
test_module = tmp_path / "test_module.py" |
| 320 |
test_module.write_text("test_lambda = lambda x: x * 2", encoding="utf-8") |
| 321 |
module_globals = runpy.run_path(str(test_module)) |
| 322 |
|
| 323 |
test_module.write_text("lambda x: x", encoding="utf-8") |
| 324 |
f1 = module_globals["test_lambda"] |
| 325 |
with pytest.raises(AssertionError): |
| 326 |
assert get_pretty_function_description(f1) == "lambda x: <unknown>" |
| 327 |
|
| 328 |
|
| 329 |
@skipif_threading # concurrent writes to the same file |
| 330 |
def test_source_with_syntax_error(tmp_path, allow_unknown_lambdas, clear_lambda_caches): |
| 331 |
if not clear_lambda_caches: |
| 332 |
pytest.skip("requires clean cache") |
| 333 |
test_module = tmp_path / "test_module.py" |
| 334 |
test_module.write_text("test_lambda = lambda x: x * 2", encoding="utf-8") |
| 335 |
module_globals = runpy.run_path(str(test_module)) |
| 336 |
f1 = module_globals["test_lambda"] |
| 337 |
|
| 338 |
test_module.write_text("line 1", encoding="utf-8") |
| 339 |
assert get_pretty_function_description(f1) == "lambda x: <unknown>" |
| 340 |
|
| 341 |
|
| 342 |
@skipif_threading # concurrent writes to the same file |
| 343 |
def test_unknown_is_not_stuck(tmp_path, allow_unknown_lambdas, clear_lambda_caches): |
| 344 |
if not clear_lambda_caches: |
| 345 |
pytest.skip("requires clean cache") |
| 346 |
test_module = tmp_path / "test_module.py" |
| 347 |
test_module.write_text("test_lambda = lambda x: x * 2", encoding="utf-8") |
| 348 |
module_globals = runpy.run_path(str(test_module)) |
| 349 |
f1 = module_globals["test_lambda"] |
| 350 |
|
| 351 |
test_module.write_text("line 1", encoding="utf-8") |
| 352 |
assert get_pretty_function_description(f1) == "lambda x: <unknown>" |
| 353 |
|
| 354 |
test_module2 = tmp_path / "test_module2.py" |
| 355 |
test_module2.write_text("test_lambda = lambda x: x * 2", encoding="utf-8") |
| 356 |
module_globals2 = runpy.run_path(str(test_module2)) |
| 357 |
f2 = module_globals2["test_lambda"] |
| 358 |
|
| 359 |
# f2 matches f1 in the digest cache, ensure that it gets a proper description |
| 360 |
assert get_pretty_function_description(f1) == "lambda x: <unknown>" |
| 361 |
assert get_pretty_function_description(f2) == "lambda x: x * 2" |