Skip to content

Commit

Permalink
Merge pull request #3915 from JonathanPlasse/simplify-pandas-type-ann…
Browse files Browse the repository at this point in the history
…otations

Simplify pandas type annotations using PEP 696
  • Loading branch information
Zac-HD committed Mar 10, 2024
2 parents a8f83d1 + a63d753 commit 27393c9
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 104 deletions.
4 changes: 4 additions & 0 deletions hypothesis-python/RELEASE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
RELEASE_TYPE: patch

Simplify the type annotation of :func:`~hypothesis.extra.pandas.column` and
:func:`~hypothesis.extra.pandas.columns` by using :pep:`696` to avoid overloading.
61 changes: 2 additions & 59 deletions hypothesis-python/src/hypothesis/extra/pandas/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from collections import OrderedDict, abc
from copy import copy
from datetime import datetime, timedelta
from typing import Any, Generic, List, Optional, Sequence, Set, Union, overload
from typing import Any, Generic, List, Optional, Sequence, Set, Union

import attr
import numpy as np
Expand Down Expand Up @@ -357,7 +357,7 @@ def result(draw):
return result()


@attr.s(slots=True, init=False)
@attr.s(slots=True)
class column(Generic[Ex]):
"""Data object for describing a column in a DataFrame.
Expand All @@ -381,63 +381,6 @@ class column(Generic[Ex]):
fill: Optional[st.SearchStrategy[Ex]] = attr.ib(default=None)
unique: bool = attr.ib(default=False)

@overload
def __init__(
self: "column[Any]",
name: Optional[Union[str, int]] = ...,
elements: None = ...,
dtype: Any = ...,
fill: None = ...,
unique: bool = ..., # noqa: FBT001
) -> None: ...

@overload
def __init__(
self: "column[Ex]",
name: Optional[Union[str, int]] = ...,
elements: Optional[st.SearchStrategy[Ex]] = ...,
dtype: Any = ...,
fill: Optional[st.SearchStrategy[Ex]] = ...,
unique: bool = ..., # noqa: FBT001
) -> None: ...

def __init__(
self,
name: Optional[Union[str, int]] = None,
elements: Optional[st.SearchStrategy[Ex]] = None,
dtype: Any = None,
fill: Optional[st.SearchStrategy[Ex]] = None,
unique: bool = False, # noqa: FBT001,FBT002
) -> None:
super().__init__()
self.name = name
self.elements = elements
self.dtype = dtype
self.fill = fill
self.unique = unique


@overload
def columns(
names_or_number: Union[int, Sequence[str]],
*,
dtype: Any = ...,
elements: None = ...,
fill: None = ...,
unique: bool = ...,
) -> List[column[Any]]: ...


@overload
def columns(
names_or_number: Union[int, Sequence[str]],
*,
dtype: Any = ...,
elements: Optional[st.SearchStrategy[Ex]] = ...,
fill: Optional[st.SearchStrategy[Ex]] = ...,
unique: bool = ...,
) -> List[column[Ex]]: ...


def columns(
names_or_number: Union[int, Sequence[str]],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,9 @@ def do_draw(self, data):
return result

def __repr__(self):
return "{}({!r}, min_size={:_}, max_size={:_})".format(
self.__class__.__name__, self.element_strategy, self.min_size, self.max_size
return (
f"{self.__class__.__name__}({self.element_strategy!r}, "
f"min_size={self.min_size:_}, max_size={self.max_size:_})"
)

def filter(self, condition):
Expand Down
11 changes: 4 additions & 7 deletions hypothesis-python/src/hypothesis/strategies/_internal/numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,10 @@ def __init__(
self.smallest_nonzero_magnitude = smallest_nonzero_magnitude

def __repr__(self):
return "{}(min_value={}, max_value={}, allow_nan={}, smallest_nonzero_magnitude={})".format(
self.__class__.__name__,
self.min_value,
self.max_value,
self.allow_nan,
self.smallest_nonzero_magnitude,
)
return (
f"{self.__class__.__name__}({self.min_value=}, {self.max_value=}, "
f"{self.allow_nan=}, {self.smallest_nonzero_magnitude=})"
).replace("self.", "")

def do_draw(self, data):
return data.draw_float(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from functools import lru_cache
from random import shuffle
from typing import (
TYPE_CHECKING,
Any,
Callable,
ClassVar,
Expand Down Expand Up @@ -51,7 +52,15 @@
from hypothesis.strategies._internal.utils import defines_strategy
from hypothesis.utils.conventions import UniqueIdentifier

Ex = TypeVar("Ex", covariant=True)
if sys.version_info >= (3, 13):
Ex = TypeVar("Ex", covariant=True, default=Any)
elif TYPE_CHECKING:
from typing_extensions import TypeVar # type: ignore[assignment]

Ex = TypeVar("Ex", covariant=True, default=Any)
else:
Ex = TypeVar("Ex", covariant=True)

Ex_Inv = TypeVar("Ex_Inv")
T = TypeVar("T")
T3 = TypeVar("T3")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Version 2024021000, Last Updated Sat Feb 10 07:07:02 2024 UTC
# Version 2024031000, Last Updated Sun Mar 10 07:07:01 2024 UTC
AAA
AARP
ABB
Expand Down Expand Up @@ -510,7 +510,6 @@ GROUP
GS
GT
GU
GUARDIAN
GUCCI
GUGE
GUIDE
Expand Down
4 changes: 4 additions & 0 deletions hypothesis-python/tests/common/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from hypothesis import Phase, Verbosity, settings
from hypothesis._settings import not_set
from hypothesis.internal.conjecture.data import AVAILABLE_PROVIDERS
from hypothesis.internal.coverage import IN_COVERAGE_TESTS


Expand Down Expand Up @@ -61,3 +62,6 @@ def run():
settings.register_profile("debug", settings(verbosity=Verbosity.debug))

settings.load_profile(os.getenv("HYPOTHESIS_PROFILE", "default"))

for backend in set(AVAILABLE_PROVIDERS) - {"hypothesis"}:
settings.register_profile(backend, backend=backend) # e.g. "crosshair"
8 changes: 4 additions & 4 deletions requirements/coverage.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ numpy==1.26.4
# -r requirements/coverage.in
# pandas
# pyarrow
packaging==23.2
packaging==24.0
# via
# black
# pytest
Expand All @@ -59,9 +59,9 @@ pluggy==1.4.0
# via pytest
ptyprocess==0.7.0
# via pexpect
pyarrow==15.0.0
pyarrow==15.0.1
# via -r requirements/coverage.in
pytest==8.1.0
pytest==8.1.1
# via
# -r requirements/test.in
# pytest-xdist
Expand All @@ -77,7 +77,7 @@ pytz==2024.1
# pandas
pyyaml==6.0.1
# via libcst
redis==5.0.2
redis==5.0.3
# via fakeredis
six==1.16.0
# via python-dateutil
Expand Down
16 changes: 8 additions & 8 deletions requirements/fuzzing.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ coverage==7.4.3
# via
# -r requirements/coverage.in
# hypofuzz
dash==2.16.0
dash==2.16.1
# via hypofuzz
dash-core-components==2.0.0
# via dash
Expand All @@ -50,17 +50,17 @@ exceptiongroup==1.2.0 ; python_version < "3.11"
# pytest
execnet==2.0.2
# via pytest-xdist
fakeredis==2.21.1
fakeredis==2.21.2
# via -r requirements/coverage.in
flask==3.0.2
# via dash
hypofuzz==24.2.3
# via -r requirements/fuzzing.in
hypothesis[cli]==6.98.15
hypothesis[cli]==6.99.1
# via hypofuzz
idna==3.6
# via requests
importlib-metadata==7.0.1
importlib-metadata==7.0.2
# via dash
iniconfig==2.0.0
# via pytest
Expand Down Expand Up @@ -93,7 +93,7 @@ numpy==1.26.4
# -r requirements/coverage.in
# pandas
# pyarrow
packaging==23.2
packaging==24.0
# via
# black
# plotly
Expand All @@ -116,11 +116,11 @@ psutil==5.9.8
# via hypofuzz
ptyprocess==0.7.0
# via pexpect
pyarrow==15.0.0
pyarrow==15.0.1
# via -r requirements/coverage.in
pygments==2.17.2
# via rich
pytest==8.1.0
pytest==8.1.1
# via
# -r requirements/test.in
# hypofuzz
Expand All @@ -137,7 +137,7 @@ pytz==2024.1
# pandas
pyyaml==6.0.1
# via libcst
redis==5.0.2
redis==5.0.3
# via fakeredis
requests==2.31.0
# via
Expand Down
4 changes: 2 additions & 2 deletions requirements/test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ execnet==2.0.2
# via pytest-xdist
iniconfig==2.0.0
# via pytest
packaging==23.2
packaging==24.0
# via pytest
pexpect==4.9.0
# via -r requirements/test.in
pluggy==1.4.0
# via pytest
ptyprocess==0.7.0
# via pexpect
pytest==8.1.0
pytest==8.1.1
# via
# -r requirements/test.in
# pytest-xdist
Expand Down
20 changes: 10 additions & 10 deletions requirements/tools.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ decorator==5.1.1
# via ipython
distlib==0.3.8
# via virtualenv
django==5.0.2
django==5.0.3
# via -r requirements/tools.in
docutils==0.20.1
# via
Expand Down Expand Up @@ -89,13 +89,13 @@ idna==3.6
# requests
imagesize==1.4.1
# via sphinx
importlib-metadata==7.0.1
importlib-metadata==7.0.2
# via
# keyring
# twine
iniconfig==2.0.0
# via pytest
ipython==8.22.1
ipython==8.22.2
# via -r requirements/tools.in
isort==5.13.2
# via shed
Expand Down Expand Up @@ -133,7 +133,7 @@ mdurl==0.1.2
# via markdown-it-py
more-itertools==10.2.0
# via jaraco-classes
mypy==1.8.0
mypy==1.9.0
# via -r requirements/tools.in
mypy-extensions==1.0.0
# via
Expand All @@ -148,7 +148,7 @@ numpy==1.26.4
# via -r requirements/tools.in
ordered-set==4.1.0
# via pelican
packaging==23.2
packaging==24.0
# via
# black
# build
Expand All @@ -164,7 +164,7 @@ pelican[markdown]==4.9.1
# via -r requirements/tools.in
pexpect==4.9.0
# via ipython
pip-tools==7.4.0
pip-tools==7.4.1
# via -r requirements/tools.in
pkginfo==1.10.0
# via twine
Expand Down Expand Up @@ -200,9 +200,9 @@ pyproject-hooks==1.0.0
# via
# build
# pip-tools
pyright==1.1.352
pyright==1.1.353
# via -r requirements/tools.in
pytest==8.1.0
pytest==8.1.1
# via -r requirements/tools.in
python-dateutil==2.9.0.post0
# via
Expand Down Expand Up @@ -235,7 +235,7 @@ rich==13.7.1
# via
# pelican
# twine
ruff==0.3.0
ruff==0.3.2
# via -r requirements/tools.in
secretstorage==3.3.3
# via keyring
Expand Down Expand Up @@ -303,7 +303,7 @@ tomli==2.0.1
# pyproject-hooks
# pytest
# tox
tox==4.13.0
tox==4.14.1
# via -r requirements/tools.in
traitlets==5.14.1
# via
Expand Down

0 comments on commit 27393c9

Please sign in to comment.