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 81852e2
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 8 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.

2 changes: 2 additions & 0 deletions hypothesis-python/src/hypothesis/internal/reflection.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from types import ModuleType
from typing import TYPE_CHECKING, Any, Callable
from unittest.mock import _patch as PatchType
from functools import lru_cache

from hypothesis.errors import HypothesisWarning
from hypothesis.internal.compat import PYPY, is_typed_named_tuple
Expand Down Expand Up @@ -303,6 +304,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 81852e2

Please sign in to comment.