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

Make assigning attributes of the settings class an error #1572

Merged
merged 1 commit into from
Sep 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 7 additions & 0 deletions hypothesis-python/RELEASE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
RELEASE_TYPE: minor

This release makes setting attributes of the :class:`hypothesis.settings`
class an explicit error. This has never had any effect, but could mislead
users who confused it with the current settings *instance*
``hypothesis.settings.default`` (which is also immutable). You can change
the global settings with :ref:` settings profiles<settings_profiles>`.
19 changes: 15 additions & 4 deletions hypothesis-python/src/hypothesis/_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,24 @@ def default(self):
assert default_variable.value is not None
return default_variable.value

@default.setter
def default(self, value):
raise AttributeError('Cannot assign settings.default')

def _assign_default_internal(self, value):
default_variable.value = value

def __setattr__(self, name, value):
if name == 'default':
raise AttributeError(
'Cannot assign to the property settings.default - '
'consider using settings.load_profile instead.'
)
elif not (isinstance(value, settingsProperty) or name.startswith('_')):
raise AttributeError(
'Cannot assign hypothesis.settings.%s=%r - the settings '
'class is immutable. You can change the global default '
'settings with settings.load_profile, or use @settings(...) '
'to decorate your test instead.' % (name, value)
)
return type.__setattr__(self, name, value)


class settings(
settingsMeta('settings', (object,), {}) # type: ignore
Expand Down
7 changes: 7 additions & 0 deletions hypothesis-python/tests/cover/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,3 +394,10 @@ def test_setting_use_coverage_is_deprecated(value):
def test_settings_apply_for_explicit_examples(x):
# Regression test for #1521
assert settings.default.verbosity == Verbosity.verbose


def test_setattr_on_settings_singleton_is_error():
# https://github.com/pandas-dev/pandas/pull/22679#issuecomment-420750921
# Should be setting attributes on settings.default, not settings!
with pytest.raises(AttributeError):
settings.max_examples = 10