Skip to content

Commit dfd0b2f

Browse files
committed
Fix for updated NewType
1 parent 3a386e2 commit dfd0b2f

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

hypothesis-python/RELEASE.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
RELEASE_TYPE: patch
2+
3+
This patch fixes :func:`~hypothesis.strategies.from_type` and
4+
:func:`~hypothesis.strategies.register_type_strategy` for
5+
:obj:`python:typing.NewType` on Python 3.10, which changed the
6+
underlying implementation (see :bpo:`44353` for details).

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,17 @@ def try_issubclass(thing, superclass):
9898

9999

100100
def is_a_new_type(thing):
101-
# At runtime, `typing.NewType` returns an identity function rather
102-
# than an actual type, but we can check whether that thing matches.
103-
return (
104-
hasattr(thing, "__supertype__")
105-
and getattr(thing, "__module__", None) in ("typing", "typing_extensions")
106-
and inspect.isfunction(thing)
107-
)
101+
if sys.version_info[:2] < (3, 10):
102+
# At runtime, `typing.NewType` returns an identity function rather
103+
# than an actual type, but we can check whether that thing matches.
104+
return (
105+
hasattr(thing, "__supertype__")
106+
and getattr(thing, "__module__", None) in ("typing", "typing_extensions")
107+
and inspect.isfunction(thing)
108+
)
109+
# In 3.10 and later, NewType is actually a class - which simplifies things.
110+
# See https://bugs.python.org/issue44353 for links to the various patches.
111+
return isinstance(thing, typing.NewType) # pragma: no cover # on 3.8, anyway
108112

109113

110114
def is_a_type(thing):

0 commit comments

Comments
 (0)