8000 FIX: make safe to add / remove artists during ArtistList iteration by tacaswell · Pull Request #22229 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

FIX: make safe to add / remove artists during ArtistList iteration #22229

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 3 commits into from
Jan 27, 2022

Conversation

tacaswell
Copy link
Member

PR Summary

If you remove artists during iteration some of the artists will be skipped due
to the underlying list being mutated.

PR Checklist

Tests and Styling

  • Has pytest style unit tests (and pytest passes).
  • Is Flake 8 compliant (install flake8-docstrings and run flake8 --docstring-convention=all).

If you remove artists during iteration some of the artists will be skipped due
to the underlying list being mutated.
@tacaswell tacaswell added this to the v3.5.2 milestone Jan 14, 2022
Copy link
Contributor
@anntzer anntzer left a comment

Choose a reason for hiding this comment

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

But this seems to be normal Python semantics, no? ("if you want to iterate on a list and remove entries from it at the same time, you need to make a copy first") I'm not sure we should deviate from standard semantics here (especially as the name ArtistList does suggest list-like semantics.

Hence blocking as I think this at least needs some more discussion, but open to changing my mind.

Copy link
Member
@timhoffm timhoffm left a comment

Choose a reason for hiding this comment

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

ArtistList exists to make the returned list finally immutable.

If we'll do this using a tuple, as suggested in the ArtistList docstring, that will effectively be equivalent to the behavioral change in this PR. So anticipating the change already is fine.

One could imagine alternative scenarios with a read-only proxy object to the internal data, which would suffer the modification problem. Such an object would only make sense if creating a copy is too expensive, but I don't think that's the case.

@tacaswell
Copy link
Member Author

But this seems to be normal Python semantics, no?

I had the same thought, however because it is a proxy which is providing a list-like interface of a filtered view on top of another list I think we should err on the side of hiding the mutation. Because all of the proxy lists operate on top of the same underlying list you could come up with a pathological example where you are iterating over and mutating more than one and they start to affect each other.

@anntzer
Copy link
Contributor
anntzer commented Jan 14, 2022

Even immutable proxies on mutable containers don't copy the entries, e.g. d = {<some dict>}; for k in d: d.pop(k) fails. Still, I don't feel that strongly about this, as long as this has been thought out.

@anntzer anntzer dismissed their stale review January 14, 2022 21:45

per the above

@tacaswell tacaswell modified the milestones: v3.5.2, v3.6.0 Jan 14, 2022
@tacaswell
Copy link
Member Author

Moved to 3.6 and I'll add a behavior change note?

@tacaswell tacaswell force-pushed the fix_mutation_safe_iter branch from c3ae176 to c6b0f25 Compare January 15, 2022 03:53
Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com>
@QuLogic QuLogic merged commit e1910b8 into matplotlib:main Jan 27, 2022
@tacaswell tacaswell deleted the fix_mutation_safe_iter branch January 27, 2022 17:35
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
0