Skip to content

_setup_dynamic_initializer simplified#28

Closed
SylvainCorlay wants to merge 3 commits intoipython:masterfrom
SylvainCorlay:default_initializers
Closed

_setup_dynamic_initializer simplified#28
SylvainCorlay wants to merge 3 commits intoipython:masterfrom
SylvainCorlay:default_initializers

Conversation

@SylvainCorlay
Copy link
Member

_setup_dynamic_initializer used to return a boolean on whether there is a dynamic initializer for a given trait. It does not anymore.

Two remarks though:

  • instance_init is supposed to correspond to the part or the initialization that depends on the HasTraits instance.
    All of what is currently in the implementation of instance_init for our trait types does not actually depend on obj. It could simply be part of __init__. There is no real reason to keep it there.
  • After we move this, the call to _setup_dynamic_initializer could be moved to TraitType.instance_init.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dislike this change - it makes the API more stateful, because _set_default_value_at_instance_init() now relies on _setup_dynamic_initializer having been called first. But in #23, you remove this method entirely. The two PRs will conflict - what is the relationship between them?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I already rebased the #23 on this PR. Regarding statefulness, _trait_dyn_inits is also used in __get__.

The task of _setup_dynamic_initializer is to set this variable.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, should we just be reviewing #23 as a single unit now?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SylvainCorlay
Copy link
Member Author

Thanks for looking at this. I guess I am trying to split things too much into small changes to get them in.

So here is the idea: instance_init is supposed to handle the part of the initialization which may depend on the underlying HasTraits instance. What _setup_dynamic_initializer does falls very much into that category. Therefore, it should probably be called in instance_init.

One issue is that instance_init is also called by descriptors holding other descriptors like List(Int()), and we don't want these to be set up by dynamic initializers... Hence there should maybe be two levels for such initialization: one called at the top level only, i.e. called in HasTraits.__new__, which does things like setting default initializers and calls the other one that only does the part necessary for sub-traits.

@SylvainCorlay SylvainCorlay force-pushed the default_initializers branch 3 times, most recently from e3d6f08 to 2c6056d Compare June 9, 2015 14:46
@SylvainCorlay SylvainCorlay force-pushed the default_initializers branch from 2c6056d to 95b9470 Compare June 9, 2015 14:54
@takluyver
Copy link
Member

As mentioned on #23, I'm not happy about a simplification PR that adds a new public API method. From the diff, this does not now appear to make the code simpler - has what you're trying to achieve changed?

@SylvainCorlay
Copy link
Member Author

Too me, having if isinstance statements is a sign of bad design. It makes sense in the context of metaclass magics, but here it is better solved and more extensible with polymorphism.

takluyver added a commit to takluyver/traitlets that referenced this pull request Jun 12, 2015
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.
@minrk minrk modified the milestone: no action Nov 9, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants