X Tutup
Skip to content

Fix mutable default arguments in backend_svg.py#31141

Merged
anntzer merged 1 commit intomatplotlib:mainfrom
llukito:fix-svg-mutable-defaults
Feb 13, 2026
Merged

Fix mutable default arguments in backend_svg.py#31141
anntzer merged 1 commit intomatplotlib:mainfrom
llukito:fix-svg-mutable-defaults

Conversation

@llukito
Copy link
Contributor

@llukito llukito commented Feb 12, 2026

Description

This PR fixes a mutable default argument issue in lib/matplotlib/backends/backend_svg.py.

In the XMLWriter class, the start and element methods were using a mutable dictionary (attrib={}) as a default argument.

While the current implementation may not be strictly mutating this dictionary in place, using a mutable default is an anti-pattern that creates a shared state across function calls. This change replaces it with None to ensure a fresh dictionary is created for every call, preventing potential side effects in future code changes.

Changes

  • Changed def start(..., attrib={}, ...) to def start(..., attrib=None, ...)
  • Changed def element(..., attrib={}, ...) to def element(..., attrib=None, ...)
  • Added if attrib is None: attrib = {} to initialize the dictionary safely inside the methods.

@github-actions
Copy link

Thank you for opening your first PR into Matplotlib!

If you have not heard from us in a week or so, please leave a new comment below and that should bring it to our attention. Most of our reviewers are volunteers and sometimes things fall through the cracks.

You can also join us on gitter for real-time discussion.

For details on testing, writing docs, and our review process, please see the developer guide.
Please let us know if (and how) you use AI, it will help us give you better feedback on your PR.

We strive to be a welcoming and open project. Please follow our Code of Conduct.

@llukito
Copy link
Contributor Author

llukito commented Feb 12, 2026

I noticed the checks failed, but they appear to be unrelated to my changes in backend_svg.py.

@llukito
Copy link
Contributor Author

llukito commented Feb 13, 2026

@tacaswell Thanks for the approval!

I see that @anntzer opened #31143, which refactors this class and effectively resolves the same issue.

Do you prefer to merge this PR as a targeted fix first, or should I close this in favor of the refactor? Happy to go with whatever works best for the codebase.

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.

I can rebase my PR on top of this one, no problem. Can you squash your two commits together, though?

@llukito llukito force-pushed the fix-svg-mutable-defaults branch from fefab51 to fded0d5 Compare February 13, 2026 07:53
@llukito
Copy link
Contributor Author

llukito commented Feb 13, 2026

Squashed and force-pushed, thanks for the review.

@anntzer
Copy link
Contributor

anntzer commented Feb 13, 2026

test failures are spurious.

@anntzer anntzer merged commit c4b9963 into matplotlib:main Feb 13, 2026
34 of 40 checks passed
@QuLogic QuLogic added this to the v3.11.0 milestone Feb 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

X Tutup