Skip to content

Commit

Permalink
Make dict hint more useful in normal case
Browse files Browse the repository at this point in the history
  • Loading branch information
Zac-HD committed Jun 25, 2018
1 parent 45afe6b commit 6d58963
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
7 changes: 7 additions & 0 deletions hypothesis-python/docs/details.rst
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,13 @@ up our type hints.
which is otherwise backwards-compatible. We often omit type hints for
deprecated features or arguments, as an additional form of warning.
There are known issues inferring the type of examples generated by
:func:`~hypothesis.strategies.deferred`, :func:`~hypothesis.strategies.recursive`,
:func:`~hypothesis.strategies.one_of`, :func:`~hypothesis.strategies.dictionaries`,
and :func:`~hypothesis.strategies.fixed_dictionaries`.
We will fix these, and require correspondingly newer versions of Mypy for type
hinting, as the ecosystem improves.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Writing downstream type hints
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
8 changes: 5 additions & 3 deletions hypothesis-python/src/hypothesis/strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
from random import Random # noqa
from typing import Any, Dict, Union, Sequence, Callable, Pattern # noqa
from typing import TypeVar, Tuple, List, Set, FrozenSet, overload # noqa
from typing import Type, Mapping, Text, AnyStr, Optional # noqa
from typing import Type, Text, AnyStr, Optional # noqa

from hypothesis.utils.conventions import InferType # noqa
from hypothesis.searchstrategy.strategies import T, Ex # noqa
Expand Down Expand Up @@ -716,7 +716,7 @@ def iterables(elements=None, min_size=None, average_size=None, max_size=None,

@defines_strategy
def fixed_dictionaries(
mapping # type: Mapping[T, SearchStrategy[Ex]]
mapping # type: Dict[T, SearchStrategy[Ex]]
):
# type: (...) -> SearchStrategy[Dict[T, Ex]]
"""Generates a dictionary of the same type as mapping with a fixed set of
Expand Down Expand Up @@ -746,7 +746,9 @@ def dictionaries(
average_size=None, # type: int
max_size=None, # type: int
):
# type: (...) -> SearchStrategy[Mapping[Ex, T]]
# type: (...) -> SearchStrategy[Dict[Ex, T]]
# Describing the exact dict_class to Mypy drops the key and value types,
# so we report Dict[K, V] instead of Mapping[Any, Any] for now. Sorry!
"""Generates dictionaries of type dict_class with keys drawn from the keys
argument and values drawn from the values argument.
Expand Down
8 changes: 3 additions & 5 deletions whole-repo-tests/test_type_hints.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,12 @@ def get_mypy_analysed_type(fname, val):
('integers().map(str)', 'str'),
('booleans().filter(bool)', 'bool'),
('lists(none())', 'list[None]'),
('dictionaries(integers(), datetimes())',
'typing.Mapping[int, datetime.datetime]'),
('dictionaries(integers(), datetimes())', 'dict[int, datetime.datetime]'),
('recursive(integers(), lists)', 'Union[list[Ex`-1], int]'),
# Mypy might not resolve deferred strategies, but it doesn't error either.
('deferred(lambda: integers() | lists(s))', 'Any'),
# Note: this *should* give 'Union[int, str]' (or Text), but Mypy doesn't
# cope with finding the union of the parameter types.
# If or when this changes, fix the hints on `one_of` too!
# See https://github.com/python/mypy/issues/5269 - fix the hints on
# `one_of` and document the minimum Mypy version when the issue is fixed.
('one_of(integers(), text())', 'Any'),
])
def test_revealed_types(tmpdir, val, expect):
Expand Down

0 comments on commit 6d58963

Please sign in to comment.