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

CI: Pre-commit to apply black/isort/flake8 on ipython notebooks #3287

Merged
merged 12 commits into from Oct 19, 2021

Conversation

cphyc
Copy link
Member

@cphyc cphyc commented May 20, 2021

PR Summary

This adds a pre-commit hook to automatically format with black our notebooks. This should respect our black/isort/pyupgrade configs.

@neutrinoceros neutrinoceros added infrastructure Related to CI, versioning, websites, organizational issues, etc code style Related to linting tools labels May 20, 2021
@cphyc
Copy link
Member Author

cphyc commented May 20, 2021

pre-commit.ci run

@neutrinoceros
Copy link
Member

neutrinoceros commented May 20, 2021

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.

@cphyc
Copy link
Member Author

cphyc commented May 20, 2021

pre-commit.ci autofix

@cphyc
Copy link
Member Author

cphyc commented May 20, 2021

@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.
We could create an issue to have it triggered once 4.0 is released? Wdyt?

@neutrinoceros
Copy link
Member

We could create an issue to have it triggered once 4.0 is released? Wdyt?

I'd put it in the 4.1 Milestone, drop the auto-fix commit for now, and switch this to draft for now

@cphyc cphyc changed the title Pre-commit to apply black on notebooks Pre-commit to apply black/isort/flake8 on ipython notebooks May 20, 2021
@cphyc cphyc marked this pull request as draft May 20, 2021 09:59
- id: nbqa-isort
args: [--nbqa-mutate]
- id: nbqa-flake8
args: [--extend-ignore=E402]
Copy link
Member Author

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.

@cphyc
Copy link
Member Author

cphyc commented May 20, 2021

Todo:

  • once we're happy with this PR, call pre-commit.ci autofix
  • inspect whether we can reactivate E402 with flake or not

@neutrinoceros neutrinoceros added this to the 4.1 milestone May 20, 2021
@MarcoGorelli
Copy link

Hey, thanks for trying out nbQA!

Just a note on one of the error messages:

doc/source/analyzing/mesh_filter.ipynb:cell_4:4:5: E999 SyntaxError: invalid syntax

This looks like a bug in nbQA - cell4, after black has been run, becomes

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!

@MarcoGorelli
Copy link

MarcoGorelli commented May 20, 2021

Got it, it's because IPython.core.inputsplitter.IPythonInputSplitter thinks "% hot_ad["temperature"]" is IPython magic:

(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!

@neutrinoceros
Copy link
Member

Thanks a lot @MarcoGorelli !
There is no rush on our side, FYI :)

@MarcoGorelli
Copy link

MarcoGorelli commented May 23, 2021

Thanks a lot @MarcoGorelli !
There is no rush on our side, FYI :)

Fixed in version 0.9.0 🎉

It looks like doc/source/cookbook/fits_xray_images.ipynb now has a syntax error:

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

@neutrinoceros
Copy link
Member

Awesome, thank you so much for catching this !

@MarcoGorelli
Copy link

MarcoGorelli commented Jun 1, 2021

Happy to help, and thanks for your awesome library!

doc/source/cookbook/fits_xray_images.ipynb is still failing as it contains invalid syntax - I'd suggest either fixing that first, or (for now) using

-      args: [--nbqa-mutate]
+      args: [--nbqa-mutate, --nbqa-skip-bad-cells]

@cphyc
Copy link
Member Author

cphyc commented Jun 1, 2021

@MarcoGorelli thanks! I've fixed the syntax error and the flake8 issues, it now seems to be working like a charm!

@cphyc
Copy link
Member Author

cphyc commented Jun 1, 2021

pre-commit.ci autofix

@neutrinoceros
Copy link
Member

@cphyc I believe the failures on Jenkins were fixed on the main branch, can you rebase this please ?

@cphyc
Copy link
Member Author

cphyc commented Jun 7, 2021

pre-commit.ci autofix

@cphyc cphyc marked this pull request as ready for review July 6, 2021 17:06
@cphyc
Copy link
Member Author

cphyc commented Jul 13, 2021

pre-commit.ci autofix

1 similar comment
@neutrinoceros
Copy link
Member

pre-commit.ci autofix

@neutrinoceros
Copy link
Member

@cphyc looks like pre-commit.ci auto fixing is currently broken

@neutrinoceros
Copy link
Member

neutrinoceros commented Jul 13, 2021

Nevermind I get what's happening: most likely pre-commit.ci is timing out after 3min so it can't apply your changes.
If you don't want to assume authorship for a this sweeping change, I suggest you use a similar trick as I did with #2749 :

pre-commit run --all-files
git commit --author=convert-repo ...

Also, you may want to add the resulting commit to .git-blame-ignore-revs

@neutrinoceros
Copy link
Member

Wow, black 21.8b0 now has a specialised pre-commit hook for notebooks,
psf/black#2357

@MarcoGorelli since you contributed this too, what's your recommendation ? is there any edge to keep using the nbqa hook or should we switch ?

@MarcoGorelli
Copy link

Hey @neutrinoceros, thanks for asking - I'd advise using black's hook, I'll deprecate the nbqa-black one

Comment on lines 38 to 41
- id: black
language_version: python3
- id: black-jupyter
language_version: python3

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

Copy link
Member

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.

@matthewturk
Copy link
Member

So where do we think we're at on this?

@matthewturk matthewturk changed the title Pre-commit to apply black/isort/flake8 on ipython notebooks CI: Pre-commit to apply black/isort/flake8 on ipython notebooks Oct 9, 2021
@cphyc
Copy link
Member Author

cphyc commented Oct 11, 2021

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

@cphyc cphyc closed this Oct 11, 2021
@cphyc cphyc reopened this Oct 11, 2021
@cphyc
Copy link
Member Author

cphyc commented Oct 12, 2021

@yt-fido test this please

@neutrinoceros neutrinoceros reopened this Oct 14, 2021
@matthewturk matthewturk merged commit 8c94e16 into yt-project:main Oct 19, 2021
@cphyc cphyc deleted the lint-ipynb branch October 19, 2021 15:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
code style Related to linting tools docs infrastructure Related to CI, versioning, websites, organizational issues, etc
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants