Skip to content

Commit

Permalink
InferType -> EllipsesType
Browse files Browse the repository at this point in the history
  • Loading branch information
Zac-HD committed Oct 17, 2022
1 parent bea95f0 commit 07ff885
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 41 deletions.
21 changes: 10 additions & 11 deletions hypothesis-python/src/hypothesis/core.py
Expand Up @@ -110,16 +110,15 @@
MappedSearchStrategy,
SearchStrategy,
)
from hypothesis.utils.conventions import infer
from hypothesis.vendor.pretty import RepresentationPrinter
from hypothesis.version import __version__

if sys.version_info >= (3, 10): # pragma: no cover
from types import EllipsisType as InferType
from types import EllipsisType as EllipsisType
elif TYPE_CHECKING:
from builtins import ellipsis as InferType
from builtins import ellipsis as EllipsisType
else:
InferType = type(Ellipsis)
EllipsisType = type(Ellipsis)


TestFunc = TypeVar("TestFunc", bound=Callable)
Expand Down Expand Up @@ -292,7 +291,7 @@ def is_invalid_test(test, original_sig, given_arguments, given_kwargs):
f"arguments, but got {len(given_arguments)} {given_arguments!r}"
)

if infer in given_arguments:
if ... in given_arguments:
return invalid(
"... was passed as a positional argument to @given, but may only be "
"passed as a keyword argument or as the sole argument of @given"
Expand Down Expand Up @@ -998,7 +997,7 @@ def fuzz_one_input(

@overload
def given(
*_given_arguments: Union[SearchStrategy[Any], InferType],
*_given_arguments: Union[SearchStrategy[Any], EllipsisType],
) -> Callable[
[Callable[..., Optional[Coroutine[Any, Any, None]]]], Callable[..., None]
]: # pragma: no cover
Expand All @@ -1007,16 +1006,16 @@ def given(

@overload
def given(
**_given_kwargs: Union[SearchStrategy[Any], InferType],
**_given_kwargs: Union[SearchStrategy[Any], EllipsisType],
) -> Callable[
[Callable[..., Optional[Coroutine[Any, Any, None]]]], Callable[..., None]
]: # pragma: no cover
...


def given(
*_given_arguments: Union[SearchStrategy[Any], InferType],
**_given_kwargs: Union[SearchStrategy[Any], InferType],
*_given_arguments: Union[SearchStrategy[Any], EllipsisType],
**_given_kwargs: Union[SearchStrategy[Any], EllipsisType],
) -> Callable[
[Callable[..., Optional[Coroutine[Any, Any, None]]]], Callable[..., None]
]:
Expand Down Expand Up @@ -1069,9 +1068,9 @@ def run_test_as_given(test):
new_signature = new_given_signature(original_sig, given_kwargs)

# Use type information to convert "infer" arguments into appropriate strategies.
if infer in given_kwargs.values():
if ... in given_kwargs.values():
hints = get_type_hints(test)
for name in [name for name, value in given_kwargs.items() if value is infer]:
for name in [name for name, value in given_kwargs.items() if value is ...]:
if name not in hints:
return _invalid(
f"passed {name}=... for {test.__name__}, but {name} has "
Expand Down
15 changes: 7 additions & 8 deletions hypothesis-python/src/hypothesis/extra/django/_impl.py
Expand Up @@ -24,14 +24,13 @@
from hypothesis.extra.django._fields import from_field
from hypothesis.internal.reflection import define_function_signature
from hypothesis.strategies._internal.utils import defines_strategy
from hypothesis.utils.conventions import infer

if sys.version_info >= (3, 10): # pragma: no cover
from types import EllipsisType as InferType
from types import EllipsisType as EllipsisType
elif TYPE_CHECKING:
from builtins import ellipsis as InferType
from builtins import ellipsis as EllipsisType
else:
InferType = type(Ellipsis)
EllipsisType = type(Ellipsis)


class HypothesisTestCase:
Expand Down Expand Up @@ -67,7 +66,7 @@ class StaticLiveServerTestCase(HypothesisTestCase, dst.StaticLiveServerTestCase)

@defines_strategy()
def from_model(
*model: Type[dm.Model], **field_strategies: Union[st.SearchStrategy, InferType]
*model: Type[dm.Model], **field_strategies: Union[st.SearchStrategy, EllipsisType]
) -> st.SearchStrategy:
"""Return a strategy for examples of ``model``.
Expand Down Expand Up @@ -106,7 +105,7 @@ def from_model(

fields_by_name = {f.name: f for f in m_type._meta.concrete_fields}
for name, value in sorted(field_strategies.items()):
if value is infer:
if value is ...:
field_strategies[name] = from_field(fields_by_name[name])
for name, field in sorted(fields_by_name.items()):
if (
Expand Down Expand Up @@ -157,7 +156,7 @@ def _models_impl(draw, strat):
def from_form(
form: Type[df.Form],
form_kwargs: Optional[dict] = None,
**field_strategies: Union[st.SearchStrategy, InferType],
**field_strategies: Union[st.SearchStrategy, EllipsisType],
) -> st.SearchStrategy[df.Form]:
"""Return a strategy for examples of ``form``.
Expand Down Expand Up @@ -214,7 +213,7 @@ def from_form(
else:
fields_by_name[name] = field
for name, value in sorted(field_strategies.items()):
if value is infer:
if value is ...:
field_strategies[name] = from_field(fields_by_name[name])

for name, field in sorted(fields_by_name.items()):
Expand Down
20 changes: 8 additions & 12 deletions hypothesis-python/src/hypothesis/extra/ghostwriter.py
Expand Up @@ -116,14 +116,13 @@
SampledFromStrategy,
)
from hypothesis.strategies._internal.types import _global_type_lookup, is_generic_type
from hypothesis.utils.conventions import infer

if sys.version_info >= (3, 10): # pragma: no cover
from types import EllipsisType as InferType
from types import EllipsisType as EllipsisType
elif TYPE_CHECKING:
from builtins import ellipsis as InferType
from builtins import ellipsis as EllipsisType
else:
InferType = type(Ellipsis)
EllipsisType = type(Ellipsis)


IMPORT_SECTION = """
Expand Down Expand Up @@ -252,10 +251,7 @@ def _type_from_doc_fragment(token: str) -> Optional[type]:
return getattr(sys.modules.get(mod, None), name, None)


def _strategy_for(
param: inspect.Parameter,
docstring: str,
) -> Union[st.SearchStrategy, InferType]:
def _strategy_for(param: inspect.Parameter, docstring: str) -> st.SearchStrategy:
# Example types in docstrings:
# - `:type a: sequence of integers`
# - `b (list, tuple, or None): ...`
Expand Down Expand Up @@ -532,7 +528,7 @@ def _get_strategies(
hints = get_type_hints(f)
docstring = getattr(f, "__doc__", None) or ""
builder_args = {
k: infer if k in hints else _strategy_for(v, docstring)
k: ... if k in hints else _strategy_for(v, docstring)
for k, v in params.items()
}
with _with_any_registered():
Expand Down Expand Up @@ -1323,7 +1319,7 @@ def binary_operation(
*,
associative: bool = True,
commutative: bool = True,
identity: Union[X, InferType, None] = infer,
identity: Union[X, EllipsisType, None] = ...,
distributes_over: Optional[Callable[[X, X], X]] = None,
except_: Except = (),
style: str = "pytest",
Expand Down Expand Up @@ -1384,7 +1380,7 @@ def _make_binop_body(
*,
associative: bool = True,
commutative: bool = True,
identity: Union[X, InferType, None] = infer,
identity: Union[X, EllipsisType, None] = ...,
distributes_over: Optional[Callable[[X, X], X]] = None,
except_: Tuple[Type[Exception], ...],
style: str,
Expand Down Expand Up @@ -1454,7 +1450,7 @@ def maker(
# Guess that the identity element is the minimal example from our operands
# strategy. This is correct often enough to be worthwhile, and close enough
# that it's a good starting point to edit much of the rest.
if identity is infer:
if identity is ...:
try:
identity = find(operands, lambda x: True, settings=_quietly_settings)
except Exception:
Expand Down
20 changes: 10 additions & 10 deletions hypothesis-python/src/hypothesis/strategies/_internal/core.py
Expand Up @@ -114,14 +114,14 @@
TextStrategy,
)
from hypothesis.strategies._internal.utils import cacheable, defines_strategy
from hypothesis.utils.conventions import infer, not_set
from hypothesis.utils.conventions import not_set

if sys.version_info >= (3, 10): # pragma: no cover
from types import EllipsisType as InferType
from types import EllipsisType as EllipsisType
elif typing.TYPE_CHECKING: # pragma: no cover
from builtins import ellipsis as InferType
from builtins import ellipsis as EllipsisType
else:
InferType = type(Ellipsis)
EllipsisType = type(Ellipsis)


try:
Expand Down Expand Up @@ -863,7 +863,7 @@ def __repr__(self):
@defines_strategy()
def builds(
*callable_and_args: Union[Callable[..., Ex], SearchStrategy[Any]],
**kwargs: Union[SearchStrategy[Any], InferType],
**kwargs: Union[SearchStrategy[Any], EllipsisType],
) -> SearchStrategy[Ex]:
"""Generates values by drawing from ``args`` and ``kwargs`` and passing
them to the callable (provided as the first positional argument) in the
Expand Down Expand Up @@ -898,14 +898,14 @@ def builds(
"target to construct."
)

if infer in args: # type: ignore # we only annotated the allowed types
if ... in args: # type: ignore # we only annotated the allowed types
# Avoid an implementation nightmare juggling tuples and worse things
raise InvalidArgument(
"... was passed as a positional argument to "
"builds(), but is only allowed as a keyword arg"
)
required = required_args(target, args, kwargs)
to_infer = {k for k, v in kwargs.items() if v is infer}
to_infer = {k for k, v in kwargs.items() if v is ...}
if required or to_infer:
if isinstance(target, type) and attr.has(target):
# Use our custom introspection for attrs classes
Expand Down Expand Up @@ -1993,7 +1993,7 @@ def _functions(*, like, returns, pure):
"The first argument to functions() must be a callable to imitate, "
f"but got non-callable like={nicerepr(like)!r}"
)
if returns is None or returns is infer:
if returns in (None, ...):
# Passing `None` has never been *documented* as working, but it still
# did from May 2020 to Jan 2022 so we'll avoid breaking it without cause.
hints = get_type_hints(like)
Expand Down Expand Up @@ -2036,7 +2036,7 @@ def functions(
...

@defines_strategy()
def functions(*, like=lambda: None, returns=infer, pure=False):
def functions(*, like=lambda: None, returns=..., pure=False):
# We shouldn't need overloads here, but mypy disallows default args for
# generics: https://github.com/python/mypy/issues/3737
"""functions(*, like=lambda: None, returns=..., pure=False)
Expand Down Expand Up @@ -2067,7 +2067,7 @@ def functions(*, like=lambda: None, returns=infer, pure=False):
def functions(
*,
like: Callable[..., Any] = lambda: None,
returns: Union[SearchStrategy[Any], InferType] = infer,
returns: Union[SearchStrategy[Any], EllipsisType] = ...,
pure: bool = False,
) -> SearchStrategy[Callable[..., Any]]:
"""functions(*, like=lambda: None, returns=..., pure=False)
Expand Down

0 comments on commit 07ff885

Please sign in to comment.