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

Fix callback default (horror bug!) #1534

Merged
merged 4 commits into from
Apr 20, 2020
Merged

Fix callback default (horror bug!) #1534

merged 4 commits into from
Apr 20, 2020

Conversation

awaelchli
Copy link
Contributor

@awaelchli awaelchli commented Apr 20, 2020

Before submitting

  • Was this discussed/approved via a Github issue? (no need for typos and docs improvements)
  • Did you read the contributor guideline, Pull Request section?
  • Did you make sure to update the docs?
  • Did you write any new necessary tests?
  • If you made a notable change (that affects users), did you update the CHANGELOG?

What does this PR do?

Fixes a horror bug that took me sooo many hours to find when implementing #1450.
An argument in a function or method should NEVER be initialized to an empty list! Here is an example of what happens when you do so:

from pytorch_lightning import Trainer
from pytorch_lightning.callbacks import ModelCheckpoint


a = Trainer()
a.callbacks.append(ModelCheckpoint('./a'))

print(len(a.callbacks))

b = Trainer()
b.callbacks.append(ModelCheckpoint('./b'))

print(len(b.callbacks))

Output (on master):

1
2

Expected:

1
1

It is a big Python pitfall.

PR review

Anyone in the community is free to review the PR once the tests have passed.
If we didn't discuss your PR in Github issues there's a high chance it will not be merged.

Did you have fun?

Not this time! 🙃

@mergify mergify bot requested a review from a team April 20, 2020 05:08
@awaelchli
Copy link
Contributor Author

@hadim just letting you know :))

@pep8speaks
Copy link

pep8speaks commented Apr 20, 2020

Hello @awaelchli! Thanks for updating this PR.

There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻

Comment last updated at 2020-04-20 05:24:13 UTC

@codecov
Copy link

codecov bot commented Apr 20, 2020

Codecov Report

Merging #1534 into master will not change coverage.
The diff coverage is 100%.

@@          Coverage Diff           @@
##           master   #1534   +/-   ##
======================================
  Coverage      90%     90%           
======================================
  Files          68      68           
  Lines        3804    3804           
======================================
  Hits         3441    3441           
  Misses        363     363           

@Borda Borda added the bug Something isn't working label Apr 20, 2020
Copy link
Member

@Borda Borda left a comment

Choose a reason for hiding this comment

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

LGTM 🐰

pytorch_lightning/trainer/trainer.py Show resolved Hide resolved
Copy link
Member

@Borda Borda left a comment

Choose a reason for hiding this comment

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

LGTM 🐰

@mergify mergify bot requested a review from a team April 20, 2020 06:02
@Borda Borda added the priority: 0 High priority task label Apr 20, 2020
@Borda Borda added this to the 0.7.4 milestone Apr 20, 2020
@Borda Borda added the ready PRs ready to be merged label Apr 20, 2020
@williamFalcon williamFalcon merged commit 4fca994 into Lightning-AI:master Apr 20, 2020
@awaelchli awaelchli deleted the bugfix/callback_default branch April 20, 2020 12:23
@hadim
Copy link
Contributor

hadim commented Apr 20, 2020

What??? I didn't know that! Thanks, @awaelchli for the catch.

@awaelchli
Copy link
Contributor Author

Yep but no harm was done, because most users have anyway just one Trainer in their script and never got into trouble so far :) haha this is really a weird Python thing.
https://docs.python-guide.org/writing/gotchas/#mutable-default-arguments

@awaelchli awaelchli mentioned this pull request Apr 20, 2020
7 tasks
@Borda Borda modified the milestones: 0.7.4, v0.7.x Apr 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working priority: 0 High priority task ready PRs ready to be merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants