Skip to content

Commit cc1aee8

Browse files
committed
cache extract_lambda_source
1 parent 8005910 commit cc1aee8

File tree

4 files changed

+11
-9
lines changed

4 files changed

+11
-9
lines changed

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+
This patch fixes a significant slowdown when using the :func:`~hypothesis.stateful.precondition` decorator in some cases, due to expensive repr formatting internally (:issue:`3963`).

hypothesis-python/src/RELEASE.rst

Lines changed: 0 additions & 3 deletions
This file was deleted.

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import textwrap
2222
import types
2323
import warnings
24-
from functools import partial, wraps
24+
from functools import lru_cache, partial, wraps
2525
from io import StringIO
2626
from keyword import iskeyword
2727
from random import _inst as global_random_instance
@@ -303,6 +303,7 @@ def visit_Lambda(self, node):
303303
SPACE_PRECEDES_CLOSE_BRACKET = re.compile(r" \)")
304304

305305

306+
@lru_cache(maxsize=1024)
306307
def extract_lambda_source(f):
307308
"""Extracts a single lambda expression from the string source. Returns a
308309
string indicating an unknown body if it gets confused in any way.

hypothesis-python/tests/cover/test_reflection.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -541,12 +541,10 @@ def test_required_args(target, args, kwargs, expected):
541541
assert required_args(target, args, kwargs) == expected
542542

543543

544-
# fmt: off
545-
pi = "π"; is_str_pi = lambda x: x == pi # noqa: E702
546-
# fmt: on
547-
548-
549544
def test_can_handle_unicode_identifier_in_same_line_as_lambda_def():
545+
# fmt: off
546+
pi = "π"; is_str_pi = lambda x: x == pi # noqa: E702
547+
# fmt: on
550548
assert get_pretty_function_description(is_str_pi) == "lambda x: x == pi"
551549

552550

@@ -567,6 +565,9 @@ def test_does_not_crash_on_utf8_lambda_without_encoding(monkeypatch):
567565
# has to fall back to assuming it's ASCII.
568566

569567
monkeypatch.setattr(reflection, "detect_encoding", None)
568+
# fmt: off
569+
pi = "π"; is_str_pi = lambda x: x == pi # noqa: E702
570+
# fmt: on
570571
assert get_pretty_function_description(is_str_pi) == "lambda x: <unknown>"
571572

572573

0 commit comments

Comments
 (0)