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
CI: Pre-commit to apply black/isort/flake8 on ipython notebooks #3287
Conversation
pre-commit.ci run |
Depending on the size of the diff we'll get from this (likely, big), I'd advise that this would be postpone to after the incoming release. |
pre-commit.ci autofix |
@neutrinoceros I triggered the pre-commit.ci bot, but actually we could decide not to trigger it now and just activate the hook in this PR. |
I'd put it in the 4.1 Milestone, drop the auto-fix commit for now, and switch this to draft for now |
- id: nbqa-isort | ||
args: [--nbqa-mutate] | ||
- id: nbqa-flake8 | ||
args: [--extend-ignore=E402] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: deactivating E402 (https://www.flake8rules.com/rules/E402.html) as it seems to clash with adding some line magics in cells.
Todo:
|
Hey, thanks for trying out nbQA! Just a note on one of the error messages:
This looks like a bug in nbQA - cell4, after print("Temperature of all cells:\n ad['temperature'] = \n%s\n" % ad["temperature"])
print(
"Temperatures of all \"hot\" cells:\n hot_ad['temperature'] = \n%s"
% hot_ad["temperature"]
) and that's definitely valid syntax...I'll come back to you on this, sorry about that, but thanks for helping to find this bug! |
Got it, it's because (Pdb) !line
' % hot_ad["temperature"]\n'
(Pdb) IPythonInputSplitter().transform_cell(line)
'get_ipython().run_line_magic(\'hot_ad\', \'["temperature"]\')\n' I think the solution is for nbQA to simply stop trying to handle inline magics, it's just too complicated / unreliable and leads to bugs like this one. I'll ping when a new version's out, hopefully this weekend! |
Thanks a lot @MarcoGorelli ! |
Fixed in version 0.9.0 🎉 It looks like error: cannot format doc/source/cookbook/fits_xray_images.ipynb: Cannot parse: cell_12:2:125: [("fits", "flux"), ("fits", "projected_temperature"), ("gas", "pseudo_pressure"), ("gas", "pseudo_entropy"]),
Oh no! 💥 💔 💥 Looks like the square and round brackets after "pseudo_entropy" are swapped got swapped in efb3411 |
Awesome, thank you so much for catching this ! |
Happy to help, and thanks for your awesome library!
- args: [--nbqa-mutate]
+ args: [--nbqa-mutate, --nbqa-skip-bad-cells] |
@MarcoGorelli thanks! I've fixed the syntax error and the flake8 issues, it now seems to be working like a charm! |
pre-commit.ci autofix |
@cphyc I believe the failures on Jenkins were fixed on the main branch, can you rebase this please ? |
pre-commit.ci autofix |
pre-commit.ci autofix |
1 similar comment
pre-commit.ci autofix |
@cphyc looks like pre-commit.ci auto fixing is currently broken |
Nevermind I get what's happening: most likely pre-commit.ci is timing out after 3min so it can't apply your changes. pre-commit run --all-files
git commit --author=convert-repo ... Also, you may want to add the resulting commit to |
Wow, black 21.8b0 now has a specialised pre-commit hook for notebooks, @MarcoGorelli since you contributed this too, what's your recommendation ? is there any edge to keep using the nbqa hook or should we switch ? |
Hey @neutrinoceros, thanks for asking - I'd advise using black's hook, I'll deprecate the |
.pre-commit-config.yaml
Outdated
- id: black | ||
language_version: python3 | ||
- id: black-jupyter | ||
language_version: python3 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
black-jupyter
does everything black
does, but also checks notebooks - you probably just need that one
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While you're at it @cphyc, you can remove the language_version
line. It became useless in the latest release.
'Kill two birds with one stone'
So where do we think we're at on this? |
This is good to go, but the tests failed randomly... I'll close/re-open to trigger them |
@yt-fido test this please |
PR Summary
This adds a pre-commit hook to automatically format with black our notebooks. This should respect our black/isort/pyupgrade configs.