Skip to content
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

Allow Paths to be marked as readonly #2010

Merged
merged 4 commits into from May 15, 2013
Merged

Conversation

mdboom
Copy link
Member

@mdboom mdboom commented May 15, 2013

This PR is just to prevent the user from modifying the singleton Paths (unit_circle, unit_rectangle etc) which then cause nasty side effects for anything else that tries to use that Path later (such as the Marker infrastructure).

Issue #2003 recently "rediscovered" this bug, but we've seen it before.

  1. Adds a readonly kwarg to the Path constructor
  2. Mark all of the singleton paths as readonly
  3. deepcopy those Paths in tests that need to modify the path.

The actual domain of this API change is fairly small, as most Paths created will still be mutable -- this only affects code that wants to modify one of the singleton Paths.

…eadonly, and deepcopy those Paths in tests that need to modify the path.
@mdboom mdboom mentioned this pull request May 15, 2013
self._update_values()

@property
def simplify_threshold(self):
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure this property (and some of the others) really need to exist. The original attribute access was perfectly satisfactory - if you changed something that didn't make sense then you would expect it to blow up...
I guess what I'm saying is that I'm not really a big fan of protecting values in this way - if a user wants to fiddle with them, then they should be able to.

Copy link
Member Author

Choose a reason for hiding this comment

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

The nice thing about this approach is that when the vertices are updated, these values are updated as well, so they should always be correct. I think that's a good thing.

Copy link
Member

Choose a reason for hiding this comment

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

I think that's a good thing.

I agree, but there are always corner cases 😉

path.vertices[10:50] += 10

I'm happy with the read only protection for coding mistakes (such as mine) - but this is python: I don't think we need to worry about protecting the whole state of Path instances.

Either way, it's much of a muchness - and more important than whether it is a property or an actual attribute, I think there should be a oneline docstring for all of these.

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 think I'll leave this as properties -- I think in most cases this is preferred.

Agreed about the docstrings.

I realise we can't protect much, but I think this was a particularly surprising one, mainly because I think users are surprised that Path.unit_circle() is a singleton. I could have also resolved this by having those return a copy, but that breaks some optimizations in markers and elsewhere where it tries to save the same path only once wherever possible.

@mdboom
Copy link
Member Author

mdboom commented May 15, 2013

Ah -- it seems the with assert_raises context manager trick doesn't work on Python 2.6.

mdboom added a commit that referenced this pull request May 15, 2013
Allow Paths to be marked as readonly
@mdboom mdboom merged commit 83d9cf8 into matplotlib:master May 15, 2013
@mdboom mdboom deleted the immutable-paths branch August 7, 2014 13:53
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.

None yet

2 participants