Skip to content

Allow unhashable keys in AxesStack. #8451

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 11, 2017

Conversation

anntzer
Copy link
Contributor

@anntzer anntzer commented Apr 9, 2017

They simply will compare unequal to anything else.

Closes #8395.
Alternative to #8399 (although the part about removing Transform.__eq__ in #8399 stays valid).
See discussion starting at #7377 (comment) for why this is a reasonable approach.

@@ -122,7 +124,7 @@ def add(self, key, a):
try:
hash(key)
except TypeError:
raise ValueError("first argument, %s, is not a valid key" % key)
Copy link
Member

Choose a reason for hiding this comment

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

But if the same key comes in again it will get a different object which sort of defeats the point of this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But transforms are explicitly mutable (not just by fiddling with the attributes: they actually expose a bunch of methods explicitly for that purpose), which means that trying to key on them is bound to give wrong results (this is the same reason why lists are not hashable in python). (In fact I had prepared another PR for making all transform classes hashable, but that is not a robust solution in the presence of mutability.)

I argued in #7377 that this behavior (trying to detect and drop duplicates axes) is already brittle anyways and should be deprecated.

@WeatherGod
Copy link
Member

WeatherGod commented Apr 10, 2017 via email

@anntzer
Copy link
Contributor Author

anntzer commented Apr 10, 2017

Freezing a transform doesn't make it immutable, it just makes it independent of changes to its children (by recursively replacing the children by "private" copies of them). This is in fact the goal stated in the docstring:

        Returns a frozen copy of this transform node.  The frozen copy
        will not update when its children change.  Useful for storing
        a previously known state of a transform where
        ``copy.deepcopy()`` might normally be used.

@WeatherGod
Copy link
Member

WeatherGod commented Apr 10, 2017 via email

@anntzer
Copy link
Contributor Author

anntzer commented Apr 10, 2017

Transforms are not hashable and typically not used as keys in AxesStack.

@WeatherGod
Copy link
Member

WeatherGod commented Apr 10, 2017 via email

@tacaswell tacaswell added this to the 2.0.1 (next bug fix release) milestone Apr 12, 2017
@tacaswell
Copy link
Member

Ok, that makes sense, but this still seems hackish. Would in be better to remove the transform from the kwargs used to make the key (in Figure._make_key)?

On reading the code of the OP, does transform even get used? If an Axes is passed in, kwargs is only used for the key and nothing else. This seems not quite right.

@anntzer
Copy link
Contributor Author

anntzer commented Apr 12, 2017

FWIW I still think the ultimately correct way to do this is to get rid of the brittle "check whether there is already an axes with the same key" approach (for reasons already explained elsewhere).

Alternatively, instead of using a dict, you could just use a list of pairs (key, value) and check for equality with the keys one at a time -- there is no way you can have so many Axes that the performance becomes an issue. But this is still bound to have problematic semantics due to key mutability.

@QuLogic QuLogic modified the milestones: 2.0.1 (next bug fix release), 2.0.2 (next bug fix release) May 3, 2017
@tacaswell
Copy link
Member

@anntzer I am convinced on this, can you rebase?

They simply will compare unequal to anything else.
@anntzer anntzer force-pushed the unhashable-keys-in-axesstack branch from c4e83b4 to 3d6c9c0 Compare July 10, 2017 23:53
@anntzer
Copy link
Contributor Author

anntzer commented Jul 10, 2017

done

@tacaswell tacaswell merged commit f5f3d79 into matplotlib:master Jul 11, 2017
@tacaswell
Copy link
Member

Thank you @anntzer

@QuLogic QuLogic modified the milestones: 2.1 (next point release), 2.0.3 (next bug fix release) Jul 11, 2017
@anntzer anntzer deleted the unhashable-keys-in-axesstack branch July 11, 2017 03:11
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.

4 participants