Skip to content

How to get hypothesmith to generate a FunctionDef with arguments? #31

@julian-goettingen

Description

@julian-goettingen

I have been experimenting with this library for a bit for purposes of testing refactoring tools. I can generate lots of extremely weird identifiers for all kinds of things, but the structure of everything generated seems fairly simple - too simple.

For example, I am trying to generate function signatures. I imagine many possibilities with the arguments - they can have annotations, positional-only-args, the **kwargs-keyword, default-arguments, etc etc.

Here is my code to generate function-definitions that have at least something in their argument brackets (execute with pytests -s flag to show the prints):

import hypothesmith
from hypothesis import given, settings, HealthCheck, assume
import libcst
import re

s = hypothesmith.from_node(node=libcst.FunctionDef, auto_target=True)

@given(code = s)
@settings(suppress_health_check=[HealthCheck.filter_too_much, HealthCheck.too_slow], max_examples=5000)
def test(code):
    assume(re.search(r"\([^)(]+\)\s*:", code) is not None) # should match 'def foo(x):pass' but not 'def foo():pass'
    print("------")
    print(code)
    print("------")

This calculates for a long time but finds absolutely nothing. What am I doing wrong? Or did I misunderstand the purpose of this library?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions