Skip to content

Commit

Permalink
Merge pull request #3911 from JonathanPlasse/improve-and-test-numpy-e…
Browse files Browse the repository at this point in the history
…xtra

Improve and test hypothesis.extra.numpy type annotations
  • Loading branch information
Zac-HD committed Mar 11, 2024
2 parents 57d5087 + 64da5d2 commit e640900
Show file tree
Hide file tree
Showing 18 changed files with 404 additions and 53 deletions.
3 changes: 3 additions & 0 deletions hypothesis-python/RELEASE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
RELEASE_TYPE: patch

This patch further improves the type annotations in :mod:`hypothesis.extra.numpy`.
186 changes: 186 additions & 0 deletions hypothesis-python/src/hypothesis/extra/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,11 +624,61 @@ def dtype_factory(kind, sizes, valid_sizes, endianness):
return strat.map((endianness + kind).format)


@overload
@defines_dtype_strategy
def unsigned_integer_dtypes(
*,
endianness: str = "?",
sizes: Literal[8],
) -> st.SearchStrategy["np.dtype[np.uint8]"]: ...


@overload
@defines_dtype_strategy
def unsigned_integer_dtypes(
*,
endianness: str = "?",
sizes: Literal[16],
) -> st.SearchStrategy["np.dtype[np.uint16]"]: ...


@overload
@defines_dtype_strategy
def unsigned_integer_dtypes(
*,
endianness: str = "?",
sizes: Literal[32],
) -> st.SearchStrategy["np.dtype[np.uint32]"]: ...


@overload
@defines_dtype_strategy
def unsigned_integer_dtypes(
*,
endianness: str = "?",
sizes: Literal[64],
) -> st.SearchStrategy["np.dtype[np.uint64]"]: ...


@overload
@defines_dtype_strategy
def unsigned_integer_dtypes(
*,
endianness: str = "?",
sizes: Sequence[Literal[8, 16, 32, 64]] = (8, 16, 32, 64),
) -> st.SearchStrategy["np.dtype[np.unsignedinteger[Any]]"]: ...


