Skip to content

Fix RevPrefixMap to inherit directly from TraitType#1098

Merged
mdickinson merged 5 commits into
masterfrom
fix/rev-prefix-map
Oct 26, 2021
Merged

Fix RevPrefixMap to inherit directly from TraitType#1098
mdickinson merged 5 commits into
masterfrom
fix/rev-prefix-map

Conversation

@mdickinson
Copy link
Copy Markdown
Member

This PR rewrites RevPrefixMap to inherit directly from TraitType rather than subclassing PrefixMap. This reduces the fragility of RevPrefixMap with respect to future upstream changes to PrefixMap.

To get to this code, I simply pasted in the Traits 6.2.0 version of PrefixMap under a new name OldPrefixMap to remove the dependence on upstream, and then refactored to collapse the RevPrefixMap -> OldPrefixMap -> TraitType inheritance tree to RevPrefixMap -> TraitType.

I've added some extra tests, to be sure that I didn't break existing behaviour while refactoring.

There are plenty of further cleanup and refactoring opportunities in RevPrefixMap (particularly in the __init__ and with the duplication between value_for and validate), but those are probably best left to the next minor feature release. I'm hoping that this PR can go into the 4.7.4 bugfix release.

Fixes #1094.

@mdickinson
Copy link
Copy Markdown
Member Author

Actually, let me do at least a little bit of cleanup in the __init__ method.

@mdickinson mdickinson marked this pull request as draft October 20, 2021 10:18
@mdickinson
Copy link
Copy Markdown
Member Author

Actually, let me do at least a little bit of cleanup in the __init__ method.

Done.

@mdickinson mdickinson marked this pull request as ready for review October 20, 2021 10:31
Comment thread tvtk/tvtk_base.py
@rahulporuri rahulporuri self-requested a review October 20, 2021 10:38
Comment thread tvtk/tvtk_base.py
self._map = {key: key for key in self.map}
self._rmap = {value: key for key, value in self.map.items()}

if default_value is None:
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We may be able to simplify this - it looks as though for all current use-cases of RevPrefixMap, there will always be a default value present (so we can make it required rather than optional in the signature), and that default value doesn't itself need completion - it'll be one of the keys of the map.

But that simplification would be a behaviour change, and I'm reluctant to make such a behaviour change in a bugfix release.

Comment thread tvtk/tvtk_base.py

matches = [key for key in self.map if key.startswith(value)]
if len(matches) == 1:
self._map[value] = match = matches[0]
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Traits 6.3.0 removed the caching from PrefixMap. We could do the same here. (But again, this would be a behaviour change that shouldn't be made in a bugfix release.)

@mdickinson
Copy link
Copy Markdown
Member Author

Sorry, @rahulporuri. In case it wasn't clear, I'm done tinkering with this PR. (There's lots more I'd love to do given time, but I don't have that time right now, and this seems like it should be enough for the bugfix.)

Copy link
Copy Markdown
Contributor

@rahulporuri rahulporuri left a comment

Choose a reason for hiding this comment

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

LGTM.

@mdickinson mdickinson merged commit 09dd09c into master Oct 26, 2021
@mdickinson mdickinson deleted the fix/rev-prefix-map branch October 26, 2021 08:43
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.

Rewrite RevPrefixMap to avoid depending on internal details of PrefixMap

2 participants