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

Strategie from_type fails in some cases when using tuple instead of Tuple as type #3527

Closed
jldiaz opened this issue Dec 9, 2022 · 2 comments
Labels
bug something is clearly wrong here

Comments

@jldiaz
Copy link

jldiaz commented Dec 9, 2022

There is a simple example:

from dataclasses import dataclass
from typing import Tuple
from hypothesis import given, strategies as st

@dataclass
class ItWorks:
    a: dict[Tuple[int, int], str]

@dataclass
class ItDoesnt:
    a: dict[tuple[int, int], str]

@given(st.from_type(ItWorks))
def test_works(x: ItWorks):
    assert len(x.a) >=0


@given(st.from_type(ItDoesnt))
def test_doesnt_work(x: ItDoesnt):
    assert len(x.a) >=0 

The difference between classes ItWorks and ItDoesnt is that the latter uses tuple instead of typing.Tuple. The first test passes, the second one produces the exception:

_______________________________________________ test_doesnt_work ______________________________________________

    @given(st.from_type(ItDoesnt))
>   def test_doesnt_work(x: ItDoesnt):

tests/test_demo.py:19: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
.venv/lib/python3.10/site-packages/hypothesis/core.py:568: in process_arguments_to_given
    search_strategy.validate()
.venv/lib/python3.10/site-packages/hypothesis/strategies/_internal/strategies.py:417: in validate
    self.do_validate()
.venv/lib/python3.10/site-packages/hypothesis/strategies/_internal/collections.py:43: in do_validate
    s.validate()
.venv/lib/python3.10/site-packages/hypothesis/strategies/_internal/strategies.py:417: in validate
    self.do_validate()
.venv/lib/python3.10/site-packages/hypothesis/strategies/_internal/strategies.py:821: in do_validate
    self.mapped_strategy.validate()
.venv/lib/python3.10/site-packages/hypothesis/strategies/_internal/strategies.py:417: in validate
    self.do_validate()
.venv/lib/python3.10/site-packages/hypothesis/strategies/_internal/lazy.py:131: in do_validate
    w.validate()
.venv/lib/python3.10/site-packages/hypothesis/strategies/_internal/strategies.py:417: in validate
    self.do_validate()
.venv/lib/python3.10/site-packages/hypothesis/strategies/_internal/strategies.py:821: in do_validate
    self.mapped_strategy.validate()
.venv/lib/python3.10/site-packages/hypothesis/strategies/_internal/strategies.py:417: in validate
    self.do_validate()
.venv/lib/python3.10/site-packages/hypothesis/strategies/_internal/collections.py:43: in do_validate
    s.validate()
.venv/lib/python3.10/site-packages/hypothesis/strategies/_internal/strategies.py:417: in validate
    self.do_validate()
.venv/lib/python3.10/site-packages/hypothesis/strategies/_internal/lazy.py:131: in do_validate
    w.validate()
.venv/lib/python3.10/site-packages/hypothesis/strategies/_internal/core.py:871: in validate
    fixed_dictionaries(self.kwargs).validate()
.venv/lib/python3.10/site-packages/hypothesis/strategies/_internal/strategies.py:417: in validate
    self.do_validate()
.venv/lib/python3.10/site-packages/hypothesis/strategies/_internal/lazy.py:131: in do_validate
    w.validate()
.venv/lib/python3.10/site-packages/hypothesis/strategies/_internal/strategies.py:417: in validate
    self.do_validate()
.venv/lib/python3.10/site-packages/hypothesis/strategies/_internal/strategies.py:821: in do_validate
    self.mapped_strategy.validate()
.venv/lib/python3.10/site-packages/hypothesis/strategies/_internal/strategies.py:417: in validate
    self.do_validate()
.venv/lib/python3.10/site-packages/hypothesis/strategies/_internal/collections.py:43: in do_validate
    s.validate()
.venv/lib/python3.10/site-packages/hypothesis/strategies/_internal/strategies.py:417: in validate
    self.do_validate()
.venv/lib/python3.10/site-packages/hypothesis/strategies/_internal/lazy.py:131: in do_validate
    w.validate()
.venv/lib/python3.10/site-packages/hypothesis/strategies/_internal/strategies.py:418: in validate
    self.is_empty
.venv/lib/python3.10/site-packages/hypothesis/strategies/_internal/strategies.py:136: in accept
    recur(self)
.venv/lib/python3.10/site-packages/hypothesis/strategies/_internal/strategies.py:132: in recur
    mapping[strat] = getattr(strat, calculation)(recur)
.venv/lib/python3.10/site-packages/hypothesis/strategies/_internal/deferred.py:65: in calc_is_empty
    return recur(self.wrapped_strategy)
.venv/lib/python3.10/site-packages/hypothesis/strategies/_internal/deferred.py:35: in wrapped_strategy
    result = self.__definition()
.venv/lib/python3.10/site-packages/hypothesis/strategies/_internal/core.py:1047: in <lambda>
    lambda thing: deferred(lambda: _from_type(thing)),
.venv/lib/python3.10/site-packages/hypothesis/strategies/_internal/core.py:1167: in _from_type
    return types.from_typing_type(thing)
.venv/lib/python3.10/site-packages/hypothesis/strategies/_internal/types.py:425: in from_typing_type
    if not any(
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
.0 = <list_iterator object at 0x7f017187cfd0>

    if not any(
>       isinstance(T, type) and issubclass(int, T)
        for T in list(union_elems) + [elem_type]
    ):
E   TypeError: issubclass() argument 2 cannot be a parameterized generic

.venv/lib/python3.10/site-packages/hypothesis/strategies/_internal/types.py:426: TypeError

Since it is now recommended to use directly the non-typing generic vs the typing.Tuple type, this is a problem.

Note. This happens apparently only when the tuple is used as key in a dict.

@Zac-HD Zac-HD added the bug something is clearly wrong here label Dec 9, 2022
@nicksspirit
Copy link
Contributor

Alright I got this!!

nicksspirit added a commit to nicksspirit/hypothesis that referenced this issue Apr 24, 2023
@Zac-HD
Copy link
Member

Zac-HD commented Jun 11, 2023

Both work on hypothesis version 6.77.0 (Python 3.10.11), so I'm assuming that this has been fixed and closing. If you can reproduce, please let us know and we'll re-open the issue!

@Zac-HD Zac-HD closed this as completed Jun 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug something is clearly wrong here
Projects
None yet
Development

No branches or pull requests

3 participants