Skip to content

Added support for setting ObjectSelector.objects#440

Closed
jbednar wants to merge 2 commits into
mainfrom
objselector
Closed

Added support for setting ObjectSelector.objects#440
jbednar wants to merge 2 commits into
mainfrom
objselector

Conversation

@jbednar
Copy link
Copy Markdown
Member

@jbednar jbednar commented Sep 29, 2020

Addresses #398 and possibly #331 .

ObjectSelector was originally designed only to store a list of objects, which works fine if the objects are strings or have a .name attribute, but otherwise a GUI needs to work with names for the objects, and has to get them somewhere. To support this case, ObjectSelector was previously extended to accept a dict of name,obj pairs as the objects argument to the constructor, but then extracted only the objects and stored it in self.objects, while storing the mapping in self.names (a lookup table that needs to be mappable both ways for GUI purposes, to give the label from the current value or the new value from a new label). This worked, but caused problems for anyone who wanted to dynamically adjust the objects after the Parameter was constructed.

With this PR, the behavior is the same if a user sets .objects after construction as it is during construction; in both cases the object values are extracted (now storing in ._objects) and the names mapping is stored in self.names. This way a user can update the objects list whenever they like by passing a new dictionary.

It also supports cases where people have done a manual workaround like

        self.param.x.names = new_opts
        self.param.x.objects = list(new_opts.values())

by keeping the old names if a list is provided and the old names are compatible (have the same length). This special case could be removed now, but it would affect people who had used the previously required workaround.

Note that this PR does not address people trying to do self.param.x.objects.append(...). Anyone have ideas about whether that could be made to work?

@jbednar
Copy link
Copy Markdown
Member Author

jbednar commented Sep 30, 2020

I think this PR is a net improvement over the current behavior, but I also think we should consider an alternative approach where we simply accept that .objects will either be a dict or a list, and we consistently handle both those cases separately throughout the code. That way a user could do .append safely as long as they never switched between dict or list, but it would still be confusing, and it's hard to see how to make it backwards compatible because the current workaround relies on people first using a dict and then using a list. :-(

@jbednar jbednar marked this pull request as draft September 30, 2020 20:15
@jbednar jbednar added this to the 2.0 milestone May 10, 2021
@philippjfr
Copy link
Copy Markdown
Member

Superseded by #598

@philippjfr philippjfr closed this Jan 9, 2023
@jbednar jbednar deleted the objselector branch December 5, 2025 17:03
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.

2 participants