hypothesis-auto icon indicating copy to clipboard operation
hypothesis-auto copied to clipboard

Support `Any` type

Open sobolevn opened this issue 4 years ago • 3 comments

Hi!

I have tried this code:

from hypothesis_auto import auto_pytest_magic
from typing import Any

def under_test(arg: Any) -> bool:
    return arg == arg

auto_pytest_magic(under_test)

And it does not work:

_______________________________________________________ ERROR collecting ex.py _______________________________________________________
.venv/lib/python3.8/site-packages/pluggy/hooks.py:286: in __call__
    return self._hookexec(self, self.get_hookimpls(), kwargs)
.venv/lib/python3.8/site-packages/pluggy/manager.py:93: in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
.venv/lib/python3.8/site-packages/pluggy/manager.py:84: in <lambda>
    self._inner_hookexec = lambda hook, methods, kwargs: hook.multicall(
.venv/lib/python3.8/site-packages/_pytest/python.py:246: in pytest_pycollect_makeitem
    res = list(collector._genfunctions(name, obj))
.venv/lib/python3.8/site-packages/_pytest/python.py:413: in _genfunctions
    self.ihook.pytest_generate_tests.call_extra(methods, dict(metafunc=metafunc))
.venv/lib/python3.8/site-packages/pluggy/hooks.py:324: in call_extra
    return self(**kwargs)
.venv/lib/python3.8/site-packages/pluggy/hooks.py:286: in __call__
    return self._hookexec(self, self.get_hookimpls(), kwargs)
.venv/lib/python3.8/site-packages/pluggy/manager.py:93: in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
.venv/lib/python3.8/site-packages/pluggy/manager.py:84: in <lambda>
    self._inner_hookexec = lambda hook, methods, kwargs: hook.multicall(
.venv/lib/python3.8/site-packages/_pytest/python.py:139: in pytest_generate_tests
    metafunc.parametrize(*marker.args, **marker.kwargs, _param_mark=marker)  # type: ignore[misc] # noqa: F821
.venv/lib/python3.8/site-packages/_pytest/python.py:915: in parametrize
    argnames, parameters = ParameterSet._for_parametrize(
.venv/lib/python3.8/site-packages/_pytest/mark/structures.py:108: in _for_parametrize
    parameters = cls._parse_parametrize_parameters(argvalues, force_tuple)
.venv/lib/python3.8/site-packages/_pytest/mark/structures.py:101: in _parse_parametrize_parameters
    return [
.venv/lib/python3.8/site-packages/_pytest/mark/structures.py:101: in <listcomp>
    return [
.venv/lib/python3.8/site-packages/hypothesis_auto/tester.py:161: in auto_test_cases
    for parameters in auto_parameters(auto_function_, *args, auto_limit_=auto_limit_, **kwargs):
.venv/lib/python3.8/site-packages/hypothesis_auto/tester.py:126: in auto_parameters
    yield strategy.example()
.venv/lib/python3.8/site-packages/hypothesis/strategies/_internal/strategies.py:314: in example
    example_generating_inner_function()
.venv/lib/python3.8/site-packages/hypothesis/strategies/_internal/strategies.py:302: in example_generating_inner_function
    @settings(
.venv/lib/python3.8/site-packages/hypothesis/core.py:420: in process_arguments_to_given
    search_strategy.validate()
.venv/lib/python3.8/site-packages/hypothesis/strategies/_internal/strategies.py:377: in validate
    self.do_validate()
.venv/lib/python3.8/site-packages/hypothesis/strategies/_internal/collections.py:39: in do_validate
    s.validate()
.venv/lib/python3.8/site-packages/hypothesis/strategies/_internal/strategies.py:377: in validate
    self.do_validate()
.venv/lib/python3.8/site-packages/hypothesis/strategies/_internal/strategies.py:638: in do_validate
    self.mapped_strategy.validate()
.venv/lib/python3.8/site-packages/hypothesis/strategies/_internal/strategies.py:377: in validate
    self.do_validate()
.venv/lib/python3.8/site-packages/hypothesis/strategies/_internal/lazy.py:118: in do_validate
    w.validate()
.venv/lib/python3.8/site-packages/hypothesis/strategies/_internal/strategies.py:377: in validate
    self.do_validate()
.venv/lib/python3.8/site-packages/hypothesis/strategies/_internal/strategies.py:638: in do_validate
    self.mapped_strategy.validate()
.venv/lib/python3.8/site-packages/hypothesis/strategies/_internal/strategies.py:377: in validate
    self.do_validate()
.venv/lib/python3.8/site-packages/hypothesis/strategies/_internal/collections.py:39: in do_validate
    s.validate()
.venv/lib/python3.8/site-packages/hypothesis/strategies/_internal/strategies.py:377: in validate
    self.do_validate()
.venv/lib/python3.8/site-packages/hypothesis/strategies/_internal/lazy.py:118: in do_validate
    w.validate()
.venv/lib/python3.8/site-packages/hypothesis/strategies/_internal/strategies.py:377: in validate
    self.do_validate()
.venv/lib/python3.8/site-packages/hypothesis/strategies/_internal/strategies.py:638: in do_validate
    self.mapped_strategy.validate()
.venv/lib/python3.8/site-packages/hypothesis/strategies/_internal/strategies.py:377: in validate
    self.do_validate()
.venv/lib/python3.8/site-packages/hypothesis/strategies/_internal/lazy.py:118: in do_validate
    w.validate()
.venv/lib/python3.8/site-packages/hypothesis/strategies/_internal/strategies.py:377: in validate
    self.do_validate()
.venv/lib/python3.8/site-packages/hypothesis/strategies/_internal/collections.py:39: in do_validate
    s.validate()
.venv/lib/python3.8/site-packages/hypothesis/strategies/_internal/strategies.py:377: in validate
    self.do_validate()
.venv/lib/python3.8/site-packages/hypothesis/strategies/_internal/strategies.py:638: in do_validate
    self.mapped_strategy.validate()
.venv/lib/python3.8/site-packages/hypothesis/strategies/_internal/strategies.py:377: in validate
    self.do_validate()
.venv/lib/python3.8/site-packages/hypothesis/strategies/_internal/collections.py:39: in do_validate
    s.validate()
.venv/lib/python3.8/site-packages/hypothesis/strategies/_internal/strategies.py:377: in validate
    self.do_validate()
.venv/lib/python3.8/site-packages/hypothesis/strategies/_internal/lazy.py:118: in do_validate
    w.validate()
.venv/lib/python3.8/site-packages/hypothesis/strategies/_internal/strategies.py:378: in validate
    self.is_empty
.venv/lib/python3.8/site-packages/hypothesis/strategies/_internal/strategies.py:125: in accept
    recur(self)
.venv/lib/python3.8/site-packages/hypothesis/strategies/_internal/strategies.py:121: in recur
    mapping[strat] = getattr(strat, calculation)(recur)
.venv/lib/python3.8/site-packages/hypothesis/strategies/_internal/deferred.py:80: in calc_is_empty
    return recur(self.wrapped_strategy)
.venv/lib/python3.8/site-packages/hypothesis/strategies/_internal/deferred.py:43: in wrapped_strategy
    result = self.__definition()
.venv/lib/python3.8/site-packages/hypothesis/strategies/_internal/core.py:1385: in <lambda>
    lambda thing: deferred(lambda: _from_type(thing)),
.venv/lib/python3.8/site-packages/hypothesis/strategies/_internal/core.py:1473: in _from_type
    return types.from_typing_type(thing)
.venv/lib/python3.8/site-packages/hypothesis/strategies/_internal/types.py:229: in from_typing_type
    raise ResolutionFailed(
E   hypothesis.errors.ResolutionFailed: Could not resolve typing.Any to a strategy; consider using register_type_strategy

sobolevn avatar Aug 07 '20 16:08 sobolevn

This is working as intended: see this function and comment for an explanation.

Zac-HD avatar Dec 01 '20 11:12 Zac-HD

Yes, I remember our conversation about unbound TypeVar resolution. By the way, is it consistent with Any? Does it does not look like so from the first glance.

I can open a new issue to discuss it if there's anything to discuss 🙂

sobolevn avatar Dec 01 '20 15:12 sobolevn

An unbound TypeVar is semantically very different to Any - the former means "this could be any type, so long as it's consistent between locations", where as the latter means "we're not expressing this in the type systems (because it's too hard or too dynamic or not supported or ...)".

I don't think there's much to discuss, it's working as intended :thinking:

>>> from typing import TypeVar
>>> from hypothesis.strategies import from_type
>>> T = TypeVar("T")
>>> from_type(T)
shared(
    sampled_from([type(None), bool, int, float, str, bytes]),
    key='typevar=~T'
).flatmap(from_type)

Zac-HD avatar Dec 02 '20 02:12 Zac-HD