Skip to content

Commit

Permalink
Set no_implicit_optional to True for mypy and align typehints
Browse files Browse the repository at this point in the history
  • Loading branch information
kprzybyla authored and Zac-HD committed Sep 11, 2020
1 parent 301f8e9 commit f3dbed5
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 77 deletions.
6 changes: 3 additions & 3 deletions hypothesis-python/src/hypothesis/_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import threading
import warnings
from enum import Enum, IntEnum, unique
from typing import Any, Dict, List
from typing import Any, Dict, List, Optional

import attr

Expand Down Expand Up @@ -140,7 +140,7 @@ def __getattr__(self, name):
else:
raise AttributeError("settings has no attribute %s" % (name,))

def __init__(self, parent: "settings" = None, **kwargs: Any) -> None:
def __init__(self, parent: Optional["settings"] = None, **kwargs: Any) -> None:
if parent is not None and not isinstance(parent, settings):
raise InvalidArgument(
"Invalid argument: parent=%r is not a settings instance" % (parent,)
Expand Down Expand Up @@ -291,7 +291,7 @@ def show_changed(self):
return ", ".join(sorted(bits, key=len))

@staticmethod
def register_profile(name: str, parent: "settings" = None, **kwargs: Any) -> None:
def register_profile(name: str, parent: Optional["settings"] = None, **kwargs: Any) -> None:
"""Registers a collection of values to be used as a settings profile.
Settings profiles can be loaded by name - for example, you might
Expand Down
6 changes: 3 additions & 3 deletions hypothesis-python/src/hypothesis/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1223,9 +1223,9 @@ def find(
specifier: SearchStrategy[Ex],
condition: Callable[[Any], bool],
*,
settings: Settings = None,
random: Random = None,
database_key: bytes = None
settings: Optional[Settings] = None,
random: Optional[Random] = None,
database_key: Optional[bytes] = None
) -> Ex:
"""Returns the minimal example from the given strategy ``specifier`` that
matches the predicate function ``condition``."""
Expand Down
4 changes: 2 additions & 2 deletions hypothesis-python/src/hypothesis/extra/django/_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import unittest
from functools import partial
from inspect import Parameter, signature
from typing import Type, Union
from typing import Type, Union, Optional

from django import forms as df, test as dt
from django.core.exceptions import ValidationError
Expand Down Expand Up @@ -149,7 +149,7 @@ def _models_impl(draw, strat):
@st.defines_strategy()
def from_form(
form: Type[df.Form],
form_kwargs: dict = None,
form_kwargs: Optional[dict] = None,
**field_strategies: Union[st.SearchStrategy, InferType]
) -> st.SearchStrategy[df.Form]:
"""Return a strategy for examples of ``form``.
Expand Down
10 changes: 5 additions & 5 deletions hypothesis-python/src/hypothesis/extra/ghostwriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
from itertools import permutations, zip_longest
from string import ascii_lowercase
from textwrap import dedent, indent
from typing import Any, Callable, Dict, Mapping, Set, Tuple, Type, TypeVar, Union
from typing import Any, Callable, Dict, Mapping, Set, Tuple, Type, TypeVar, Union, Optional

import black

Expand Down Expand Up @@ -336,7 +336,7 @@ def _make_test_body(
test_body: str,
except_: Tuple[Type[Exception], ...],
style: str,
given_strategies: Mapping[str, Union[str, st.SearchStrategy]] = None,
given_strategies: Optional[Mapping[str, Union[str, st.SearchStrategy]]] = None,
) -> Tuple[Set[str], str]:
# Get strategies for all the arguments to each function we're testing.
with _with_any_registered():
Expand Down Expand Up @@ -763,7 +763,7 @@ def binary_operation(
associative: bool = True,
commutative: bool = True,
identity: Union[X, InferType, None] = infer,
distributes_over: Callable[[X, X], X] = None,
distributes_over: Optional[Callable[[X, X], X]] = None,
except_: Except = (),
style: str = "pytest",
) -> str:
Expand Down Expand Up @@ -825,7 +825,7 @@ def _make_binop_body(
associative: bool = True,
commutative: bool = True,
identity: Union[X, InferType, None] = infer,
distributes_over: Callable[[X, X], X] = None,
distributes_over: Optional[Callable[[X, X], X]] = None,
except_: Tuple[Type[Exception], ...],
style: str,
) -> Tuple[Set[str], str]:
Expand All @@ -840,7 +840,7 @@ def _make_binop_body(
all_imports = set()
parts = []

def maker(sub_property: str, args: str, body: str, right: str = None) -> None:
def maker(sub_property: str, args: str, body: str, right: Optional[str] = None) -> None:
if right is not None:
body = f"left={body}\nright={right}\n" + _assert_eq(style, "left", "right")
imports, body = _make_test_body(
Expand Down
6 changes: 3 additions & 3 deletions hypothesis-python/src/hypothesis/extra/lark.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"""

from inspect import getfullargspec
from typing import Dict
from typing import Dict, Optional

import attr
import lark
Expand Down Expand Up @@ -198,8 +198,8 @@ def inner(value):
def from_lark(
grammar: lark.lark.Lark,
*,
start: str = None,
explicit: Dict[str, st.SearchStrategy[str]] = None
start: Optional[str] = None,
explicit: Optional[Dict[str, st.SearchStrategy[str]]] = None
) -> st.SearchStrategy[str]:
"""A strategy for strings accepted by the given context-free grammar.
Expand Down
26 changes: 15 additions & 11 deletions hypothesis-python/src/hypothesis/extra/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import math
import re
from typing import Any, NamedTuple, Sequence, Tuple, Union
from typing import Any, NamedTuple, Sequence, Tuple, Union, Optional

import numpy as np

Expand Down Expand Up @@ -303,8 +303,8 @@ def arrays(
dtype: Any,
shape: Union[int, Shape, st.SearchStrategy[Shape]],
*,
elements: st.SearchStrategy[Any] = None,
fill: st.SearchStrategy[Any] = None,
elements: Optional[st.SearchStrategy[Any]] = None,
fill: Optional[st.SearchStrategy[Any]] = None,
unique: bool = False
) -> st.SearchStrategy[np.ndarray]:
r"""Returns a strategy for generating :class:`numpy:numpy.ndarray`\ s.
Expand Down Expand Up @@ -419,7 +419,11 @@ def arrays(
@st.defines_strategy()
@deprecated_posargs
def array_shapes(
*, min_dims: int = 1, max_dims: int = None, min_side: int = 1, max_side: int = None
*,
min_dims: int = 1,
max_dims: Optional[int] = None,
min_side: int = 1,
max_side: Optional[int] = None,
) -> st.SearchStrategy[Shape]:
"""Return a strategy for array shapes (tuples of int >= 1)."""
check_type(int, min_dims, "min_dims")
Expand Down Expand Up @@ -709,7 +713,7 @@ def nested_dtypes(
subtype_strategy: st.SearchStrategy[np.dtype] = scalar_dtypes(),
*,
max_leaves: int = 10,
max_itemsize: int = None
max_itemsize: Optional[int] = None
) -> st.SearchStrategy[np.dtype]:
"""Return the most-general dtype strategy.
Expand All @@ -729,7 +733,7 @@ def nested_dtypes(
@st.defines_strategy()
@deprecated_posargs
def valid_tuple_axes(
ndim: int, *, min_size: int = 0, max_size: int = None
ndim: int, *, min_size: int = 0, max_size: Optional[int] = None
) -> st.SearchStrategy[Shape]:
"""Return a strategy for generating permissible tuple-values for the
``axis`` argument for a numpy sequential function (e.g.
Expand Down Expand Up @@ -781,9 +785,9 @@ def broadcastable_shapes(
shape: Shape,
*,
min_dims: int = 0,
max_dims: int = None,
max_dims: Optional[int] = None,
min_side: int = 1,
max_side: int = None
max_side: Optional[int] = None
) -> st.SearchStrategy[Shape]:
"""Return a strategy for generating shapes that are broadcast-compatible
with the provided shape.
Expand Down Expand Up @@ -1102,9 +1106,9 @@ def mutually_broadcastable_shapes(
signature: Union[UniqueIdentifier, str] = not_set,
base_shape: Shape = (),
min_dims: int = 0,
max_dims: int = None,
max_dims: Optional[int] = None,
min_side: int = 1,
max_side: int = None
max_side: Optional[int] = None
) -> st.SearchStrategy[BroadcastableShapes]:
"""Return a strategy for generating a specified number of shapes, N, that are
mutually-broadcastable with one another and with the provided "base-shape".
Expand Down Expand Up @@ -1326,7 +1330,7 @@ def basic_indices(
shape: Shape,
*,
min_dims: int = 0,
max_dims: int = None,
max_dims: Optional[int] = None,
allow_newaxis: bool = False,
allow_ellipsis: bool = True
) -> st.SearchStrategy[BasicIndex]:
Expand Down
24 changes: 12 additions & 12 deletions hypothesis-python/src/hypothesis/extra/pandas/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from collections import OrderedDict, abc
from copy import copy
from typing import Any, List, Sequence, Set, Union
from typing import Any, List, Sequence, Set, Union, Optional

import attr
import numpy as np
Expand Down Expand Up @@ -154,7 +154,7 @@ def do_draw(self, data):
@st.cacheable
@st.defines_strategy()
def range_indexes(
min_size: int = 0, max_size: int = None
min_size: int = 0, max_size: Optional[int] = None
) -> st.SearchStrategy[pandas.RangeIndex]:
"""Provides a strategy which generates an :class:`~pandas.Index` whose
values are 0, 1, ..., n for some n.
Expand All @@ -178,10 +178,10 @@ def range_indexes(
@deprecated_posargs
def indexes(
*,
elements: st.SearchStrategy[Ex] = None,
elements: Optional[st.SearchStrategy[Ex]] = None,
dtype: Any = None,
min_size: int = 0,
max_size: int = None,
max_size: Optional[int] = None,
unique: bool = True
) -> st.SearchStrategy[pandas.Index]:
"""Provides a strategy for producing a :class:`pandas.Index`.
Expand Down Expand Up @@ -219,10 +219,10 @@ def indexes(
@deprecated_posargs
def series(
*,
elements: st.SearchStrategy[Ex] = None,
elements: Optional[st.SearchStrategy[Ex]] = None,
dtype: Any = None,
index: st.SearchStrategy[Union[Sequence, pandas.Index]] = None,
fill: st.SearchStrategy[Ex] = None,
index: Optional[st.SearchStrategy[Union[Sequence, pandas.Index]]] = None,
fill: Optional[st.SearchStrategy[Ex]] = None,
unique: bool = False
) -> st.SearchStrategy[pandas.Series]:
"""Provides a strategy for producing a :class:`pandas.Series`.
Expand Down Expand Up @@ -337,8 +337,8 @@ def columns(
names_or_number: Union[int, Sequence[str]],
*,
dtype: Any = None,
elements: st.SearchStrategy[Ex] = None,
fill: st.SearchStrategy[Ex] = None,
elements: Optional[st.SearchStrategy[Ex]] = None,
fill: Optional[st.SearchStrategy[Ex]] = None,
unique: bool = False
) -> List[column]:
"""A convenience function for producing a list of :class:`column` objects
Expand All @@ -363,10 +363,10 @@ def columns(
@deprecated_posargs
@st.defines_strategy()
def data_frames(
columns: Sequence[column] = None,
columns: Optional[Sequence[column]] = None,
*,
rows: st.SearchStrategy[Union[dict, Sequence[Any]]] = None,
index: st.SearchStrategy[Ex] = None
rows: Optional[st.SearchStrategy[Union[dict, Sequence[Any]]]] = None,
index: Optional[st.SearchStrategy[Ex]] = None
) -> st.SearchStrategy[pandas.DataFrame]:
"""Provides a strategy for producing a :class:`pandas.DataFrame`.
Expand Down
Loading

0 comments on commit f3dbed5

Please sign in to comment.