Skip to content

Commit

Permalink
Updates for mypy
Browse files Browse the repository at this point in the history
  • Loading branch information
Zac-HD committed Nov 19, 2023
1 parent 405c19b commit fdf1c4f
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 16 deletions.
8 changes: 4 additions & 4 deletions hypothesis-python/src/hypothesis/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -610,10 +610,10 @@ def get_random_for_wrapped_test(test, wrapped_test):

@attr.s
class Stuff:
selfy = attr.ib(default=None)
args = attr.ib(factory=tuple)
kwargs = attr.ib(factory=dict)
given_kwargs = attr.ib(factory=dict)
selfy: Any = attr.ib(default=None)
args: tuple = attr.ib(factory=tuple)
kwargs: dict = attr.ib(factory=dict)
given_kwargs: dict = attr.ib(factory=dict)


def process_arguments_to_given(wrapped_test, arguments, kwargs, given_kwargs, params):
Expand Down
2 changes: 1 addition & 1 deletion hypothesis-python/src/hypothesis/extra/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,7 @@ def array_dtypes(
field_names,
st.tuples(field_names, field_names).filter(lambda ns: ns[0] != ns[1]),
)
elements = st.tuples(name_titles, subtype_strategy)
elements: st.SearchStrategy[tuple] = st.tuples(name_titles, subtype_strategy)
if allow_subarrays:
elements |= st.tuples(
name_titles, subtype_strategy, array_shapes(max_dims=2, max_side=2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@
}


GROUP_CACHE_STRATEGY = st.shared(st.builds(dict), key="hypothesis.regex.group_cache")
GROUP_CACHE_STRATEGY: st.SearchStrategy[dict] = st.shared(
st.builds(dict), key="hypothesis.regex.group_cache"
)


@st.composite
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ def _networks(bits):
st.binary(),
st.integers(0, 255),
# As with Reversible, we tuplize this for compatibility with Hashable.
st.lists(st.integers(0, 255)).map(tuple), # type: ignore
st.lists(st.integers(0, 255)).map(tuple),
),
typing.BinaryIO: st.builds(io.BytesIO, st.binary()),
typing.TextIO: st.builds(io.StringIO, st.text()),
Expand Down
12 changes: 3 additions & 9 deletions whole-repo-tests/test_mypy.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,13 @@ def convert_lines():
("dictionaries(integers(), datetimes())", "dict[int, datetime.datetime]"),
("data()", "hypothesis.strategies._internal.core.DataObject"),
("none() | integers()", "Union[None, int]"),
# Ex`-1 stands for recursion in the whole type, i.e. Ex`0 == Union[...]
("recursive(integers(), lists)", "Union[list[Ex`-1], int]"),
("recursive(integers(), lists)", "Union[list[Any], int]"),
# We have overloads for up to five types, then fall back to Any.
# (why five? JSON atoms are None|bool|int|float|str and we do that a lot)
("one_of(integers(), text())", "Union[int, str]"),
(
"one_of(integers(), text(), none(), binary(), builds(list))",
"Union[int, str, None, bytes, list[_T`1]]",
"Union[int, str, None, bytes, list[Never]]",
),
(
"one_of(integers(), text(), none(), binary(), builds(list), builds(dict))",
Expand All @@ -126,19 +125,14 @@ def convert_lines():
"tuples(text(), text(), text(), text(), text(), text())",
"tuple[Any, ...]",
),
(
"from_type(type).flatmap(from_type).filter(lambda x: not isinstance(x, int))",
"Ex_Inv`-1",
),
],
)
def test_revealed_types(tmpdir, val, expect):
"""Check that Mypy picks up the expected `X` in SearchStrategy[`X`]."""
f = tmpdir.join(expect + ".py")
f.write(
"from hypothesis.strategies import *\n"
f"s = {val}\n"
"reveal_type(s)\n" # fmt: skip
f"reveal_type({val})\n" # fmt: skip
)
typ = get_mypy_analysed_type(str(f.realpath()), val)
assert typ == f"SearchStrategy[{expect}]"
Expand Down

0 comments on commit fdf1c4f

Please sign in to comment.