Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assert "lambda" in source, source does not hold #3956

Closed
pspacek opened this issue Apr 26, 2024 · 2 comments
Closed

assert "lambda" in source, source does not hold #3956

pspacek opened this issue Apr 26, 2024 · 2 comments

Comments

@pspacek
Copy link

pspacek commented Apr 26, 2024

Hello,

I don't really understand what is going on here... But code comment

def extract_lambda_source(f):
...
        This is not a good function and I am sorry for it. Forgive me my
        sins, oh lord

makes it clear that some improvement might be in order, so I'm trying to provide information that something bad is happening here.

Version affected:

  • hypothesis 6.100.1
  • pytest 8.1.1

Traceback:

/usr/lib/python3.11/site-packages/hypothesis/stateful.py:175: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
/usr/lib/python3.11/site-packages/hypothesis/internal/conjecture/data.py:2379: in draw
    return strategy.do_draw(self)
/usr/lib/python3.11/site-packages/hypothesis/stateful.py:992: in do_draw
    rule = data.draw(st.sampled_from(self.rules).filter(rule_is_enabled))
/usr/lib/python3.11/site-packages/hypothesis/internal/conjecture/data.py:2379: in draw
    return strategy.do_draw(self)
/usr/lib/python3.11/site-packages/hypothesis/strategies/_internal/strategies.py:543: in do_draw
    result = self.do_filtered_draw(data)
/usr/lib/python3.11/site-packages/hypothesis/strategies/_internal/strategies.py:569: in do_filtered_draw
    element = self.get_element(i)
/usr/lib/python3.11/site-packages/hypothesis/strategies/_internal/strategies.py:557: in get_element
    return self._transform(self.elements[i])
/usr/lib/python3.11/site-packages/hypothesis/strategies/_internal/strategies.py:538: in _transform
    if not f(element):
/usr/lib/python3.11/site-packages/hypothesis/stateful.py:990: in rule_is_enabled
    return self.is_valid(r) and feature_flags.is_enabled(r.function.__name__)
/usr/lib/python3.11/site-packages/hypothesis/stateful.py:1009: in is_valid
    where = f"{desc} precondition {get_pretty_function_description(pred)}"
/usr/lib/python3.11/site-packages/hypothesis/internal/reflection.py:449: in get_pretty_function_description
    return extract_lambda_source(f)

...

        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, source
E       AssertionError: @rule(target=names, pyname=subdomains(names)) def add_subdomain(self, pyname): event("ADD subdomain") return self._add(pyname)
E       assert 'lambda' in '@rule(target=names, pyname=subdomains(names)) def add_subdomain(self, pyname): event("ADD subdomain") return self._add(pyname)'

/usr/lib/python3.11/site-packages/hypothesis/internal/reflection.py:337: AssertionError

During handling of the above exception, another exception occurred:

self = <hypothesis.stateful.BareQPTest.TestCase testMethod=runTest>

    def runTest(self):
>       run_state_machine_as_test(cls, settings=self.settings)

/usr/lib/python3.11/site-packages/hypothesis/stateful.py:434: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
/usr/lib/python3.11/site-packages/hypothesis/stateful.py:242: in run_state_machine_as_test
    run_state_machine(state_machine_factory)
/usr/lib/python3.11/site-packages/hypothesis/stateful.py:117: in run_state_machine
    @given(st.data())
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

    def inconsistent_generation():
>       raise Flaky(
            "Inconsistent data generation! Data generation behaved differently "
            "between different runs. Is your data generation depending on external "
            "state?"
        )
E       hypothesis.errors.Flaky: Inconsistent data generation! Data generation behaved differently between different runs. Is your data generation depending on external state?

/usr/lib/python3.11/site-packages/hypothesis/internal/conjecture/datatree.py:47: Flaky

I suspect this might be caused by this part of test code (example is heavily abbreviated and does not really test anything here):

from hypothesis import assume, example, event, given
from hypothesis.stateful import Bundle, RuleBasedStateMachine, rule, precondition
import hypothesis

pytest.importorskip("dns", minversion="2.0.0")
import dns.name

from strategies import dns_names, composite

@composite
def subdomains(draw, named_bundle):
    parent = draw(named_bundle)
    # the parent name has less then two bytes left, no way to add a subdomain to it
    if len(parent) + sum(map(len, parent)) > 253:
        return parent
    subdomain = draw(dns_names(suffix=parent))
    return subdomain

class BareQPTest(RuleBasedStateMachine):
    def __init__(self):
        super().__init__()
        self.generation = 0
        self.model = {}

    @rule(target=names, pyname=dns_names())
    def add_random(self, pyname):
        event("ADD random")
        return self._add(pyname)

    # hack to increase likelihood that named bundle is not empty
    @precondition(lambda self: len(self.model) > 0)
    @rule(target=names, pyname=subdomains(names))
    def add_subdomain(self, pyname):
        event("ADD subdomain")
        return self._add(pyname)

    def _add(self, pyname):
        exists = pyname not in self.model
        print("insert", exists, pyname)
        event("INSERT", exists)
        if pyname not exists:
            self.model[pyname] = True

        return pyname

dns_names() is custom strategy based on dnspython library:
strategies.py.gz

@Zac-HD
Copy link
Member

Zac-HD commented Apr 26, 2024

I can't reproduce from your code sample because it has a SyntaxError - if you can provide a small code sample which reproduces the problem with as few dependencies as possible, that would be great.

@pspacek
Copy link
Author

pspacek commented Apr 29, 2024

I'm sorry, I'm not able to reproduce it in any sensibly small setup - and the original involves Python CFFI ... I'm going to close this and reopen if I eventually find out what has happened.

@pspacek pspacek closed this as completed Apr 29, 2024
@pspacek pspacek closed this as not planned Won't fix, can't repro, duplicate, stale Apr 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants