diff --git a/hypothesis-python/RELEASE.rst b/hypothesis-python/RELEASE.rst new file mode 100644 index 0000000000..03575b3ee5 --- /dev/null +++ b/hypothesis-python/RELEASE.rst @@ -0,0 +1,3 @@ +RELEASE_TYPE: patch + +Clean up an internal helper. diff --git a/hypothesis-python/src/hypothesis/_settings.py b/hypothesis-python/src/hypothesis/_settings.py index 13c90373ed..6d100b7296 100644 --- a/hypothesis-python/src/hypothesis/_settings.py +++ b/hypothesis-python/src/hypothesis/_settings.py @@ -18,7 +18,6 @@ import datetime import inspect import os -import warnings from collections.abc import Collection, Generator, Sequence from enum import Enum, EnumMeta, unique from functools import total_ordering @@ -31,13 +30,13 @@ ) from hypothesis.errors import ( - HypothesisDeprecationWarning, InvalidArgument, ) from hypothesis.internal.conjecture.providers import AVAILABLE_PROVIDERS from hypothesis.internal.reflection import get_pretty_function_description from hypothesis.internal.validation import check_type, try_convert from hypothesis.utils.conventions import not_set +from hypothesis.utils.deprecation import note_deprecation from hypothesis.utils.dynamicvariables import DynamicVariable if TYPE_CHECKING: @@ -1191,20 +1190,6 @@ def local_settings(s: settings) -> Generator[settings, None, None]: yield s -def note_deprecation( - message: str, *, since: str, has_codemod: bool, stacklevel: int = 0 -) -> None: - if since != "RELEASEDAY": - date = datetime.date.fromisoformat(since) - assert datetime.date(2021, 1, 1) <= date - if has_codemod: - message += ( - "\n The `hypothesis codemod` command-line tool can automatically " - "refactor your code to fix this warning." - ) - warnings.warn(HypothesisDeprecationWarning(message), stacklevel=2 + stacklevel) - - default = settings( max_examples=100, derandomize=False, diff --git a/hypothesis-python/src/hypothesis/control.py b/hypothesis-python/src/hypothesis/control.py index 11f8223231..628ee5c6af 100644 --- a/hypothesis-python/src/hypothesis/control.py +++ b/hypothesis-python/src/hypothesis/control.py @@ -18,7 +18,6 @@ from weakref import WeakKeyDictionary from hypothesis import Verbosity, settings -from hypothesis._settings import note_deprecation from hypothesis.errors import InvalidArgument, UnsatisfiedAssumption from hypothesis.internal.compat import BaseExceptionGroup from hypothesis.internal.conjecture.data import ConjectureData @@ -26,6 +25,7 @@ from hypothesis.internal.reflection import get_pretty_function_description from hypothesis.internal.validation import check_type from hypothesis.reporting import report, verbose_report +from hypothesis.utils.deprecation import note_deprecation from hypothesis.utils.dynamicvariables import DynamicVariable from hypothesis.vendor.pretty import IDKey, PrettyPrintFunction, pretty diff --git a/hypothesis-python/src/hypothesis/database.py b/hypothesis-python/src/hypothesis/database.py index 9106a2d219..68568675f3 100644 --- a/hypothesis-python/src/hypothesis/database.py +++ b/hypothesis-python/src/hypothesis/database.py @@ -37,11 +37,11 @@ from urllib.request import Request, urlopen from zipfile import BadZipFile, ZipFile -from hypothesis._settings import note_deprecation from hypothesis.configuration import storage_directory from hypothesis.errors import HypothesisException, HypothesisWarning from hypothesis.internal.conjecture.choice import ChoiceT from hypothesis.utils.conventions import UniqueIdentifier, not_set +from hypothesis.utils.deprecation import note_deprecation __all__ = [ "DirectoryBasedExampleDatabase", diff --git a/hypothesis-python/src/hypothesis/errors.py b/hypothesis-python/src/hypothesis/errors.py index 0e8fa889df..daf81828c9 100644 --- a/hypothesis-python/src/hypothesis/errors.py +++ b/hypothesis-python/src/hypothesis/errors.py @@ -222,8 +222,8 @@ class Frozen(HypothesisException): def __getattr__(name: str) -> Any: if name == "MultipleFailures": - from hypothesis._settings import note_deprecation from hypothesis.internal.compat import BaseExceptionGroup + from hypothesis.utils.deprecation import note_deprecation note_deprecation( "MultipleFailures is deprecated; use the builtin `BaseExceptionGroup` type " diff --git a/hypothesis-python/src/hypothesis/extra/numpy.py b/hypothesis-python/src/hypothesis/extra/numpy.py index e78b0dd15d..0cf3efaea3 100644 --- a/hypothesis-python/src/hypothesis/extra/numpy.py +++ b/hypothesis-python/src/hypothesis/extra/numpy.py @@ -27,7 +27,6 @@ import numpy as np from hypothesis import strategies as st -from hypothesis._settings import note_deprecation from hypothesis.errors import HypothesisException, InvalidArgument from hypothesis.extra._array_helpers import ( _BIE, @@ -60,6 +59,7 @@ check_strategy, ) from hypothesis.strategies._internal.utils import defines_strategy +from hypothesis.utils.deprecation import note_deprecation def _try_import(mod_name: str, attr_name: str) -> Any: diff --git a/hypothesis-python/src/hypothesis/extra/pandas/impl.py b/hypothesis-python/src/hypothesis/extra/pandas/impl.py index 4d4c00f127..12da7370a6 100644 --- a/hypothesis-python/src/hypothesis/extra/pandas/impl.py +++ b/hypothesis-python/src/hypothesis/extra/pandas/impl.py @@ -19,7 +19,6 @@ import pandas from hypothesis import strategies as st -from hypothesis._settings import note_deprecation from hypothesis.control import reject from hypothesis.errors import InvalidArgument from hypothesis.extra import numpy as npst @@ -33,6 +32,7 @@ ) from hypothesis.strategies._internal.strategies import Ex, check_strategy from hypothesis.strategies._internal.utils import cacheable, defines_strategy +from hypothesis.utils.deprecation import note_deprecation try: from pandas.core.arrays.integer import IntegerDtype diff --git a/hypothesis-python/src/hypothesis/internal/conjecture/data.py b/hypothesis-python/src/hypothesis/internal/conjecture/data.py index 4ac53e9dd9..9ac26e1cba 100644 --- a/hypothesis-python/src/hypothesis/internal/conjecture/data.py +++ b/hypothesis-python/src/hypothesis/internal/conjecture/data.py @@ -70,6 +70,7 @@ from hypothesis.internal.observability import PredicateCounts from hypothesis.reporting import debug_report from hypothesis.utils.conventions import not_set +from hypothesis.utils.deprecation import note_deprecation from hypothesis.utils.threading import ThreadLocal if TYPE_CHECKING: @@ -81,7 +82,6 @@ def __getattr__(name: str) -> Any: if name == "AVAILABLE_PROVIDERS": - from hypothesis._settings import note_deprecation from hypothesis.internal.conjecture.providers import AVAILABLE_PROVIDERS note_deprecation( diff --git a/hypothesis-python/src/hypothesis/internal/conjecture/engine.py b/hypothesis-python/src/hypothesis/internal/conjecture/engine.py index 85b34f474f..3a1c1944b4 100644 --- a/hypothesis-python/src/hypothesis/internal/conjecture/engine.py +++ b/hypothesis-python/src/hypothesis/internal/conjecture/engine.py @@ -23,7 +23,7 @@ from typing import Literal, NoReturn, cast from hypothesis import HealthCheck, Phase, Verbosity, settings as Settings -from hypothesis._settings import local_settings, note_deprecation +from hypothesis._settings import local_settings from hypothesis.database import ExampleDatabase, choices_from_bytes, choices_to_bytes from hypothesis.errors import ( BackendCannotProceed, @@ -70,6 +70,7 @@ from hypothesis.internal.healthcheck import fail_health_check from hypothesis.internal.observability import Observation, with_observability_callback from hypothesis.reporting import base_report, report, verbose_report +from hypothesis.utils.deprecation import note_deprecation # In most cases, the following constants are all Final. However, we do allow users # to monkeypatch all of these variables, which means we cannot annotate them as diff --git a/hypothesis-python/src/hypothesis/internal/observability.py b/hypothesis-python/src/hypothesis/internal/observability.py index 84d5b51bf4..ad674f2265 100644 --- a/hypothesis-python/src/hypothesis/internal/observability.py +++ b/hypothesis-python/src/hypothesis/internal/observability.py @@ -51,6 +51,7 @@ from hypothesis.internal.escalation import InterestingOrigin from hypothesis.internal.floats import float_to_int from hypothesis.internal.intervalsets import IntervalSet +from hypothesis.utils.deprecation import note_deprecation if TYPE_CHECKING: from hypothesis.internal.conjecture.data import ConjectureData, Spans, Status @@ -355,8 +356,6 @@ def __bool__(self): return bool(_callbacks) def _note_deprecation(self): - from hypothesis._settings import note_deprecation - note_deprecation( "hypothesis.internal.observability.TESTCASE_CALLBACKS is deprecated. " "Replace TESTCASE_CALLBACKS.append with add_observability_callback, " diff --git a/hypothesis-python/src/hypothesis/stateful.py b/hypothesis-python/src/hypothesis/stateful.py index 8026d2d167..2103fdcb2c 100644 --- a/hypothesis-python/src/hypothesis/stateful.py +++ b/hypothesis-python/src/hypothesis/stateful.py @@ -28,12 +28,7 @@ from unittest import TestCase from hypothesis import strategies as st -from hypothesis._settings import ( - HealthCheck, - Verbosity, - note_deprecation, - settings as Settings, -) +from hypothesis._settings import HealthCheck, Verbosity, settings as Settings from hypothesis.control import _current_build_context, current_build_context from hypothesis.core import TestFunc, given from hypothesis.errors import ( @@ -62,6 +57,7 @@ SearchStrategy, check_strategy, ) +from hypothesis.utils.deprecation import note_deprecation from hypothesis.vendor.pretty import RepresentationPrinter T = TypeVar("T") diff --git a/hypothesis-python/src/hypothesis/strategies/_internal/core.py b/hypothesis-python/src/hypothesis/strategies/_internal/core.py index 5aaf0fe497..4a8224cc9c 100644 --- a/hypothesis-python/src/hypothesis/strategies/_internal/core.py +++ b/hypothesis-python/src/hypothesis/strategies/_internal/core.py @@ -45,7 +45,6 @@ ) from uuid import UUID -from hypothesis._settings import note_deprecation from hypothesis.control import ( cleanup, current_build_context, @@ -138,6 +137,7 @@ ) from hypothesis.strategies._internal.utils import cacheable, defines_strategy from hypothesis.utils.conventions import not_set +from hypothesis.utils.deprecation import note_deprecation from hypothesis.vendor.pretty import RepresentationPrinter diff --git a/hypothesis-python/src/hypothesis/utils/deprecation.py b/hypothesis-python/src/hypothesis/utils/deprecation.py new file mode 100644 index 0000000000..e27e2cf9c1 --- /dev/null +++ b/hypothesis-python/src/hypothesis/utils/deprecation.py @@ -0,0 +1,28 @@ +# This file is part of Hypothesis, which may be found at +# https://github.com/HypothesisWorks/hypothesis/ +# +# Copyright the Hypothesis Authors. +# Individual contributors are listed in AUTHORS.rst and the git log. +# +# This Source Code Form is subject to the terms of the Mozilla Public License, +# v. 2.0. If a copy of the MPL was not distributed with this file, You can +# obtain one at https://mozilla.org/MPL/2.0/. + +import datetime +import warnings + +from hypothesis.errors import HypothesisDeprecationWarning + + +def note_deprecation( + message: str, *, since: str, has_codemod: bool, stacklevel: int = 0 +) -> None: + if since != "RELEASEDAY": + date = datetime.date.fromisoformat(since) + assert datetime.date(2021, 1, 1) <= date + if has_codemod: + message += ( + "\n The `hypothesis codemod` command-line tool can automatically " + "refactor your code to fix this warning." + ) + warnings.warn(HypothesisDeprecationWarning(message), stacklevel=2 + stacklevel) diff --git a/hypothesis-python/tests/cover/test_settings.py b/hypothesis-python/tests/cover/test_settings.py index 5cac554320..754d04dd63 100644 --- a/hypothesis-python/tests/cover/test_settings.py +++ b/hypothesis-python/tests/cover/test_settings.py @@ -26,7 +26,6 @@ Verbosity, default_variable, local_settings, - note_deprecation, settings, ) from hypothesis.database import InMemoryExampleDatabase @@ -36,6 +35,7 @@ ) from hypothesis.stateful import RuleBasedStateMachine, rule from hypothesis.utils.conventions import not_set +from hypothesis.utils.deprecation import note_deprecation from tests.common.utils import ( checks_deprecated_behaviour, @@ -381,22 +381,6 @@ def test_deadline_given_valid_timedelta(): assert x.microseconds == 30000 -@pytest.mark.parametrize( - "x", - [ - 0, - -0.7, - -1, - 86400000000000000.2, - datetime.timedelta(microseconds=-1), - datetime.timedelta(0), - ], -) -def test_invalid_deadline(x): - with pytest.raises(InvalidArgument): - settings(deadline=x) - - @pytest.mark.parametrize("value", ["always"]) def test_can_not_set_print_blob_to_non_print_settings(value): with pytest.raises(InvalidArgument): @@ -467,6 +451,10 @@ def test_derandomise_with_explicit_database_is_invalid(): {"stateful_step_count": 2.5}, {"deadline": -1}, {"deadline": 0}, + {"deadline": -0.7}, + {"deadline": 86400000000000000.2}, + {"deadline": datetime.timedelta(microseconds=-1)}, + {"deadline": datetime.timedelta(0)}, {"deadline": True}, {"deadline": False}, {"backend": "nonexistent_backend"}, @@ -489,11 +477,9 @@ def __repr__(self): not_settings = NotSettings() - with pytest.raises(InvalidArgument) as excinfo: + with pytest.raises(InvalidArgument, match=r"parent=\(not settings repr\)"): settings(not_settings) - assert "parent=(not settings repr)" in str(excinfo.value) - def test_default_settings_do_not_use_ci(): assert settings.get_profile("default").suppress_health_check == ()