Skip to content

Commit

Permalink
cache extract_lambda_source
Browse files Browse the repository at this point in the history
  • Loading branch information
tybug committed May 3, 2024
1 parent 8005910 commit cc1aee8
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 9 deletions.
3 changes: 3 additions & 0 deletions hypothesis-python/RELEASE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
RELEASE_TYPE: patch

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`).
3 changes: 0 additions & 3 deletions hypothesis-python/src/RELEASE.rst

This file was deleted.

3 changes: 2 additions & 1 deletion hypothesis-python/src/hypothesis/internal/reflection.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import textwrap
import types
import warnings
from functools import partial, wraps
from functools import lru_cache, partial, wraps
from io import StringIO
from keyword import iskeyword
from random import _inst as global_random_instance
Expand Down Expand Up @@ -303,6 +303,7 @@ def visit_Lambda(self, node):
SPACE_PRECEDES_CLOSE_BRACKET = re.compile(r" \)")


@lru_cache(maxsize=1024)
def extract_lambda_source(f):
"""Extracts a single lambda expression from the string source. Returns a
string indicating an unknown body if it gets confused in any way.
Expand Down
11 changes: 6 additions & 5 deletions hypothesis-python/tests/cover/test_reflection.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,12 +541,10 @@ def test_required_args(target, args, kwargs, expected):
assert required_args(target, args, kwargs) == expected


# fmt: off
pi = "π"; is_str_pi = lambda x: x == pi # noqa: E702
# fmt: on


def test_can_handle_unicode_identifier_in_same_line_as_lambda_def():
# fmt: off
pi = "π"; is_str_pi = lambda x: x == pi # noqa: E702
# fmt: on
assert get_pretty_function_description(is_str_pi) == "lambda x: x == pi"


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

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


Expand Down

0 comments on commit cc1aee8

Please sign in to comment.