Repository: HypothesisWorks/hypothesis

Issue:
Test failure (again) with Python 3.9.0a6
A continuation of #2348 -- there are 3 type errors uncovered by 4 tests (excerpted below). I'm trying to read through the codebase but someone familiar with it can probably figure out what's causing this faster.

ref: https://docs.python.org/3.9/whatsnew/3.9.html

full bug report: https://bugzilla.redhat.com/show_bug.cgi?id=1830976

```
__________ test_specialised_collection_types[A_NamedTuple-tuple-int] ___________
[gw1] linux -- Python 3.9.0 /usr/bin/python3
Traceback (most recent call last):
  File "/builddir/build/BUILD/hypothesis-hypothesis-python-5.10.4/hypothesis-python/tests/cover/test_lookup.py", line 131, in test_specialised_collection_types
...
  File "/builddir/build/BUILDROOT/python-hypothesis-5.10.4-1.fc33.x86_64/usr/lib/python3.9/site-packages/hypothesis/internal/conjecture/data.py", line 888, in draw
    return strategy.do_draw(self)
  File "/builddir/build/BUILDROOT/python-hypothesis-5.10.4-1.fc33.x86_64/usr/lib/python3.9/site-packages/hypothesis/strategies/_internal/strategies.py", line 658, in do_draw
    result = self.pack(data.draw(self.mapped_strategy))
  File "/builddir/build/BUILDROOT/python-hypothesis-5.10.4-1.fc33.x86_64/usr/lib/python3.9/site-packages/hypothesis/strategies/_internal/core.py", line 1241, in <lambda>
    lambda value: target(*value[0], **value[1])  # type: ignore
TypeError: __new__() missing 1 required positional argument: 'elem'
```

```
____________________ test_infers_args_for_namedtuple_builds ____________________
[gw1] linux -- Python 3.9.0 /usr/bin/python3
Traceback (most recent call last):
  File "/builddir/build/BUILD/hypothesis-hypothesis-python-5.10.4/hypothesis-python/tests/cover/test_lookup.py", line 453, in test_infers_args_for_namedtuple_builds
...
  File "/builddir/build/BUILDROOT/python-hypothesis-5.10.4-1.fc33.x86_64/usr/lib/python3.9/site-packages/hypothesis/internal/conjecture/data.py", line 888, in draw
    return strategy.do_draw(self)
  File "/builddir/build/BUILDROOT/python-hypothesis-5.10.4-1.fc33.x86_64/usr/lib/python3.9/site-packages/hypothesis/strategies/_internal/strategies.py", line 658, in do_draw
    result = self.pack(data.draw(self.mapped_strategy))
  File "/builddir/build/BUILDROOT/python-hypothesis-5.10.4-1.fc33.x86_64/usr/lib/python3.9/site-packages/hypothesis/strategies/_internal/core.py", line 1241, in <lambda>
    lambda value: target(*value[0], **value[1])  # type: ignore
TypeError: __new__() missing 1 required positional argument: 'a'
```

```
_________________ test_infers_args_for_namedtuple_from_type ___________________
[gw1] linux -- Python 3.9.0 /usr/bin/python3
Traceback (most recent call last):
  File "/builddir/build/BUILD/hypothesis-hypothesis-python-5.10.4/hypothesis-python/tests/cover/test_lookup.py", line 458, in test_infers_args_for_namedtuple_from_type
...
  File "/builddir/build/BUILDROOT/python-hypothesis-5.10.4-1.fc33.x86_64/usr/lib/python3.9/site-packages/hypothesis/internal/conjecture/data.py", line 888, in draw
    return strategy.do_draw(self)
  File "/builddir/build/BUILDROOT/python-hypothesis-5.10.4-1.fc33.x86_64/usr/lib/python3.9/site-packages/hypothesis/strategies/_internal/strategies.py", line 658, in do_draw
    result = self.pack(data.draw(self.mapped_strategy))
  File "/builddir/build/BUILDROOT/python-hypothesis-5.10.4-1.fc33.x86_64/usr/lib/python3.9/site-packages/hypothesis/strategies/_internal/core.py", line 1241, in <lambda>
    lambda value: target(*value[0], **value[1])  # type: ignore
TypeError: __new__() missing 1 required positional argument: 'a'
```

```
_____________ test_resolves_forward_references_outside_annotations _____________
[gw1] linux -- Python 3.9.0 /usr/bin/python3
Traceback (most recent call last):
  File "/builddir/build/BUILD/hypothesis-hypothesis-python-5.10.4/hypothesis-python/tests/cover/test_lookup_py36.py", line 31, in test_resolves_forward_references_outside_annotations
...
  File "/builddir/build/BUILDROOT/python-hypothesis-5.10.4-1.fc33.x86_64/usr/lib/python3.9/site-packages/hypothesis/internal/conjecture/data.py", line 888, in draw
    return strategy.do_draw(self)
  File "/builddir/build/BUILDROOT/python-hypothesis-5.10.4-1.fc33.x86_64/usr/lib/python3.9/site-packages/hypothesis/strategies/_internal/strategies.py", line 658, in do_draw
    result = self.pack(data.draw(self.mapped_strategy))
  File "/builddir/build/BUILDROOT/python-hypothesis-5.10.4-1.fc33.x86_64/usr/lib/python3.9/site-packages/hypothesis/strategies/_internal/core.py", line 1241, in <lambda>
    lambda value: target(*value[0], **value[1])  # type: ignore
TypeError: __new__() missing 3 required positional arguments: 'val', 'l', and 'r'
```
```


Discussion:
Likely related to this change somehow: __field_types removed in favor of __annotations__:
https://bugs.python.org/issue40182

... but hypothesis is still testing for it:
https://github.com/HypothesisWorks/hypothesis/blob/master/hypothesis-python/src/hypothesis/internal/compat.py#L95

Resolving patch:
diff --git a/hypothesis-python/src/hypothesis/internal/compat.py b/hypothesis-python/src/hypothesis/internal/compat.py
--- a/hypothesis-python/src/hypothesis/internal/compat.py
+++ b/hypothesis-python/src/hypothesis/internal/compat.py
@@ -92,7 +92,7 @@ def is_typed_named_tuple(cls):
     return (
         issubclass(cls, tuple)
         and hasattr(cls, "_fields")
-        and hasattr(cls, "_field_types")
+        and (hasattr(cls, "_field_types") or hasattr(cls, "__annotations__"))
     )
 
 


Test patch: