Blob Blame History Raw
From 2484942b4f9ff63e6c3c0dae2097a266622fc87d Mon Sep 17 00:00:00 2001
From: Zac Hatfield-Dodds <zac.hatfield.dodds@gmail.com>
Date: Wed, 22 Jun 2022 22:41:38 -0700
Subject: [PATCH] Support 3.11 GenericAlias again

---
 hypothesis-python/RELEASE.rst                       |  4 ++++
 .../src/hypothesis/strategies/_internal/types.py    | 13 +++++--------
 2 files changed, 9 insertions(+), 8 deletions(-)
 create mode 100644 hypothesis-python/RELEASE.rst

diff --git a/hypothesis-python/RELEASE.rst b/hypothesis-python/RELEASE.rst
new file mode 100644
index 0000000000..6b49da62d3
--- /dev/null
+++ b/hypothesis-python/RELEASE.rst
@@ -0,0 +1,4 @@
+RELEASE_TYPE: patch
+
+This patch fixes :func:`~hypothesis.strategies.from_type` on Python 3.11,
+following `python/cpython#93754 <https://github.com/python/cpython/pull/93754/>`__.
diff --git a/hypothesis-python/src/hypothesis/strategies/_internal/types.py b/hypothesis-python/src/hypothesis/strategies/_internal/types.py
index 617b360856..4e410bcf58 100644
--- a/hypothesis-python/src/hypothesis/strategies/_internal/types.py
+++ b/hypothesis-python/src/hypothesis/strategies/_internal/types.py
@@ -39,11 +39,13 @@
 from hypothesis.strategies._internal.lazy import unwrap_strategies
 from hypothesis.strategies._internal.strategies import OneOfStrategy
 
+GenericAlias: typing.Any
 UnionType: typing.Any
 try:
     # The type of PEP-604 unions (`int | str`), added in Python 3.10
-    from types import UnionType
+    from types import GenericAlias, UnionType
 except ImportError:
+    GenericAlias = ()
     UnionType = ()
 
 try:
@@ -51,11 +53,6 @@
 except ImportError:
     typing_extensions = None  # type: ignore
 
-try:
-    from typing import _GenericAlias  # type: ignore  # python >= 3.7
-except ImportError:
-    _GenericAlias = ()
-
 try:
     from typing import _AnnotatedAlias  # type: ignore
 except ImportError:
@@ -298,7 +295,7 @@ def find_annotated_strategy(annotated_type):  # pragma: no cover
 def has_type_arguments(type_):
     """Decides whethere or not this type has applied type arguments."""
     args = getattr(type_, "__args__", None)
-    if args and isinstance(type_, _GenericAlias):
+    if args and isinstance(type_, (typing._GenericAlias, GenericAlias)):
         # There are some cases when declared types do already have type arguments
         # Like `Sequence`, that is `_GenericAlias(abc.Sequence[T])[T]`
         parameters = getattr(type_, "__parameters__", None)
@@ -312,7 +309,7 @@ def is_generic_type(type_):
     # The ugly truth is that `MyClass`, `MyClass[T]`, and `MyClass[int]` are very different.
     # We check for `MyClass[T]` and `MyClass[int]` with the first condition,
     # while the second condition is for `MyClass`.
-    return isinstance(type_, typing_root_type) or (
+    return isinstance(type_, typing_root_type + (GenericAlias,)) or (
         isinstance(type_, type) and typing.Generic in type_.__mro__
     )