-
Notifications
You must be signed in to change notification settings - Fork 422
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
Validate declarative interface attributes #2170
Conversation
Right now, typos in the names of attributes used by the declarative plotting interface lead to strange errors that can be difficult to debug. In this commit, a new mixin class is defined to provide validation of attribute names against a whitelist. This approach was inspired by a Stack Overflow question: __setattr__ versus __slots__ for constraining attribute creation... and the nearest-string function was stolen from Rosetta Code. Since this is a proof of concept, validation is only applied to the MapPanel object.
The test works on my machine. Not sure what module is not being found. I suppose it might be the lack of a |
cdaf2d1
to
03d46dd
Compare
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.
Thanks for this PR @sgdecker, this has definitely been a pain point for users and would be great to give better error statements to help the users find the right path, rather than either an indecipherable error or silently fail to do what the user expected.
A couple of suggestions including not using the term 'whitelist', I've suggested an alternative, but there are other potential options as well. In addition, there is a function from difflib
that might work to our advantage here instead of a custom definition.
Ping @dcamron since you worked on another shot at this, at least on the disallowing unknown attributes side. |
In this commit, the ValidationMixin is applied to all of the objects in the declarative plotting interface intended for external use.
03d46dd
to
af035bb
Compare
Looking good and glad the built-in function works well - it does simplify the code a lot. I'm wondering if we could add one more test with some out there traitlet to test the else portion of the if statement? Something like |
The Python standard module difflib contains a function we can use to replace the function stolen from Rosetta Code. If the case where the attribute name is incorrect, but it is reasonably close to a correct name, that name is suggested to the user. Otherwise, no suggestion is made.
Tests are provided that test the case where a suggestion to fix a possible typo is made as well as the case where no suggestion is offered.
af035bb
to
9c79896
Compare
Great idea. I have added a second test and made the original test more specific. |
Looks good on my end now. This will be a definite improvement for the users. (Don't know why the Docs are failing on 3.9) |
I've come around to reviewing this, and I think the implementation is mostly solid. However, I did just arrive at a thought to ask @kgoebber @dopplershift @sgdecker: do we actually want this hard-and-fast-disallow behavior? Would it be still helpful but more flexible to check and only warn on setting "invalid" attributes instead of outright refusing them? |
The reason I am in favor of a hard-and-fast disallow is that if you slight mistype an attribute (say by adding an extra s to an attribute name) there are numerous times you get silent failures (still get a map produced) and many newer programmers are less likely to read a warning message if a map still gets produced. The hard disallow would force them to read the error message to then successfully produce a map and since we give a direct suggestion (in what would likely be the vast majority of cases) then the end user should be able to easily correct the error. |
I agree with @kgoebber . If there was some use case where assigning to additional attributes would make sense, I could see a warning being OK, but I can't see one. A great example of a silent failure one of my students recently experienced, was where he wrote something along the lines of In cases where the incorrectly named attribute does trigger an error already (like the original example), I would be worried about a warning being lost among a sea of text from a long traceback message. |
I'm in agreement with the above. The only use case for a user adding their own attributes (which wouldn't trigger any behavior on their own) is some kind of weird monkey-patching (like we do with |
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.
Thanks for this @sgdecker. I have some thoughts about how we can reorganize this a bit to make Declarative more robust in the future, but I want to get those thoughts to you with some time to respond. We definitely prefer to get this in with the release we want to cut this week! I will open up an issue capturing some of my thoughts and we can try to get that in for the next release.
This PR addresses a portion of #2016
Right now, typos in the names of attributes used by the declarative
plotting interface lead to strange errors that can be difficult to
debug. Here, a new mixin class is defined to provide
validation of attribute names against a whitelist.
This approach was inspired by a Stack Overflow question
and the nearest-string function was stolen from Rosetta Code.
Since this is a proof of concept, validation is only applied to theMapPanel object.
Validation is extended to all publicly facing objects in the decalarative interface, and a test has been added.
Here is a motivating example:
Output w/o the PR:
Output w/ the PR: