Skip to content

Commit e640900

Browse files
authored
Merge pull request #3911 from JonathanPlasse/improve-and-test-numpy-extra
Improve and test hypothesis.extra.numpy type annotations
2 parents 57d5087 + 64da5d2 commit e640900

18 files changed

+404
-53
lines changed

hypothesis-python/RELEASE.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
RELEASE_TYPE: patch
2+
3+
This patch further improves the type annotations in :mod:`hypothesis.extra.numpy`.

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

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,11 +624,61 @@ def dtype_factory(kind, sizes, valid_sizes, endianness):
624624
return strat.map((endianness + kind).format)
625625

626626

627+
@overload
628+
@defines_dtype_strategy
629+
def unsigned_integer_dtypes(
630+
*,
631+
endianness: str = "?",
632+
sizes: Literal[8],
633+
) -> st.SearchStrategy["np.dtype[np.uint8]"]: ...
634+
635+
636+
@overload
637+
@defines_dtype_strategy
638+
def unsigned_integer_dtypes(
639+
*,
640+
endianness: str = "?",
641+
sizes: Literal[16],
642+
) -> st.SearchStrategy["np.dtype[np.uint16]"]: ...
643+
644+
645+
@overload
646+
@defines_dtype_strategy
647+
def unsigned_integer_dtypes(
648+
*,
649+
endianness: str = "?",
650+
sizes: Literal[32],
651+
) -> st.SearchStrategy["np.dtype[np.uint32]"]: ...
652+
653+
654+
@overload
655+
@defines_dtype_strategy
656+
def unsigned_integer_dtypes(
657+
*,
658+
endianness: str = "?",
659+
sizes: Literal[64],
660+
) -> st.SearchStrategy["np.dtype[np.uint64]"]: ...
661+
662+
663+
@overload
627664
@defines_dtype_strategy
628665
def unsigned_integer_dtypes(
629666
*,
630667
endianness: str = "?",
631668
sizes: Sequence[Literal[8, 16, 32, 64]] = (8, 16, 32, 64),
669+
) -> st.SearchStrategy["np.dtype[np.unsignedinteger[Any]]"]: ...
670+
671+
672+
@defines_dtype_strategy
673+
def unsigned_integer_dtypes(
674+
*,
675+
endianness: str = "?",
676+
sizes: Union[Literal[8, 16, 32, 64], Sequence[Literal[8, 16, 32, 64]]] = (
677+
8,
678+
16,
679+
32,
680+
64,
681+
),
632682
) -> st.SearchStrategy["np.dtype[np.unsignedinteger[Any]]"]:
633683
"""Return a strategy for unsigned integer dtypes.
634684
@@ -642,11 +692,61 @@ def unsigned_integer_dtypes(
642692
return dtype_factory("u", sizes, (8, 16, 32, 64), endianness)
643693

644694

695+
@overload
696+
@defines_dtype_strategy
697+
def integer_dtypes(
698+
*,
699+
endianness: str = "?",
700+
sizes: Literal[8],
701+
) -> st.SearchStrategy["np.dtype[np.int8]"]: ...
702+
703+
704+
@overload
705+
@defines_dtype_strategy
706+
def integer_dtypes(
707+
*,
708+
endianness: str = "?",
709+
sizes: Literal[16],
710+
) -> st.SearchStrategy["np.dtype[np.int16]"]: ...
711+
712+
713+
@overload
714+
@defines_dtype_strategy
715+
def integer_dtypes(
716+
*,
717+
endianness: str = "?",
718+
sizes: Literal[32],
719+
) -> st.SearchStrategy["np.dtype[np.int32]"]: ...
720+
721+
722+
@overload
723+
@defines_dtype_strategy
724+
def integer_dtypes(
725+
*,
726+
endianness: str = "?",
727+
sizes: Literal[64],
728+
) -> st.SearchStrategy["np.dtype[np.int64]"]: ...
729+
730+
731+
@overload
645732
@defines_dtype_strategy
646733
def integer_dtypes(
647734
*,
648735
endianness: str = "?",
649736
sizes: Sequence[Literal[8, 16, 32, 64]] = (8, 16, 32, 64),
737+
) -> st.SearchStrategy["np.dtype[np.signedinteger[Any]]"]: ...
738+
739+
740+
@defines_dtype_strategy
741+
def integer_dtypes(
742+
*,
743+
endianness: str = "?",
744+
sizes: Union[Literal[8, 16, 32, 64], Sequence[Literal[8, 16, 32, 64]]] = (
745+
8,
746+
16,
747+
32,
748+
64,
749+
),
650750
) -> st.SearchStrategy["np.dtype[np.signedinteger[Any]]"]:
651751
"""Return a strategy for signed integer dtypes.
652752
@@ -656,11 +756,58 @@ def integer_dtypes(
656756
return dtype_factory("i", sizes, (8, 16, 32, 64), endianness)
657757

658758

759+
@overload
760+
@defines_dtype_strategy
761+
def floating_dtypes(
762+
*,
763+
endianness: str = "?",
764+
sizes: Literal[16],
765+
) -> st.SearchStrategy["np.dtype[np.float16]"]: ...
766+
767+
768+
@overload
769+
@defines_dtype_strategy
770+
def floating_dtypes(
771+
*,
772+
endianness: str = "?",
773+
sizes: Literal[32],
774+
) -> st.SearchStrategy["np.dtype[np.float32]"]: ...
775+
776+
777+
@overload
778+
@defines_dtype_strategy
779+
def floating_dtypes(
780+
*,
781+
endianness: str = "?",
782+
sizes: Literal[64],
783+
) -> st.SearchStrategy["np.dtype[np.float64]"]: ...
784+
785+
786+
@overload
787+
@defines_dtype_strategy
788+
def floating_dtypes(
789+
*,
790+
endianness: str = "?",
791+
sizes: Literal[128],
792+
) -> st.SearchStrategy["np.dtype[np.float128]"]: ...
793+
794+
795+
@overload
659796
@defines_dtype_strategy
660797
def floating_dtypes(
661798
*,
662799
endianness: str = "?",
663800
sizes: Sequence[Literal[16, 32, 64, 96, 128]] = (16, 32, 64),
801+
) -> st.SearchStrategy["np.dtype[np.floating[Any]]"]: ...
802+
803+
804+
@defines_dtype_strategy
805+
def floating_dtypes(
806+
*,
807+
endianness: str = "?",
808+
sizes: Union[
809+
Literal[16, 32, 64, 96, 128], Sequence[Literal[16, 32, 64, 96, 128]]
810+
] = (16, 32, 64),
664811
) -> st.SearchStrategy["np.dtype[np.floating[Any]]"]:
665812
"""Return a strategy for floating-point dtypes.
666813
@@ -674,11 +821,50 @@ def floating_dtypes(
674821
return dtype_factory("f", sizes, (16, 32, 64, 96, 128), endianness)
675822

676823

824+
@overload
825+
@defines_dtype_strategy
826+
def complex_number_dtypes(
827+
*,
828+
endianness: str = "?",
829+
sizes: Literal[64],
830+
) -> st.SearchStrategy["np.dtype[np.complex64]"]: ...
831+
832+
833+
@overload
834+
@defines_dtype_strategy
835+
def complex_number_dtypes(
836+
*,
837+
endianness: str = "?",
838+
sizes: Literal[128],
839+
) -> st.SearchStrategy["np.dtype[np.complex128]"]: ...
840+
841+
842+
@overload
843+
@defines_dtype_strategy
844+
def complex_number_dtypes(
845+
*,
846+
endianness: str = "?",
847+
sizes: Literal[256],
848+
) -> st.SearchStrategy["np.dtype[np.complex256]"]: ...
849+
850+
851+
@overload
677852
@defines_dtype_strategy
678853
def complex_number_dtypes(
679854
*,
680855
endianness: str = "?",
681856
sizes: Sequence[Literal[64, 128, 192, 256]] = (64, 128),
857+
) -> st.SearchStrategy["np.dtype[np.complexfloating[Any, Any]]"]: ...
858+
859+
860+
@defines_dtype_strategy
861+
def complex_number_dtypes(
862+
*,
863+
endianness: str = "?",
864+
sizes: Union[Literal[64, 128, 192, 256], Sequence[Literal[64, 128, 192, 256]]] = (
865+
64,
866+
128,
867+
),
682868
) -> st.SearchStrategy["np.dtype[np.complexfloating[Any, Any]]"]:
683869
"""Return a strategy for complex-number dtypes.
684870

hypothesis-python/tests/redis/test_redis_exampledatabase.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
99
# obtain one at https://mozilla.org/MPL/2.0/.
1010

11+
import uuid
12+
1113
import pytest
1214
from fakeredis import FakeRedis
1315

@@ -46,10 +48,8 @@ def test_all_methods():
4648
class DatabaseComparison(RuleBasedStateMachine):
4749
def __init__(self):
4850
super().__init__()
49-
self.dbs = [
50-
InMemoryExampleDatabase(),
51-
RedisExampleDatabase(FakeRedis()),
52-
]
51+
server = FakeRedis(host=uuid.uuid4().hex) # Different (fake) server each time
52+
self.dbs = [InMemoryExampleDatabase(), RedisExampleDatabase(server)]
5353

5454
keys = Bundle("keys")
5555
values = Bundle("values")

requirements/coverage.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ exceptiongroup==1.2.0 ; python_version < "3.11"
2626
# pytest
2727
execnet==2.0.2
2828
# via pytest-xdist
29-
fakeredis==2.21.1
29+
fakeredis==2.21.2
3030
# via -r requirements/coverage.in
3131
iniconfig==2.0.0
3232
# via pytest

tooling/src/hypothesistooling/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def tags():
3737
.decode("ascii")
3838
.strip()
3939
)
40-
REPO_TESTS = ROOT / "whole-repo-tests"
40+
REPO_TESTS = ROOT / "whole_repo_tests"
4141

4242

4343
def hash_for_name(name):

whole_repo_tests/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# This file is part of Hypothesis, which may be found at
2+
# https://github.com/HypothesisWorks/hypothesis/
3+
#
4+
# Copyright the Hypothesis Authors.
5+
# Individual contributors are listed in AUTHORS.rst and the git log.
6+
#
7+
# This Source Code Form is subject to the terms of the Mozilla Public License,
8+
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
9+
# obtain one at https://mozilla.org/MPL/2.0/.

0 commit comments

Comments
 (0)