@defines_dtype_strategy
def unsigned_integer_dtypes(
*,
endianness: str = "?",
sizes: Union[Literal[8, 16, 32, 64], Sequence[Literal[8, 16, 32, 64]]] = (
8,
16,
32,
64,
),
) -> st.SearchStrategy["np.dtype[np.unsignedinteger[Any]]"]:
"""Return a strategy for unsigned integer dtypes.
Expand All @@ -642,11 +692,61 @@ def unsigned_integer_dtypes(
return dtype_factory("u", sizes, (8, 16, 32, 64), endianness)


@overload
@defines_dtype_strategy
def integer_dtypes(
*,
endianness: str = "?",
sizes: Literal[8],
) -> st.SearchStrategy["np.dtype[np.int8]"]: ...


@overload
@defines_dtype_strategy
def integer_dtypes(
*,
endianness: str = "?",
sizes: Literal[16],
) -> st.SearchStrategy["np.dtype[np.int16]"]: ...


@overload
@defines_dtype_strategy
def integer_dtypes(
*,
endianness: str = "?",
sizes: Literal[32],
) -> st.SearchStrategy["np.dtype[np.int32]"]: ...


@overload
@defines_dtype_strategy
def integer_dtypes(
*,
endianness: str = "?",
sizes: Literal[64],
) -> st.SearchStrategy["np.dtype[np.int64]"]: ...


@overload
@defines_dtype_strategy
def integer_dtypes(
*,
endianness: str = "?",
sizes: Sequence[Literal[8, 16, 32, 64]] = (8, 16, 32, 64),
) -> st.SearchStrategy["np.dtype[np.signedinteger[Any]]"]: ...


@defines_dtype_strategy
def integer_dtypes(
*,
endianness: str = "?",
sizes: Union[Literal[8, 16, 32, 64], Sequence[Literal[8, 16, 32, 64]]] = (
8,
16,
32,
64,
),
) -> st.SearchStrategy["np.dtype[np.signedinteger[Any]]"]:
"""Return a strategy for signed integer dtypes.
Expand All @@ -656,11 +756,58 @@ def integer_dtypes(
return dtype_factory("i", sizes, (8, 16, 32, 64), endianness)


@overload
@defines_dtype_strategy
def floating_dtypes(
*,
endianness: str = "?",
sizes: Literal[16],
) -> st.SearchStrategy["np.dtype[np.float16]"]: ...


@overload
@defines_dtype_strategy
def floating_dtypes(
*,
endianness: str = "?",
sizes: Literal[32],
) -> st.SearchStrategy["np.dtype[np.float32]"]: ...


@overload
@defines_dtype_strategy
def floating_dtypes(
*,
endianness: str = "?",
sizes: Literal[64],
) -> st.SearchStrategy["np.dtype[np.float64]"]: ...


@overload
@defines_dtype_strategy
def floating_dtypes(
*,
endianness: str = "?",
sizes: Literal[128],
) -> st.SearchStrategy["np.dtype[np.float128]"]: ...


@overload
@defines_dtype_strategy
def floating_dtypes(
*,
endianness: str = "?",
sizes: Sequence[Literal[16, 32, 64, 96, 128]] = (16, 32, 64),
) -> st.SearchStrategy["np.dtype[np.floating[Any]]"]: ...


@defines_dtype_strategy
def floating_dtypes(
*,
endianness: str = "?",
sizes: Union[
Literal[16, 32, 64, 96, 128], Sequence[Literal[16, 32, 64, 96, 128]]
] = (16, 32, 64),
) -> st.SearchStrategy["np.dtype[np.floating[Any]]"]:
"""Return a strategy for floating-point dtypes.
Expand All @@ -674,11 +821,50 @@ def floating_dtypes(
return dtype_factory("f", sizes, (16, 32, 64, 96, 128), endianness)


@overload
@defines_dtype_strategy
def complex_number_dtypes(
*,
endianness: str = "?",
sizes: Literal[64],
) -> st.SearchStrategy["np.dtype[np.complex64]"]: ...


@overload
@defines_dtype_strategy
def complex_number_dtypes(
*,
endianness: str = "?",
sizes: Literal[128],
) -> st.SearchStrategy["np.dtype[np.complex128]"]: ...


@overload
@defines_dtype_strategy
def complex_number_dtypes(
*,
endianness: str = "?",
sizes: Literal[256],
) -> st.SearchStrategy["np.dtype[np.complex256]"]: ...


@overload
@defines_dtype_strategy
def complex_number_dtypes(
*,
endianness: str = "?",
sizes: Sequence[Literal[64, 128, 192, 256]] = (64, 128),
) -> st.SearchStrategy["np.dtype[np.complexfloating[Any, Any]]"]: ...


@defines_dtype_strategy
def complex_number_dtypes(
*,
endianness: str = "?",
sizes: Union[Literal[64, 128, 192, 256], Sequence[Literal[64, 128, 192, 256]]] = (
64,
128,
),
) -> st.SearchStrategy["np.dtype[np.complexfloating[Any, Any]]"]:
"""Return a strategy for complex-number dtypes.
Expand Down
8 changes: 4 additions & 4 deletions hypothesis-python/tests/redis/test_redis_exampledatabase.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at https://mozilla.org/MPL/2.0/.

import uuid

import pytest
from fakeredis import FakeRedis

Expand Down Expand Up @@ -46,10 +48,8 @@ def test_all_methods():
class DatabaseComparison(RuleBasedStateMachine):
def __init__(self):
super().__init__()
self.dbs = [
InMemoryExampleDatabase(),
RedisExampleDatabase(FakeRedis()),
]
server = FakeRedis(host=uuid.uuid4().hex) # Different (fake) server each time
self.dbs = [InMemoryExampleDatabase(), RedisExampleDatabase(server)]

keys = Bundle("keys")
values = Bundle("values")
Expand Down
2 changes: 1 addition & 1 deletion requirements/coverage.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ exceptiongroup==1.2.0 ; python_version < "3.11"
# pytest
execnet==2.0.2
# via pytest-xdist
fakeredis==2.21.1
fakeredis==2.21.2
# via -r requirements/coverage.in
iniconfig==2.0.0
# via pytest
Expand Down
2 changes: 1 addition & 1 deletion tooling/src/hypothesistooling/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def tags():
.decode("ascii")
.strip()
)
REPO_TESTS = ROOT / "whole-repo-tests"
REPO_TESTS = ROOT / "whole_repo_tests"


def hash_for_name(name):
Expand Down
9 changes: 9 additions & 0 deletions whole_repo_tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# This file is part of Hypothesis, which may be found at
# https://github.com/HypothesisWorks/hypothesis/
#
# Copyright the Hypothesis Authors.
# Individual contributors are listed in AUTHORS.rst and the git log.
#
# This Source Code Form is subject to the terms of the Mozilla Public License,
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at https://mozilla.org/MPL/2.0/.

0 comments on commit e640900

Please sign in to comment.