-
Notifications
You must be signed in to change notification settings - Fork 594
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
Add explicit error for assigning to settings attribute of state machine #1659
Add explicit error for assigning to settings attribute of state machine #1659
Conversation
class StateMachineMeta(type):
def __init__(self, *args, **kwargs):
super(StateMachineMeta, self).__init__(*args, **kwargs)
def __setattr__(self, name, value):
if name == 'settings' and isinstance(value, Settings):
raise AttributeError(
'Assigning `{cls}.settings = {value} does nothing. Assign '
'to {cls}.TestCase.settings, or use @{value} as a decorator '
'on the {cls} class.'.format(cls=self.__name__, value=value)
)
return type.__setattr__(self, name, value)
class GenericStateMachine(
StateMachineMeta('GenericStateMachine', (object,), {}) # type: ignore
): (and here's how you enable syntax highlighting for code blocks) Otherwise looks good, and having a test is great! |
@@ -146,8 +146,8 @@ def __setattr__(self, name, value): | |||
|
|||
|
|||
class GenericStateMachine( | |||
GenericStateMachineMeta('GenericStateMachine', | |||
(object,), {}) # type: ignore | |||
GenericStateMachineMeta('GenericStateMachine', # type: ignore |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤦♂️ I need two spaces here!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And only four at the start of the line, but Mypy doesn't care about either of those.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure how I missed that! Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nearly there!
|
||
def test_assigning_to_settings_attribute_on_state_machine_raises_error(): | ||
with pytest.raises(AttributeError): | ||
class StateMachine(RuleBasedStateMachine): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use class StateMachine(GenericStateMachine):
to avoid the class-defines-no-rules error.
hypothesis-python/RELEASE.rst
Outdated
:obj:`hypothesis.stateful.GenericStateMachine` and | ||
:obj:`hypothesis.stateful.RuleBasedStateMachine` now raise an explicit error | ||
when instances of :obj:`hypothesis.settings` are assigned to the classes' | ||
settings attribute. Error directs users to use correct assignment methods. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of saying that the error directs users to correct assignment methods, we should just do that here too. e.g.
...classes' settings attribute, which is a no-op (:issue:`1643`). Instead, users should assign to ``SomeStateMachine.TestCase.settings``, or use ``@settings(...)`` as a class decorator to handle this automatically.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That reads a lot better than what I wrote. I think I forgot my audience a bit when I was writing these notes haha
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM - I'll merge later today once the build is finished 🎉
Thanks Zac! And thanks for all the help along the way! |
This PR adds an explicit error to help prevent users from mistakenly assigning settings to the settings attribute of their state machine class. Ex:
I'm getting a strange error on when I run
./build.sh check-coverage
locally. I'm interested to see if this error shows up in CI.Closes #1643.