Skip to content

Commit 057d4de

Browse files
committed
make_strategies_namespace() repr omits api_version when inferred
1 parent 2a884fa commit 057d4de

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

hypothesis-python/src/hypothesis/extra/array_api.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -898,12 +898,6 @@ def make_strategies_namespace(
898898
"you're using the latest version of Hypothesis, then open an issue if "
899899
"one doesn't already exist."
900900
)
901-
check_argument(
902-
api_version is None
903-
or (isinstance(api_version, str) and api_version in NOMINAL_VERSIONS),
904-
f"{api_version=}, but api_version must be None, or a valid version "
905-
f"string {RELEASED_VERSIONS}. {not_available_msg}",
906-
)
907901
if api_version is None:
908902
check_argument(
909903
hasattr(xp, "__array_api_version__"),
@@ -919,6 +913,14 @@ def make_strategies_namespace(
919913
f"be a valid version string {RELEASED_VERSIONS}. {not_available_msg}",
920914
)
921915
api_version = xp.__array_api_version__
916+
inferred_version = True
917+
else:
918+
check_argument(
919+
isinstance(api_version, str) and api_version in NOMINAL_VERSIONS,
920+
f"{api_version=}, but api_version must be None, or a valid version "
921+
f"string {RELEASED_VERSIONS}. {not_available_msg}",
922+
)
923+
inferred_version = False
922924
try:
923925
array = xp.zeros(1)
924926
array.__array_namespace__()
@@ -1037,10 +1039,10 @@ def complex_dtypes(self):
10371039
) from e
10381040

10391041
def __repr__(self):
1040-
return (
1041-
f"make_strategies_namespace("
1042-
f"{self.name}, api_version='{self.api_version}')"
1043-
)
1042+
f_args = self.name
1043+
if not inferred_version:
1044+
f_args += f", api_version='{self.api_version}'"
1045+
return f"make_strategies_namespace({f_args})"
10441046

10451047
kwargs = dict(
10461048
name=xp.__name__,

hypothesis-python/tests/array_api/test_pretty.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212

1313
import pytest
1414

15+
from hypothesis.errors import InvalidArgument
16+
from hypothesis.extra.array_api import make_strategies_namespace
17+
1518
from tests.array_api.common import MIN_VER_FOR_COMPLEX
1619

1720

@@ -80,10 +83,22 @@ def test_namespaced_strategies_repr(xp, xps, name, valid_args):
8083
assert xp.__name__ not in repr(strat), f"{xp.__name__} in strat repr"
8184

8285

83-
def test_strategies_namespace_repr(xp, xps):
84-
"""Strategies namespace has good repr."""
85-
expected = (
86-
f"make_strategies_namespace({xp.__name__}, api_version='{xps.api_version}')"
87-
)
86+
@pytest.mark.filterwarnings("ignore::hypothesis.errors.HypothesisWarning")
87+
def test_inferred_version_strategies_namespace_repr(xp):
88+
"""Strategies namespace has good repr when api_version=None."""
89+
try:
90+
xps = make_strategies_namespace(xp)
91+
except InvalidArgument as e:
92+
pytest.skip(str(e))
93+
expected = f"make_strategies_namespace({xp.__name__})"
94+
assert repr(xps) == expected
95+
assert str(xps) == expected
96+
97+
98+
@pytest.mark.filterwarnings("ignore::hypothesis.errors.HypothesisWarning")
99+
def test_specified_version_strategies_namespace_repr(xp):
100+
"""Strategies namespace has good repr when api_version is specified."""
101+
xps = make_strategies_namespace(xp, api_version="2021.12")
102+
expected = f"make_strategies_namespace({xp.__name__}, api_version='2021.12')"
88103
assert repr(xps) == expected
89104
assert str(xps) == expected

0 commit comments

Comments
 (0)