Skip to content

Commit

Permalink
Assigning settings on the class is an error
Browse files Browse the repository at this point in the history
You probably meant to assign them on an (or the default) *instance*.
  • Loading branch information
Zac-HD committed Sep 16, 2018
1 parent c4840df commit 71cb648
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
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

To adjust the current settings profile, you can assign to attributes of the
current settings *instance* ``hypothesis.settings.default``. This release
makes it an error to omit the ``.default``, because assigning to attributes
of ``hypothesis.settings`` had no effect whatsoever - except to confuse
users about their settings!
18 changes: 14 additions & 4 deletions hypothesis-python/src/hypothesis/_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,23 @@ 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 setattr %s=%r on class hypothesis.settings - did '
'you mean the current instance, hypothesis.settings.default?'
% (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

0 comments on commit 71cb648

Please sign in to comment.