-
Notifications
You must be signed in to change notification settings - Fork 79
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
model fitting: handle empty poly order input #1041
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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 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 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 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from traitlets import Int, Float | ||
|
||
|
||
class HandleEmptyMixin: | ||
def __init__(self, *args, **kwargs): | ||
self._empty_to_default = kwargs.pop('replace_with_default', False) | ||
super().__init__(*args, **kwargs) | ||
|
||
def validate(self, obj, value): | ||
if isinstance(value, str) and not len(value): | ||
if self._empty_to_default: | ||
# If the field is emptied, it will override with the default value. | ||
return self.default_value | ||
# The value will remain as the empty string, likely will need to either | ||
# couple this with form validation or handle the case where the value | ||
# is an empty string once retrieved. | ||
return value | ||
return super().validate(obj, value) | ||
|
||
|
||
class IntHandleEmpty(HandleEmptyMixin, Int): | ||
pass | ||
|
||
|
||
class FloatHandleEmpty(HandleEmptyMixin, Float): | ||
pass |
Empty file.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import pytest | ||
from traitlets import HasTraits, TraitError | ||
|
||
from jdaviz.core.custom_traitlets import IntHandleEmpty, FloatHandleEmpty | ||
|
||
|
||
class Foo(HasTraits): | ||
int_handle_empty = IntHandleEmpty(0) | ||
int_handle_empty_replace = IntHandleEmpty(0, replace_with_default=True) | ||
float_handle_empty = FloatHandleEmpty() | ||
|
||
|
||
def test_inthandleempty(): | ||
foo = Foo() | ||
|
||
foo.int_handle_empty = 1 | ||
foo.int_handle_empty = '' | ||
assert foo.int_handle_empty == '' | ||
with pytest.raises( | ||
TraitError, | ||
match=r"The 'int_handle_empty' trait of a Foo instance expected an int, not the str 'blah'\."): # noqa | ||
foo.int_handle_empty = 'blah' | ||
|
||
foo.int_handle_empty_replace = 1 | ||
foo.int_handle_empty_replace = '' | ||
assert foo.int_handle_empty_replace == 0 | ||
|
||
foo.float_handle_empty = 1.2 |
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.
Is this order done such that our validate would overwrite the validate from upstream? Just curious.
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.
yes, this uses the new
validate
first (which then falls back on thesuper
validate on the upstreamInt
/Float
traitlets if the value isn't an empty string).