Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically reload settings profile if it's redefined #4153

Merged
merged 3 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions hypothesis-python/RELEASE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
RELEASE_TYPE: minor

This changes the behaviour of settings profiles so that if you reregister the currently loaded profile it will automatically reload it. Previously you would have had to load it again.

In particular this means that if you register a "ci" profile, it will automatically be used when Hypothesis detects you are running on CI.
15 changes: 15 additions & 0 deletions hypothesis-python/docs/settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,21 @@ Hypothesis will automatically detect certain common CI environments and use the
when running in them.
In particular, if you wish to use the ``ci`` profile, setting the ``CI`` environment variable will do this.

If you want to customise your CI behaviour, registering a new version of the ``ci`` profile will automatically be picked up in CI. For example, if you wanted to run more examples in CI, you might configure it as follows:


.. code-block:: python

settings.register_profile(
"ci",
settings(
settings.get_profile("ci"),
max_examples=1000,
),
)

This will configure your CI to run 1000 examples per test rather than the default of 100, and this change will automatically be picked up when running on a CI server.

.. _healthchecks:

-------------
Expand Down
7 changes: 5 additions & 2 deletions hypothesis-python/src/hypothesis/_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def default(cls):
v = default_variable.value
if v is not None:
return v
if hasattr(settings, "_current_profile"):
if getattr(settings, "_current_profile", None) is not None:
settings.load_profile(settings._current_profile)
assert default_variable.value is not None
return default_variable.value
Expand Down Expand Up @@ -132,6 +132,7 @@ class settings(metaclass=settingsMeta):
__definitions_are_locked = False
_profiles: ClassVar[dict[str, "settings"]] = {}
__module__ = "hypothesis"
_current_profile = None

def __getattr__(self, name):
if name in all_settings:
Expand Down Expand Up @@ -319,6 +320,8 @@ def register_profile(
"""
check_type(str, name, "name")
settings._profiles[name] = settings(parent=parent, **kwargs)
if settings._current_profile == name:
settings.load_profile(name)

@staticmethod
def get_profile(name: str) -> "settings":
Expand Down Expand Up @@ -768,7 +771,7 @@ def note_deprecation(


# This is tested in a subprocess so the branch doesn't show up in coverage.
if is_in_ci(): # pragma: no cover
if is_in_ci(): # pragma: no
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Also apparently unneeded given that coverage tests aren't failing!

settings.load_profile("ci")

assert settings.default is not None
Expand Down
37 changes: 37 additions & 0 deletions hypothesis-python/tests/cover/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -558,3 +558,40 @@ def test_check_defaults_to_randomize_when_not_running_on_ci():
).strip()
== "False"
)


def test_reloads_the_loaded_profile_if_registered_again():
prev_profile = settings._current_profile
try:
test_profile = "some nonsense profile purely for this test"
test_value = 123456
settings.register_profile(test_profile, settings(max_examples=test_value))
settings.load_profile(test_profile)
assert settings.default.max_examples == test_value
test_value_2 = 42
settings.register_profile(test_profile, settings(max_examples=test_value_2))
assert settings.default.max_examples == test_value_2
finally:
if prev_profile is not None:
settings.load_profile(prev_profile)


CI_TESTING_SCRIPT = """
from hypothesis import settings

if __name__ == '__main__':
settings.register_profile("ci", settings(max_examples=42))
assert settings.default.max_examples == 42
"""


@skipif_emscripten
def test_will_automatically_pick_up_changes_to_ci_profile_in_ci():
env = dict(os.environ)
env["CI"] = "true"
subprocess.check_call(
[sys.executable, "-c", CI_TESTING_SCRIPT],
env=env,
text=True,
encoding="utf-8",
)
Loading