Skip to content

Commit e26da43

Browse files
authored
Merge pull request #3381 from Zac-HD/fix-py311-types
Support 3.11 GenericAlias again
2 parents dda81b3 + 2484942 commit e26da43

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

hypothesis-python/RELEASE.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
RELEASE_TYPE: patch
2+
3+
This patch fixes :func:`~hypothesis.strategies.from_type` on Python 3.11,
4+
following `python/cpython#93754 <https://github.com/python/cpython/pull/93754/>`__.

hypothesis-python/src/hypothesis/strategies/_internal/types.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,20 @@
3939
from hypothesis.strategies._internal.lazy import unwrap_strategies
4040
from hypothesis.strategies._internal.strategies import OneOfStrategy
4141

42+
GenericAlias: typing.Any
4243
UnionType: typing.Any
4344
try:
4445
# The type of PEP-604 unions (`int | str`), added in Python 3.10
45-
from types import UnionType
46+
from types import GenericAlias, UnionType
4647
except ImportError:
48+
GenericAlias = ()
4749
UnionType = ()
4850

4951
try:
5052
import typing_extensions
5153
except ImportError:
5254
typing_extensions = None # type: ignore
5355

54-
try:
55-
from typing import _GenericAlias # type: ignore # python >= 3.7
56-
except ImportError:
57-
_GenericAlias = ()
58-
5956
try:
6057
from typing import _AnnotatedAlias # type: ignore
6158
except ImportError:
@@ -298,7 +295,7 @@ def find_annotated_strategy(annotated_type): # pragma: no cover
298295
def has_type_arguments(type_):
299296
"""Decides whethere or not this type has applied type arguments."""
300297
args = getattr(type_, "__args__", None)
301-
if args and isinstance(type_, _GenericAlias):
298+
if args and isinstance(type_, (typing._GenericAlias, GenericAlias)):
302299
# There are some cases when declared types do already have type arguments
303300
# Like `Sequence`, that is `_GenericAlias(abc.Sequence[T])[T]`
304301
parameters = getattr(type_, "__parameters__", None)
@@ -312,7 +309,7 @@ def is_generic_type(type_):
312309
# The ugly truth is that `MyClass`, `MyClass[T]`, and `MyClass[int]` are very different.
313310
# We check for `MyClass[T]` and `MyClass[int]` with the first condition,
314311
# while the second condition is for `MyClass`.
315-
return isinstance(type_, typing_root_type) or (
312+
return isinstance(type_, typing_root_type + (GenericAlias,)) or (
316313
isinstance(type_, type) and typing.Generic in type_.__mro__
317314
)
318315

0 commit comments

Comments
 (0)