Skip to content

Commit bddbc01

Browse files
committed
Update tests to expect forward ref resolution to succeed
With the new forward reference resolution feature (#4542), forward references like Optional["ConcreteFoo"] now resolve successfully when ConcreteFoo is in scope. Updated tests accordingly: - Split test_cannot_resolve_bare_forward_reference into two tests: - test_can_resolve_forward_reference_to_class for Optional/list/List - test_cannot_resolve_type_forward_reference for type/Type - Changed test_string_forward_ref_message to test_string_forward_ref_resolved since User with list["User"] now works on all Python versions
1 parent a29300f commit bddbc01

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

hypothesis-python/tests/cover/test_lookup.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -609,10 +609,19 @@ def test_override_args_for_namedtuple(thing):
609609
assert thing.a is None
610610

611611

612-
@pytest.mark.parametrize("thing", [typing.Optional, list, type, _List, _Type])
613-
def test_cannot_resolve_bare_forward_reference(thing):
612+
@pytest.mark.parametrize("thing", [typing.Optional, list, _List])
613+
def test_can_resolve_forward_reference_to_class(thing):
614+
# Forward references to classes in scope should now be resolved
615+
# See https://github.com/HypothesisWorks/hypothesis/issues/4542
614616
t = thing["ConcreteFoo"]
615-
with pytest.raises(InvalidArgument):
617+
check_can_generate_examples(st.from_type(t))
618+
619+
620+
@pytest.mark.parametrize("thing", [type, _Type])
621+
def test_cannot_resolve_type_forward_reference(thing):
622+
# Type[ForwardRef] still fails because it needs special handling
623+
t = thing["ConcreteFoo"]
624+
with pytest.raises(ResolutionFailed):
616625
check_can_generate_examples(st.from_type(t))
617626

618627

hypothesis-python/tests/cover/test_lookup_py39.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@
1010

1111
import collections.abc
1212
import dataclasses
13-
import sys
1413
import typing
1514

1615
import pytest
1716

1817
from hypothesis import given, strategies as st
19-
from hypothesis.errors import InvalidArgument
2018

2119
from tests.common.debug import (
2220
assert_all_examples,
@@ -87,12 +85,11 @@ class User:
8785
following: list["User"] # works with typing.List
8886

8987

90-
@pytest.mark.skipif(sys.version_info[:2] >= (3, 11), reason="works in new Pythons")
91-
def test_string_forward_ref_message():
92-
# See https://github.com/HypothesisWorks/hypothesis/issues/3016
88+
def test_string_forward_ref_resolved():
89+
# Forward references to types in scope now work
90+
# See https://github.com/HypothesisWorks/hypothesis/issues/4542
9391
s = st.builds(User)
94-
with pytest.raises(InvalidArgument, match="`from __future__ import annotations`"):
95-
check_can_generate_examples(s)
92+
check_can_generate_examples(s)
9693

9794

9895
@pytest.mark.parametrize("typ", (typing.Union[list[int], int], list[int] | int))

0 commit comments

Comments
 (0)