-
Notifications
You must be signed in to change notification settings - Fork 4
Property side-effects with @lt.on_set #331
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
Open
rwb27
wants to merge
9
commits into
main
Choose a base branch
from
side-effects-for-properties
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0529ec9
First implementation of @lt.on_set
rwb27 832c3ea
Run `on_set` before validating.
rwb27 93a3671
Add documentation
rwb27 77f68cb
Tidy naming convention for `lt.on_set` functions.
rwb27 7e4535d
Parametrize test for on_set to add settings.
rwb27 8dfdf37
Implement suggestions from code review by @bprobert97
rwb27 5d66d6a
Attempt to improve one of the bullet points about `on_set`.
rwb27 0c0aef9
Add some checks on on_set functions
rwb27 3d7b9fa
Update tests/test_property.py
rwb27 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Are any errors or warning messages raised if the function doesn't return a value in the on_set function?
Might be helpful to a user, and we can write a test for it
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.
Currently, I think there should be an error if an
on_setfunction returns a value that's not valid against the property's model. That probably isn't very helpful in what it says, I should take a look.I don't think it's possible to distinguish between not returning a value and returning
None. IfNoneis a valid value, I don't think there's a sensible runtime check that would work.I will take a look at the validation error that gets raised and see whether it could have a helpful message added if there's an
on_setfunction present.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 have implemented (and tested) two checks:
mypyenforces that it has to return a value, ifmypyis in use.None. If that happens, I log a warning. I'm not 100% sure I like this - I would welcome thoughts.I suppose I could ask the function to return
(True, value)to make it easier to spot - but I'm not sure I like that either...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 like the checks that have been implemented. One other idea I had is to change what the function is expected to return, so that an implicit None becomes an immediate, catchable error (i.e. force all on-set functions to return a specific dataclass or something). Instead of asking users to make on_set return a raw value or None, we could require them to return a specific class.
Here is a kind of example:
SetValue(None) means "I explicitly want to set this to None." An implicit None raises a loud, helpful error. The downside is a slightly more verbose API for the user.
Just an idea, maybe not a useful one! I think we keep the return type-hinting check regardless, I think thats super useful
Uh oh!
There was an error while loading. Please reload this page.
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.
What about changing both the name and the mode of operation to look a bit like a Pydantic "wrap validator"?
lt.Setter[int]would be a callable accepting an int, and when it's called it would set the value. That way, it's clear which code executes before and after setting the value.The documentation ought to make it clear that you must either call the setter (exactly once) or raise an exception. That way, it's clear when we have a logic error. I think the new name is likely less confusing, and removes any ambiguity around when it's called in relation to the value changing.
PS I've edited this comment a few times to remove stream-of-consciousness wittering. Sorry if you saw a previous version.