Skip to content

Commit

Permalink
compat with new Pythons
Browse files Browse the repository at this point in the history
  • Loading branch information
Zac-HD committed May 20, 2024
1 parent debc928 commit 3409525
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ jobs:
- check-py313-cover
- check-py313-nocover
- check-py313-niche
# - check-py314-cover
# - check-py314-nocover
# - check-py314-niche
- check-quality
## Skip all the (inactive/old) Rust and Ruby tests pending fixes
# - lint-ruby
Expand Down
4 changes: 4 additions & 0 deletions hypothesis-python/RELEASE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
RELEASE_TYPE: patch

This patch fixes some introspection errors new in Python 3.11.9 and
3.13.0b1, for the Ghostwriter and :func:`~hypothesis.strategies.from_type`.
4 changes: 2 additions & 2 deletions hypothesis-python/src/hypothesis/extra/ghostwriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -780,8 +780,8 @@ def _write_call(
"""
args = ", ".join(
(
(v or p.name)
if p.kind is inspect.Parameter.POSITIONAL_ONLY
(v if (v or p is None) else p.name)
if p is None or p.kind is inspect.Parameter.POSITIONAL_ONLY
else f"{p.name}={v or p.name}"
)
for v, p in zip_longest(pass_variables, _get_params(func).values())
Expand Down
16 changes: 13 additions & 3 deletions hypothesis-python/src/hypothesis/strategies/_internal/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,12 @@ def is_generic_type(type_):
)


def _try_import_forward_ref(thing, bound): # pragma: no cover
__EVAL_TYPE_TAKES_TYPE_PARAMS = (
"type_params" in inspect.signature(typing._eval_type).parameters # type: ignore
)


def _try_import_forward_ref(thing, bound, *, type_params): # pragma: no cover
"""
Tries to import a real bound type from ``TypeVar`` bound to a ``ForwardRef``.
Expand All @@ -397,7 +402,10 @@ def _try_import_forward_ref(thing, bound): # pragma: no cover
because we can only cover each path in a separate python version.
"""
try:
return typing._eval_type(bound, vars(sys.modules[thing.__module__]), None)
kw = {"globalns": vars(sys.modules[thing.__module__]), "localns": None}
if __EVAL_TYPE_TAKES_TYPE_PARAMS:
kw["type_params"] = type_params
return typing._eval_type(bound, **kw)
except (KeyError, AttributeError, NameError):
# We fallback to `ForwardRef` instance, you can register it as a type as well:
# >>> from typing import ForwardRef
Expand Down Expand Up @@ -1030,7 +1038,9 @@ def resolve_TypeVar(thing):
if getattr(thing, "__bound__", None) is not None:
bound = thing.__bound__
if isinstance(bound, typing.ForwardRef):
bound = _try_import_forward_ref(thing, bound)
# TODO: on Python 3.13 and later, we should work out what type_params
# could be part of this type, and pass them in here.
bound = _try_import_forward_ref(thing, bound, type_params=())
strat = unwrap_strategies(st.from_type(bound))
if not isinstance(strat, OneOfStrategy):
return strat
Expand Down
11 changes: 11 additions & 0 deletions hypothesis-python/tests/typing_extensions/test_backported_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# obtain one at https://mozilla.org/MPL/2.0/.

import collections
import sys
from typing import Callable, DefaultDict, Dict, List, NewType, Type, Union

import pytest
Expand Down Expand Up @@ -169,6 +170,11 @@ def test_non_runtime_type_cannot_be_registered(non_runtime_type):
st.register_type_strategy(non_runtime_type, st.none())


@pytest.mark.xfail(
sys.version_info == (3, 13, 0, "beta", 1),
reason="upstream issue",
raises=AttributeError,
)
def test_callable_with_concatenate():
P = ParamSpec("P")
func_type = Callable[Concatenate[int, P], None]
Expand All @@ -183,6 +189,11 @@ def test_callable_with_concatenate():
st.register_type_strategy(func_type, st.none())


@pytest.mark.xfail(
sys.version_info == (3, 13, 0, "beta", 1),
reason="upstream issue",
raises=AttributeError,
)
def test_callable_with_paramspec():
P = ParamSpec("P")
func_type = Callable[P, None]
Expand Down
2 changes: 1 addition & 1 deletion requirements/coverage.txt
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ tomli==2.0.1
# black
# coverage
# pytest
typing-extensions==4.11.0
typing-extensions==4.12.0rc1
# via
# -r requirements/coverage.in
# black
Expand Down
2 changes: 1 addition & 1 deletion requirements/fuzzing.txt
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ tomli==2.0.1
# black
# coverage
# pytest
typing-extensions==4.11.0
typing-extensions==4.12.0rc1
# via
# -r requirements/coverage.in
# black
Expand Down
2 changes: 1 addition & 1 deletion requirements/tools.txt
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ types-redis==4.6.0.20240425
# via -r requirements/tools.in
types-setuptools==69.5.0.20240518
# via types-cffi
typing-extensions==4.11.0
typing-extensions==4.12.0rc1
# via
# -r requirements/tools.in
# anyio
Expand Down

0 comments on commit 3409525

Please sign in to comment.