Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

extra.array_api versioning + complex support #3456

Merged
merged 62 commits into from
Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
b51512f
Rudimentary xps versioning and complex support
honno Sep 6, 2022
e08ce5a
Fix argument validation for complex dtypes
honno Sep 12, 2022
8b6acaf
Fix `check_set_value()` for complex numbers with inf components
honno Sep 12, 2022
19d88a4
Use check functions for `api_version` validation
honno Sep 12, 2022
94e0257
Fix and test `api_version` inferrence
honno Sep 13, 2022
023d901
Parametrize `xp` with most recent supporting `api_version` in tests
honno Sep 13, 2022
a7a2ed0
Raise helpful error when inferring `api_version` when no `zeros()`
honno Sep 14, 2022
18d1c70
More explicit `api_version` arg validation
honno Sep 14, 2022
1684a15
Remove unnecessary `tuple()`
honno Sep 14, 2022
a170115
Fix `test_warns_on_specifying_unsupported_version`
honno Sep 14, 2022
90662cf
Delete redundant test file
honno Sep 14, 2022
387aa60
Remove erroneous and otherwise unused `complex128` var
honno Sep 15, 2022
858939d
Skip Array API tests requiring greater `api_version`
honno Sep 16, 2022
4784e05
Change `test_can_generate_{scalar/numeric}_dtypes` on runtime
honno Sep 16, 2022
309a049
Fix `test_strategeis_namespace_repr`
honno Sep 16, 2022
0d34426
Pass `api_version` to `find_castable_builtin_for_dtype()`
honno Sep 16, 2022
c9018d0
Spelling fix for `api_vers{i}on_gt()`
honno Sep 16, 2022
2189374
Change `test_draw_arrays_from_dtype_name_strategies` on runtime
honno Sep 16, 2022
4106eb8
Fix not-actually-literally `Literal` uses
honno Sep 16, 2022
cf2d96a
Remove use of value-bound kwargs for `xps.from_dtype()` on complex
honno Sep 16, 2022
b35de45
Improve error message for invalid `api_version`
honno Sep 16, 2022
dcb426e
Parametrize `xp` for `test_make_namespace_raises_invalid_argument`
honno Sep 16, 2022
bf020f4
Cache `make_strategies_namespace()` with a `WeakValueDictionary()`
honno Sep 19, 2022
5cad500
`test/array_api/conftest.py` changes
honno Sep 19, 2022
7af2d8d
Env for specifying `api_version` in `tests/array_api/
honno Sep 19, 2022
df88bf4
Hold meta attributes in strategies namespace and validate for them
honno Sep 19, 2022
cbd157f
`xps.complex_dtypes` raises when `api_version="2021.12"`
honno Sep 19, 2022
47a823f
Descriptive comment and error for complex branch in `xps.from_dtype()`
honno Sep 19, 2022
a19b8dc
Prettier `mock_xps` definition
honno Sep 19, 2022
2f8042e
Minor doc fixes
honno Sep 20, 2022
bcfe278
Use kw-only args for `array_api.ArrayStrategy`
honno Sep 20, 2022
3156ccb
Use `contextlib.suppress()` for `api_version` inferrence
honno Sep 20, 2022
b3ce0e2
`None` replaces `...` for `api_version`
honno Sep 20, 2022
4bd1dfa
Note on future `api_version` test parametrization in readme
honno Sep 20, 2022
68593b5
`MIN_VER_FOR_COMPLEX` constant for test suite
honno Sep 20, 2022
751edc3
`find_any()` testing and refactoring for `test_scalar_dtypes.py`
honno Sep 20, 2022
5a67c58
Test case for sizes-as-int in `xps.complex_dtypes()`
honno Sep 20, 2022
7224514
docs: update `xps` hack in docs, list `xps.real_dtypes()`
honno Sep 20, 2022
ecb8057
Update `make_strategies_namespace()` docstring
honno Sep 20, 2022
cdfdbd6
`tests/array_api/` docstrings and re-org
honno Sep 20, 2022
b87ad8d
Mark params with complex in `test_strategy_can_generate_every_dtype`
honno Sep 20, 2022
0f88cdf
Use `__array_api_version__` for version inferrence
honno Sep 22, 2022
edd8f87
Handle modules lacking `__array_api_version__` in `tests/array_api/`
honno Sep 22, 2022
53fcd6f
Just use the mock for `tests/array_api/` by default
honno Sep 22, 2022
97e516f
`conftest.py`: Fix entrypoint logic, use `else` in a `try-except`
honno Sep 22, 2022
87b643b
Note decisions in complex branch of `array_api._from_dtype()`
honno Sep 23, 2022
1c22c6c
Add `RELEASE.rst`
honno Sep 23, 2022
17b1a76
Clarify "real-valued" for `xps.floating_dtypes()` docstring
honno Sep 23, 2022
bcf2117
Lint fix for raised error inside an except
honno Sep 23, 2022
56a59b4
Fix doc warning on commenting inside directive
honno Sep 23, 2022
1ee6b45
mypy fixes and ignores
honno Sep 23, 2022
8d4386d
Remove a misleading and an unnecessary comment
honno Sep 26, 2022
e7cee0c
Explain the why in `RELEASE.rst`
honno Sep 27, 2022
556aba4
Remove redundant `api_version_gt()` util
honno Sep 27, 2022
89ee94b
Move ini registration of `xp_min_version()` to top-level conftest
honno Sep 27, 2022
d0c353c
Clearer `xp_min_version()` logic in modifyitems hook
honno Sep 27, 2022
a5dd305
Revert erroneous docstring change
honno Sep 27, 2022
927b80a
Include `xp` name in `complex_dtypes()` error
honno Sep 27, 2022
2a884fa
Sanity check for `MIN_VER_FOR_COMPLEX`
honno Sep 27, 2022
057d4de
`make_strategies_namespace()` repr omits `api_version` when inferred
honno Sep 27, 2022
bc4bcb6
Distinct cache keys when `api_version=None`
honno Sep 28, 2022
0e5155d
Inferred `api_version` shares cache key with specified versions
honno Sep 29, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion hypothesis-python/src/hypothesis/extra/array_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1076,7 +1076,7 @@ def complex_dtypes(

namespace = StrategiesNamespace(**kwargs)
try:
_args_to_xps[(xp, api_version)] = namespace
_args_to_xps[(xp, None if inferred_version else api_version)] = namespace
except TypeError:
pass

Expand Down
53 changes: 43 additions & 10 deletions hypothesis-python/tests/array_api/test_strategies_namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,32 @@
)
from hypothesis.strategies import SearchStrategy

pytestmark = pytest.mark.filterwarnings("ignore::hypothesis.errors.HypothesisWarning")

@pytest.mark.filterwarnings("ignore::hypothesis.errors.HypothesisWarning")
def test_caching(xp, monkeypatch):

class HashableArrayModuleFactory:
"""
mock_xp cannot be hashed and thus cannot be used in our cache. So just for
the purposes of testing the cache, we wrap it with an unsafe hash method.
"""

def __getattr__(self, name):
return getattr(mock_xp, name)

def __hash__(self):
return hash(tuple(sorted(mock_xp.__dict__)))


@pytest.mark.parametrize("api_version", ["2021.12", None])
def test_caching(api_version, monkeypatch):
"""Caches namespaces respective to arguments."""
try:
hash(xp)
except TypeError:
pytest.skip("xp not hashable")
assert isinstance(array_api._args_to_xps, WeakValueDictionary)
xp = HashableArrayModuleFactory()
assert isinstance(array_api._args_to_xps, WeakValueDictionary) # sanity check
monkeypatch.setattr(array_api, "_args_to_xps", WeakValueDictionary())
assert len(array_api._args_to_xps) == 0 # sanity check
xps1 = array_api.make_strategies_namespace(xp, api_version="2021.12")
xps1 = array_api.make_strategies_namespace(xp, api_version=api_version)
assert len(array_api._args_to_xps) == 1
xps2 = array_api.make_strategies_namespace(xp, api_version="2021.12")
xps2 = array_api.make_strategies_namespace(xp, api_version=api_version)
assert len(array_api._args_to_xps) == 1
assert isinstance(xps2, SimpleNamespace)
assert xps2 is xps1
Expand All @@ -43,7 +55,28 @@ def test_caching(xp, monkeypatch):
assert len(array_api._args_to_xps) == 0


@pytest.mark.filterwarnings("ignore::hypothesis.errors.HypothesisWarning")
@pytest.mark.parametrize(
"api_version1, api_version2", [(None, "2021.12"), ("2021.12", None)]
)
def test_inferred_namespace_is_cached_seperately(
api_version1, api_version2, monkeypatch
):
"""Results from inferred versions do not share the same cache key as results
from specified versions."""
Zac-HD marked this conversation as resolved.
Show resolved Hide resolved
xp = HashableArrayModuleFactory()
xp.__array_api_version__ = "2021.12"
assert isinstance(array_api._args_to_xps, WeakValueDictionary) # sanity check
monkeypatch.setattr(array_api, "_args_to_xps", WeakValueDictionary())
assert len(array_api._args_to_xps) == 0 # sanity check
xps1 = array_api.make_strategies_namespace(xp, api_version=api_version1)
assert xps1.api_version == "2021.12" # sanity check
assert len(array_api._args_to_xps) == 1
xps2 = array_api.make_strategies_namespace(xp, api_version=api_version2)
assert xps2.api_version == "2021.12" # sanity check
assert len(array_api._args_to_xps) == 2
assert xps2 is not xps1


def test_complex_dtypes_raises_on_2021_12():
"""Accessing complex_dtypes() for 2021.12 strategy namespace raises helpful
error, but accessing on future versions returns expected strategy."""
Expand Down