Don't create default values for Instance until required#31
Don't create default values for Instance until required#31minrk merged 5 commits intoipython:masterfrom
Conversation
This is an alternative to ipython#23 and ipython#28 There is already a mechanism for dynamic default values with _foo_default methods, which are not called unless needed. This makes the default values for Instance traitlets use that same mechanism, avoiding instantiating classes unless those traitlets are explicitly requested without being assigned first. Along the way, two other changes to behaviour, and hence to tests, came in. Dict traitlets are based on Instance, so their behaviour changes in the same way. And a Type() traitlet with no class specified now gets a default value of object instead of None, because the former is valid when allow_none is True. It was also possible to clean up some parts of the machinery.
traitlets/traitlets.py
Outdated
There was a problem hiding this comment.
N.B. Previously _trait_dyn_inits was a mapping of trait name to method name on the object. Now it can contain either a method name or a callable. It's private, and this is the only place we use values from it, I believe.
There was a problem hiding this comment.
Switching behaviors based on if isinstance statements is not very object-oriented IMO.
|
The problem with calling That is why I created the |
|
I did notice that that was odd, but it doesn't actually cause any problems - |
|
In |
|
With the last commit, traits that are only used for validation (inside Containers or Unions) do not get a name set, and do not look at methods or the trait dictionaries on the HasTraits instance. If I were designing this from scratch, I'd try to more cleanly separate the validation parts of traitlets from the descriptor parts (notification, |
I prefer having clearly two kinds of |
|
|
|
|
@takluyver thanks, I like the new version much more. |
|
Great. I'd like @minrk to have a look over this when he has a chance. Then hopefully we can get traitlets 4.0 out, and start preparing the next level of things. |
|
I'll make sure to have a look tomorrow. |
There was a problem hiding this comment.
deprecation warning on the deprecated method?
|
I made one comment about deprecation, but I think this looks sensible. Do we close #28 if we merge this? |
|
Sure, no problem for me. |
|
Added a warning. I wonder about also deprecating |
|
I'm not aware of any. Should we merge here, and let that question be another issue? |
|
Sounds like a plan. Created issue #32 for that deprecation issue. |
|
Okay, then. |
Don't create default values for Instance until required
|
@takluyver This might have broken the widgets (cross validation of the slider values). See jupyter-widgets/ipywidgets#52. |
Closes ipythongh-32 I also removed validate_default_value(), which I just added in PR ipython#31. Its implementation is a single line, so I decided it was clearer to inline it where it's used.
|
Looks like the issue is that max tries to validate against min, but looking up the default value of min triggers a validation of that, which checks max, and so ad infinitum. I'm guessing if #33 is merged and widgets updated to use it, the error will go away. However, I think I know the change that caused this. Before, static default values were transferred into |
Should fix the problem introduced in ipythongh-31 for the widgets. One small behavioural change: traitlets with no default value will raise TraitError when accessed instead of returning Undefined.
Closes ipythongh-32 I also removed validate_default_value(), which I just added in PR ipython#31. Its implementation is a single line, so I decided it was clearer to inline it where it's used.
This is an alternative to #23 and #28
There is already a mechanism for dynamic default values with
_foo_defaultmethods, which are not called unless needed. This makes the default values for Instance traitlets use that same mechanism, avoiding instantiating classes unless those traitlets are explicitly requested without being assigned first.Along the way, two other changes to behaviour, and hence to tests, came in. Dict traitlets are based on Instance, so their behaviour changes in the same way. And a
Type()traitlet with no class specified now gets a default value of object instead of None, because the former is valid when allow_none is False.It was also possible to clean up some parts of the machinery, leading to a reduction in total line count.