Skip to content

Commit

Permalink
Create unique types for UniqueIdentifiers
Browse files Browse the repository at this point in the history
(so Mypy can tell you're using the wrong one)
  • Loading branch information
Zac-HD committed May 8, 2018
1 parent 52f2793 commit 5c021f3
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
6 changes: 3 additions & 3 deletions hypothesis-python/src/hypothesis/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@

if False:
from typing import Any, Dict, Callable, Optional, Union # noqa
from hypothesis.utils.conventions import UniqueIdentifier # noqa
from hypothesis.utils.conventions import InferType # noqa


running_under_pytest = False
Expand Down Expand Up @@ -894,8 +894,8 @@ def fake_subTest(self, msg=None, **__):


def given(
*given_arguments, # type: Union[SearchStrategy, UniqueIdentifier]
**given_kwargs # type: Union[SearchStrategy, UniqueIdentifier]
*given_arguments, # type: Union[SearchStrategy, InferType]
**given_kwargs # type: Union[SearchStrategy, InferType]
):
# type: (...) -> Callable[[Callable[..., None]], Callable[..., None]]
"""A decorator for turning a test function that accepts arguments into a
Expand Down
15 changes: 7 additions & 8 deletions hypothesis-python/src/hypothesis/extra/django/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@
from hypothesis.extra.pytz import timezones
from hypothesis.provisional import emails, ip4_addr_strings, \
ip6_addr_strings
from hypothesis.utils.conventions import UniqueIdentifier
from hypothesis.utils.conventions import DefaultValueType, default_value

if False:
from datetime import tzinfo # noqa
from typing import Any, Type, Optional, List, Text, Callable, Union # noqa
from typing import Any, Dict, Type, Optional, List, Text, Callable, Union # noqa


def get_tz_strat():
Expand Down Expand Up @@ -94,9 +94,6 @@ def add_default_field_mapping(field_type, strategy):
field_mappings()[field_type] = strategy


default_value = UniqueIdentifier(u'default_value')


def validator_to_filter(f):
# type: (Type[dm.Field]) -> Callable[[Any], bool]
"""Converts the field run_validators method to something suitable for use
Expand Down Expand Up @@ -171,7 +168,7 @@ def _get_strategy_for_field(f):

def models(
model, # type: Type[dm.Model]
**field_strategies # type: Union[st.SearchStrategy[Any], UniqueIdentifier]
**field_strategies # type: Union[st.SearchStrategy[Any], DefaultValueType]
):
# type: (...) -> st.SearchStrategy[Any]
"""Return a strategy for examples of ``model``.
Expand Down Expand Up @@ -199,8 +196,10 @@ def models(
shop_strategy = models(Shop, company=models(Company))
"""
result = {k: v for k, v in field_strategies.items()
if v is not default_value}
result = {}
for k, v in field_strategies.items():
if not isinstance(v, DefaultValueType):
result[k] = v
missed = [] # type: List[Text]
for f in model._meta.concrete_fields:
if not (f.name in field_strategies or isinstance(f, dm.AutoField)):
Expand Down
4 changes: 2 additions & 2 deletions hypothesis-python/src/hypothesis/strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
from typing import TypeVar, Tuple, List, Set, FrozenSet, overload # noqa
from typing import Type, Mapping, Text, AnyStr, Optional # noqa

from hypothesis.utils.conventions import UniqueIdentifier # noqa
from hypothesis.utils.conventions import InferType # noqa
from hypothesis.searchstrategy.strategies import T, Ex # noqa
K, V = TypeVar['K'], TypeVar['V']
# See https://github.com/python/mypy/issues/3186 - numbers.Real is wrong!
Expand Down Expand Up @@ -1024,7 +1024,7 @@ def do_draw(self, data):
@defines_strategy
def builds(
*callable_and_args, # type: Any
**kwargs # type: Union[SearchStrategy[Any], UniqueIdentifier]
**kwargs # type: Union[SearchStrategy[Any], InferType]
):
# type: (...) -> SearchStrategy[Any]
"""Generates values by drawing from ``args`` and ``kwargs`` and passing
Expand Down
12 changes: 11 additions & 1 deletion hypothesis-python/src/hypothesis/utils/conventions.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,15 @@ def __repr__(self):
return self.identifier


infer = UniqueIdentifier(u'infer')
class DefaultValueType(UniqueIdentifier):
# Using subclasses so they can be distinguished by e.g. Mypy
pass


class InferType(UniqueIdentifier):
pass


not_set = UniqueIdentifier(u'not_set')
default_value = DefaultValueType(u'default_value')
infer = InferType(u'infer')

0 comments on commit 5c021f3

Please sign in to comment.