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

Fails on typing.Type and ForwardRef #2565

Closed
sobolevn opened this issue Aug 26, 2020 · 4 comments · Fixed by #2892
Closed

Fails on typing.Type and ForwardRef #2565

sobolevn opened this issue Aug 26, 2020 · 4 comments · Fixed by #2892
Labels
enhancement it's not broken, but we want it to be better

Comments

@sobolevn
Copy link
Member

sobolevn commented Aug 26, 2020

When we use Type[MyType] - everything is good. But, Type['MyType'] fails.

Code:

>>> from hypothesis import strategies as st
>>> from typing import Type
>>> st.from_type(Type[int])
just(<class 'int'>)
>>> st.from_type(Type['int'])
from_type(typing.Type[ForwardRef('int')])
>>> st.from_type(Type['int']).example()

Output:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "hypothesis/strategies/_internal/strategies.py", line 314, in example
    example_generating_inner_function()
  File "hypothesis/strategies/_internal/strategies.py", line 302, in example_generating_inner_function
    @settings(
  File "hypothesis/core.py", line 1001, in wrapped_test
    processed_args = process_arguments_to_given(
  File "hypothesis/core.py", line 420, in process_arguments_to_given
    search_strategy.validate()
  File "hypothesis/strategies/_internal/strategies.py", line 377, in validate
    self.do_validate()
  File "hypothesis/strategies/_internal/collections.py", line 39, in do_validate
    s.validate()
  File "hypothesis/strategies/_internal/strategies.py", line 377, in validate
    self.do_validate()
  File "hypothesis/strategies/_internal/strategies.py", line 638, in do_validate
    self.mapped_strategy.validate()
  File "hypothesis/strategies/_internal/strategies.py", line 377, in validate
    self.do_validate()
  File "hypothesis/strategies/_internal/lazy.py", line 118, in do_validate
    w.validate()
  File "hypothesis/strategies/_internal/strategies.py", line 377, in validate
    self.do_validate()
  File "hypothesis/strategies/_internal/strategies.py", line 638, in do_validate
    self.mapped_strategy.validate()
  File "hypothesis/strategies/_internal/strategies.py", line 377, in validate
    self.do_validate()
  File "hypothesis/strategies/_internal/collections.py", line 39, in do_validate
    s.validate()
  File "hypothesis/strategies/_internal/strategies.py", line 377, in validate
    self.do_validate()
  File "hypothesis/strategies/_internal/lazy.py", line 118, in do_validate
    w.validate()
  File "hypothesis/strategies/_internal/strategies.py", line 378, in validate
    self.is_empty
  File "hypothesis/strategies/_internal/strategies.py", line 125, in accept
    recur(self)
  File "hypothesis/strategies/_internal/strategies.py", line 121, in recur
    mapping[strat] = getattr(strat, calculation)(recur)
  File "hypothesis/strategies/_internal/deferred.py", line 80, in calc_is_empty
    return recur(self.wrapped_strategy)
  File "hypothesis/strategies/_internal/deferred.py", line 43, in wrapped_strategy
    result = self.__definition()
  File "hypothesis/strategies/_internal/core.py", line 1389, in <lambda>
    lambda thing: deferred(lambda: _from_type(thing)),
  File "hypothesis/strategies/_internal/core.py", line 1477, in _from_type
    return types.from_typing_type(thing)
  File "hypothesis/strategies/_internal/types.py", line 282, in from_typing_type
    strategies = [
  File "hypothesis/strategies/_internal/types.py", line 283, in <listcomp>
    v if isinstance(v, st.SearchStrategy) else v(thing)
  File "hypothesis/strategies/_internal/types.py", line 507, in resolve_Type
    raise ResolutionFailed(
hypothesis.errors.ResolutionFailed: 
	thing=typing.Type[ForwardRef('int')] cannot be resolved.
	Upgrading to python>=3.6 may fix this problem via improvements to the typing module.
@Zac-HD Zac-HD added the enhancement it's not broken, but we want it to be better label Aug 27, 2020
@Zac-HD
Copy link
Member

Zac-HD commented Aug 27, 2020

Looks like _try_import_forward_ref() from #2550 could be useful here too! Are there other places ForwardRef can occur that we should be looking at?

@sobolevn
Copy link
Member Author

Pretty much any generic. Like List, Callable, etc
I agree that they should all use the same logic. 👍

@Zac-HD
Copy link
Member

Zac-HD commented Aug 27, 2020

Hmm. Sounds like we should be calling _try_import_forward_ref() pretty early in st.from_type(), then it would be shared across all users and we could raise ResolutionFailed instead of returning a ForwardRef if we can't resolve it.

@sobolevn
Copy link
Member Author

We can make it "safe" for this general use-case. Like "Let's try to resolve this, failed? Ok, no worries."

This was referenced Mar 10, 2021
This was referenced Mar 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement it's not broken, but we want it to be better
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants