Skip to content

Commit

Permalink
Merge pull request #3934 from Zac-HD/remove-skips
Browse files Browse the repository at this point in the history
Work around weird lambda issue by popping entry from `linecache`
  • Loading branch information
Zac-HD committed Mar 23, 2024
2 parents 4c1f8e6 + 9f9420a commit 357190f
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
6 changes: 6 additions & 0 deletions hypothesis-python/RELEASE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
RELEASE_TYPE: patch

If you were running Python 3.13 (currently in alpha) with :pypi:`pytest-xdist`
and then attempted to pretty-print a ``lambda`` functions which was created
using the :func:`eval` builtin, it would have raised an AssertionError.
Now you'll get ``"lambda ...: <unknown>"``, as expected.
7 changes: 6 additions & 1 deletion hypothesis-python/src/hypothesis/internal/reflection.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import ast
import hashlib
import inspect
import linecache
import os
import re
import sys
Expand Down Expand Up @@ -315,6 +316,10 @@ def extract_lambda_source(f):
sig = inspect.signature(f)
assert sig.return_annotation in (inspect.Parameter.empty, None), sig

# Using pytest-xdist on Python 3.13, there's an entry in the linecache for
# file "<string>", which then returns nonsense to getsource. Discard it.
linecache.cache.pop("<string>", None)

if sig.parameters:
if_confused = f"lambda {str(sig)[1:-1]}: <unknown>"
else:
Expand All @@ -329,7 +334,7 @@ def extract_lambda_source(f):
source = source.strip()
if "lambda" not in source and sys.platform == "emscripten": # pragma: no cover
return if_confused # work around Pyodide bug in inspect.getsource()
assert "lambda" in source
assert "lambda" in source, source

tree = None

Expand Down
5 changes: 0 additions & 5 deletions hypothesis-python/tests/cover/test_filter_rewriting.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import math
import operator
import re
import sys
from fractions import Fraction
from functools import partial
from sys import float_info
Expand All @@ -35,10 +34,6 @@

A_FEW = 15 # speed up massively-parametrized tests

# FIXME-3.13: something about get_lambda_source not working with pytest-xdist?
if sys.version_info[:2] == (3, 13) and sys.version_info.releaselevel < "final":
pytest.skip(allow_module_level=True)


@pytest.mark.parametrize(
"strategy, predicate, start, end",
Expand Down
6 changes: 0 additions & 6 deletions hypothesis-python/tests/cover/test_lambda_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,13 @@
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at https://mozilla.org/MPL/2.0/.

import sys

import pytest

from hypothesis.internal.reflection import get_pretty_function_description


def test_bracket_whitespace_is_striped():
assert get_pretty_function_description(lambda x: (x + 1)) == "lambda x: (x + 1)"


@pytest.mark.skipif(sys.version_info[:2] == (3, 13), reason="FIXME-3.13")
def test_no_whitespace_before_colon_with_no_args():
assert get_pretty_function_description(eval("lambda: None")) == "lambda: <unknown>"

Expand Down Expand Up @@ -63,7 +58,6 @@ def test_variable_names_are_not_pretty():
assert get_pretty_function_description(t) == "lambda x: True"


@pytest.mark.skipif(sys.version_info[:2] == (3, 13), reason="FIXME-3.13")
def test_does_not_error_on_dynamically_defined_functions():
x = eval("lambda t: 1")
get_pretty_function_description(x)
Expand Down

0 comments on commit 357190f

Please sign in to comment.