8000 Do not use mutables as default parameters by DimitriPapadopoulos · Pull Request #21402 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Do not use mutables as default parameters #21402

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
Oct 21, 2021
Merged

Do not use mutables as default parameters #21402

merged 1 commit into from
Oct 21, 2021

Conversation

DimitriPapadopoulos
Copy link
Contributor
@DimitriPapadopoulos DimitriPapadopoulos commented Oct 20, 2021

PR Summary

I have fixed two categories of code:

  • examples, because one shouldn't set a bad example,
  • functions that modify or risk modifying a mutable default argument.

Articles that discuss why mutable default arguments should be avoided:

PR Checklist

  • Has pytest style unit tests (and pytest passes).
  • Is Flake 8 compliant (run flake8 on changed files to check).
  • New features are documented, with examples if plot related.
  • Documentation is sphinx and numpydoc compliant (the docs should build without error).
  • Conforms to Matplotlib style conventions (install flake8-docstrings and run flake8 --docstring-convention=all).
  • New features have an entry in doc/users/next_whats_new/ (follow instructions in README.rst there).
  • API changes documented in doc/api/next_api_changes/ (follow instructions in README.rst there).

Copy link
@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Thank you for opening your first PR into Matplotlib!

If you have not heard from us in a while, please feel free to ping @matplotlib/developers or anyone who has commented on the PR. 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

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

@WeatherGod
Copy link
Member

Good catch! Some of that code is ancient, so we probably just haven't noticed.

My only question (for the group at large) is where did doc/sphinxext/github.py come from? Is it our own extension, or did it come from a third party?

@WeatherGod
Copy link
Member

By the way, do you happen to know of any style or lint checker that we could turn on to catch this in the future?

@DimitriPapadopoulos
Copy link
Contributor Author

It looks like doc/sphinxext/github.py had been copied over from IPython (820985a) and the original IPython file is broken as well:
https://github.com/ipython/ipython/blob/master/docs/sphinxext/github.py

I think the two files have diverged - at least the git logs are different.

I seem to recall DeepSource.io does catch this issue.

I have not fixed occurrences where the mutable is not modified by the function, because I don't want my changes to be too intrusive - not out of laziness 😄

@tacaswell
Copy link
Member

This PR is affected by a re-writing of our history to remove a large number of accidentally committed files see discourse for details.

To recover this PR it will need be rebased onto the new default branch (main). There are several ways to accomplish this, but we recommend (assuming that you call the matplotlib/matplotlib remote "upstream"

git remote update
git checkout main
git merge --ff-only upstream/main
git checkout YOUR_BRANCH
git rebase --onto=main upstream/old_master
# git rebase -i main # if you prefer
git push --force-with-lease   # assuming you are tracking your branch

If you do not feel comfortable doing this or need any help please reach out to any of the Matplotlib developers. We can either help you with the process or do it for you.

Thank you for your contributions to Matplotlib and sorry for the inconvenience.

@DimitriPapadopoulos
Copy link
Contributor Author

Should be rebased on main now.

@tacaswell
Copy link
Member

Thank you for being the first-time contributor test-subject for these instructions @DimitriPapadopoulos 😃 .

I have fixed two categories of code:
* examples,
* functions that modify or risk modifying a mutable default argument.
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.

Technically, all three cases are correct because the variables are never written to.

It's probably still reasonable to change this to have a simple uniform policy and prevent future chances from accidentally modifying these variables.

@QuLogic QuLogic merged commit 7d741df into matplotlib:main Oct 21, 2021
@QuLogic
Copy link
Member
QuLogic commented Oct 21, 2021

Thanks @DimitriPapadopoulos! Congratulations on your first PR to Matplotlib 🎉 We hope to hear from you again.

@DimitriPapadopoulos
Copy link
Contributor Author

Yes, it's true the mutables are not modified in these occurrences.

My concern was the examples, which should be fixed because users re-use them blindly.

@DimitriPapadopoulos DimitriPapadopoulos deleted the argument_mutable_default branch October 21, 2021 06:19
@anntzer anntzer mentioned this pull request Oct 21, 2021
7 tasks
@QuLogic QuLogic added this to the v3.6.0 milestone Jul 28, 2023
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.

5 participants
0