timothycrosley/hypothesis-auto

Dictionary parameters does not seem to work

Opened this issue · 1 comments

Declaring my parameters with dict, I was expecting the dictionaries strategy to work out of the box but it does not seem to be so. I was only seeing {} as generated values even with 100 runs.
So, I tried alternative type hints:

Example test case:

def my_function(one: dict, two: Dict[str, int], three: Mapping[str, int]):
    print('one', one, 'two', two, 'three', three)

With such examples too, I'm only seeing outputs like:

one {} two {} three {}
one {} two {} three {'': 0}

I think the issue is it should be using builds(dict) at the very least but it's not.

This is due to hypothesis.strategies.from_type(dict) only ever generating empty dictionaries, which in turn is because it doesn't have any element type annotations. For example:

>>> from typing import Dict, Mapping
>>> from hypothesis.strategies import from_type

>>> from_type(dict)
builds(dict)  # Calls `dict()`, which always returns `{}`, an empty dict

>>> from_type(Dict[str, int])
dictionaries(keys=text(), values=integers())

>>> from_type(Mapping[str, int])
one_of(
    dictionaries(keys=text(), values=integers()),
    dictionaries(keys=text(), values=integers()).map(ChainMap)
)

Remaining weirdness is because hypothesis-auto doesn't really use Hypothesis as it's meant to be used - generating a list of inputs up front breaks all our feedback, replay, and shrinking logic 😞