-
-
Notifications
You must be signed in to change notification settings - Fork 42
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
Add nbqa-yapf #547
Add nbqa-yapf #547
Conversation
Awesome, thanks! As well as this, it would be good add a simple test, and to show a little example in the docs - do you have time to do these too? No worries if not, I can do it next week
Very kind of you, mind if I include this as a README testimonial? |
@all-contributors please add @bdice for ideas and code |
I've put up a pull request to add @bdice! 🎉 |
- id: nbqa-yapf | ||
name: nbqa-yapf | ||
description: "Run 'yapf' on a Jupyter Notebook" | ||
entry: nbqa yapf --in-place |
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.
I'm not sure how the --in-place
modification flag interacts with --nbqa-mutate
but this seemed to behave like I wanted (I had to provide --nbqa-mutate
to make changes).
I'm taking a look at tests and docs now. For testing, I'm going to just copy the tests for
😄 Sure! Maybe a more descriptive testimonial would be: "nbqa is a clean, easy to use, and effective tool for notebook code style. Formatting and readability makes a huge difference when rendering notebooks in a project's documentation!" Feel free to use either statement. 🥳 |
Awesome - yeah, I think that the test |
@MarcoGorelli Thanks again for the advice/help! I pushed changes to docs/tests. I copied the
|
Hey @bdice , thanks, much appreciated that you looked into this and made extensive tests. This actually uncovers an issue. If we use two newlines between cell separators, then For example. if we have # %%NBQA-CELL-SEP
import os
# %%NBQA-CELL-SEP
def hello_world():
print('hello world')
hello_world()
# %%NBQA-CELL-SEP then we will get $ yapf t.py --diff
--- t.py (original)
+++ t.py (reformatted)
@@ -9,5 +9,4 @@
hello_world()
-
# %%NBQA-CELL-SEP
But, if instead we use single new lines as separators, e.g. # %%NBQA-CELL-SEP
import os
# %%NBQA-CELL-SEP
def hello_world():
print('hello world')
hello_world()
# %%NBQA-CELL-SEP then we get $ yapf t.py --diff
--- t.py (original)
+++ t.py (reformatted)
@@ -1,5 +1,6 @@
# %%NBQA-CELL-SEP
import os
+
# %%NBQA-CELL-SEP
def hello_world():
So...I'm a bit stuck about what to do here |
@MarcoGorelli It looks like the statements Can you help me understand why this is an issue and what effect it has? Is it changing the number of trailing newlines in notebook cells in an unexpected way? |
Sure - the issue is that nbqa converts a notebook to a temporary Python file, with two newlines and a special comment to indicate separation between cells. The issue is that like this we end up in a situation in which The only workaround I can think of it to apply |
Aha, I see. Perhaps there is an option in the yapf configuration that would stop this? Perhaps |
Great, yeah - if yapf is somewhat configurable then we could set that as the default in https://github.com/nbQA-dev/nbQA/blob/master/nbqa/config/default_config.toml , thanks! |
@MarcoGorelli It seems like the option for configuring I'm not sure what to do about this, I will defer to your expertise if you have further ideas for how to handle it. Do we try the cell-by-cell approach or just add a warning to the docs section on yapf? |
Hey @bdice - I think my preference would be to either wait for the next release, or try the cell-by-cell approach (I'm about to start a new job though so don't really have the resources for the latter at the moment - I can review a PR if you put one together, else I hope to be able to get back to this later) |
Hey @bdice - looks like yapf 0.31.0 is out! So I think we can add this option and require this as the minimum version for yapf (similarly to how we require a minimum version for isort) |
@MarcoGorelli Thanks! Unfortunately my guess was incorrect -- Likewise, I'm not sure that I have time to change to a "cell by cell" approach. I prefer using In practice, the pre-commit hook I have set up with Let me know what you'd like to do with this PR - I'm fine with whatever outcome you think is appropriate. Maybe we need to file a tracking issue with yapf? |
That's very kind of you 🙏 I'm also very short on time in this period (just started a new job) so let's close for now, we can always come back to this later |
Reopening as I have an idea for how to get this through without adding too much complexity |
Nice! Let me know if you’d like a helping hand or review. |
Sure, thanks! A review would be helpful if you have time :) cc @girip11 @s-weigand if you have comments |
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.
@MarcoGorelli The changes look good to me. Thanks!
I will review today |
Codecov Report
@@ Coverage Diff @@
## master #547 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 14 14
Lines 781 797 +16
Branches 115 117 +2
=========================================
+ Hits 781 797 +16
Continue to review full report at Codecov.
|
merging for now as I'm pretty keen on making a new release before tomorrow, can always revert if it anyone has objections / causes bugs anywhere |
@MarcoGorelli Thank you again for your guidance and help! 😄 |
@bdice I forgot to mention, if you wanted to mention |
Adds a new hook definition for yapf.
I'm a big fan of nbqa! It's truly an amazing tool. 💯