Skip to content

Commit

Permalink
Merge pull request #3610 from reaganjlee/deprecation-codemod
Browse files Browse the repository at this point in the history
  • Loading branch information
Zac-HD committed Apr 16, 2023
2 parents a136cb4 + 87435cc commit f8108ba
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 40 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: minor

This release deprecates ``Healthcheck.all()``, and :ref:`adds a codemod <codemods>`
to automatically replace it with ``list(Healthcheck)`` (:issue:`3596`).
2 changes: 1 addition & 1 deletion hypothesis-python/docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9742,7 +9742,7 @@ minor warts ranging from indirect imports to typos in comments.
3.38.0 - 2017-11-18
-------------------

This release overhauls :doc:`the health check system <healthchecks>`
This release overhauls :ref:`the health check system <healthchecks>`
in a variety of small ways.
It adds no new features, but is nevertheless a minor release because it changes
which tests are likely to fail health checks.
Expand Down
2 changes: 1 addition & 1 deletion hypothesis-python/docs/django.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Because Hypothesis runs this in a loop, the performance problems it normally has
are significantly exacerbated and your tests will be really slow.
If you are using :class:`~hypothesis.extra.django.TransactionTestCase`,
you may need to use ``@settings(suppress_health_check=[HealthCheck.too_slow])``
to avoid :doc:`errors due to slow example generation </healthchecks>`.
to avoid :ref:`errors due to slow example generation <healthchecks>`.

Having set up a test class, you can now pass :func:`@given <hypothesis.given>`
a strategy for Django models:
Expand Down
35 changes: 0 additions & 35 deletions hypothesis-python/docs/healthchecks.rst

This file was deleted.

1 change: 0 additions & 1 deletion hypothesis-python/docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ check out some of the
ghostwriter
django
numpy
healthchecks
database
stateful
supported
Expand Down
21 changes: 21 additions & 0 deletions hypothesis-python/docs/settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,24 @@ by your conftest you can load one with the command line option ``--hypothesis-pr
.. code:: bash
$ pytest tests --hypothesis-profile <profile-name>
.. _healthchecks:

-------------
Health checks
-------------

Hypothesis' health checks are designed to detect and warn you about performance
problems where your tests are slow, inefficient, or generating very large examples.

If this is expected, e.g. when generating large arrays or dataframes, you can selectively
disable them with the :obj:`~hypothesis.settings.suppress_health_check` setting.
The argument for this parameter is a list with elements drawn from any of
the class-level attributes of the HealthCheck class.
Using a value of ``list(HealthCheck)`` will disable all health checks.

.. autoclass:: hypothesis.HealthCheck
:undoc-members:
:inherited-members:
:exclude-members: all
20 changes: 20 additions & 0 deletions hypothesis-python/docs/supported.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,26 @@ noted as provisional, in which case they may be changed in minor releases.
Undocumented attributes, modules, and behaviour may include breaking
changes in patch releases.


.. _deprecation-policy:

------------
Deprecations
------------

Deprecated features will emit warnings for at least six
months, and then be removed in the following major release.

Note however that not all warnings are subject to this grace period;
sometimes we strengthen validation by adding a warning and these may
become errors immediately at a major release.

We use custom exception and warning types, so you can see
exactly where an error came from, or turn only our warnings into errors.

.. autoclass:: hypothesis.errors.HypothesisDeprecationWarning


---------------
Python versions
---------------
Expand Down
1 change: 1 addition & 0 deletions hypothesis-python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ def local_file(name):
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: Python :: Implementation :: PyPy",
"Topic :: Education :: Testing",
Expand Down
6 changes: 5 additions & 1 deletion hypothesis-python/src/hypothesis/_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,11 @@ def __repr__(self):
@classmethod
def all(cls) -> List["HealthCheck"]:
# Skipping of deprecated attributes is handled in HealthCheckMeta.__iter__
# TODO: note_deprecation() and write a codemod for HC.all() -> list(HC)
note_deprecation(
f"`Healthcheck.all()` is deprecated; use `list(HealthCheck)` instead.",
since="RELEASEDAY",
has_codemod=True,
)
return list(HealthCheck)

data_too_large = 1
Expand Down
13 changes: 13 additions & 0 deletions hypothesis-python/src/hypothesis/extra/codemods.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,16 @@ def leave_Call(self, original_node, updated_node):
for p, arg in zip(params, updated_node.args)
]
return updated_node.with_changes(args=newargs)


class HypothesisFixHealthcheckAll(VisitorBasedCodemodCommand):
"""Replace Healthcheck.all() with list(Healthcheck)"""

DESCRIPTION = "Replace Healthcheck.all() with list(Healthcheck)"

@m.leave(m.Call(func=m.Attribute(m.Name("Healthcheck"), m.Name("all")), args=[]))
def replace_healthcheck(self, original_node, updated_node):
return updated_node.with_changes(
func=cst.Name("list"),
args=[cst.Arg(value=cst.Name("Healthcheck"))],
)
16 changes: 16 additions & 0 deletions hypothesis-python/tests/codemods/test_codemods.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,19 @@ def test_noop_with_too_many_arguments_passed(self) -> None:
st.sets(st.integers(), 0, 1, True)
"""
self.assertCodemod(before=before, after=before)


class TestHealthcheckAll(CodemodTest):
TRANSFORM = codemods.HypothesisFixHealthcheckAll

def test_noop_other_attributes(self):
# Test that calls to other attributes of Healthcheck are not modified
before = "result = Healthcheck.data_too_large"
self.assertCodemod(before=before, after=before)

def test_substitution(self) -> None:
# Test that Healthcheck.all() is replaced with list(Healthcheck)
before = "result = Healthcheck.all()"
after = "result = list(Healthcheck)"
# self.assertEqual(run_codemod(input_code), expected_code)
self.assertCodemod(before=before, after=after)
8 changes: 7 additions & 1 deletion hypothesis-python/tests/cover/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@
from hypothesis.stateful import RuleBasedStateMachine, rule
from hypothesis.utils.conventions import not_set

from tests.common.utils import counts_calls, fails_with, validate_deprecation
from tests.common.utils import (
checks_deprecated_behaviour,
counts_calls,
fails_with,
validate_deprecation,
)


def test_has_docstrings():
Expand Down Expand Up @@ -492,6 +497,7 @@ def test_deprecated_settings_warn_on_set_settings():
settings(suppress_health_check=[HealthCheck.not_a_test_method])


@checks_deprecated_behaviour
def test_deprecated_settings_not_in_settings_all_list():
al = HealthCheck.all()
ls = list(HealthCheck)
Expand Down

0 comments on commit f8108ba

Please sign in to comment.