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
Pylint support. Replace pass
with type
for handling magics
#302
Conversation
Pending
|
InputThough %%time
def func():
pass Current behaviorAfter black formats current code, we have two lines separating the cell magic and python function which looks odd. %%time
def func():
pass New behaviorCell magics will become comments. After black formats current code, comments are not moved two lines apart. %%time
def func():
pass |
Nice! This would be a breaking change for Did |
But we still have those issues of black splitting these replacement statements in to multiple lines and linter warnings. But the probability of running in to those is low compared to |
I see, thanks for explaining. Would it be possible to also put the magic in a comment, so that the only valid Python code here is |
Sorry I didn't get you. Could you elaborate on this? |
You currently have
but I think
How about something like 'type() # {magic} {token}' ? I don't think there's any scenario under which |
Good point. But we can't have just I am inclining towards handling multi line split in |
🤣 it feels like flattening a carpet that's too big for the floor |
@girip11 how about |
Thanks @MarcoGorelli. This would work with currently supported/tested tools. Meanwhile I was able to come up with a solution using regex matches. Even if modifications done those temporary placeholder lines we would still be able to restore the original line magics as long as the token is untouched. If we hit any issues with this approach i think we can tryout your ApproachI used By using the token in the beginning and at the end of the code, we would be able to replace the entire code across multiple lines using regex match. I will raise next iteration of the PR with those changes incorporated. @MarcoGorelli and @s-weigand feel free to take a look at those changes though I have kept the PR still in the draft mode. With tests added for pylint, I will make the PR ready for review. |
Codecov Report
@@ Coverage Diff @@
## master #302 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 9 10 +1
Lines 418 469 +51
=========================================
+ Hits 418 469 +51
Continue to review full report at Codecov.
|
bf1c2ef
to
ddd10c0
Compare
Clever! I'm normally scared of regexes, but this seems like a safe/legitimate use of them! |
Nice! Generally, this looks really good Regarding
do you mean this would be a separate PR? Because currently, if I run from random import (
randint,
)
if (
randint(
5, 10
)
> 8
):
type(
0xD0136BEE
) # %time prin 0xD0136BEE TBH I'd rather not knowingly introduce a regression...I don't know how common it is to run |
@girip11 if I apply the following diff diff --git a/nbqa/handle_magics.py b/nbqa/handle_magics.py
index e0ad7c6..e5ee464 100644
--- a/nbqa/handle_magics.py
+++ b/nbqa/handle_magics.py
@@ -73,8 +73,8 @@ class MagicHandler(ABC):
# `{token}` is not used as `String` because with different formatters (e.g: yapf)
# we would run in to formatting issues like single quotes formatted
# to double quotes or vice versa. `{token}` is used as hexadecimal number.
- _MAGIC_TEMPLATE: str = "type({token}) # {magic:10.10} {token}"
- _MAGIC_REGEX_TEMPLATE: str = r"type\({token}\).*{token}"
+ _MAGIC_TEMPLATE: str = "int() # {token} {magic:10.10} {token}"
+ _MAGIC_REGEX_TEMPLATE: str = r"int\(\) # {token}.*{token}"
def replace_magic(self, ipython_magic: str) -> MagicSubstitution:
""" then all the current tests still pass, and Would this work for you? Is there an advantage of (we might still get a false positive from |
Good catch. There are two ways to go about the line length issue.
I was using What are your thoughts on this? |
I have made all the changes. This PR is ready for review. Only pylint usage in docs needs to be updated. |
I agree with you on this. I just thought who would use line length to be as less as 50. So I just ignored space matches in the regex. Fixing the regex would solve the formatting issue. |
Sure, I'm happy to go with that! 👍 |
I will add this short line length as a test case for black in order to avoid regressions of this sort in the future. |
Pending tests to be added Changes to be committed: new file: nbqa/handle_magics.py modified: nbqa/save_source.py modified: tests/test_black.py modified: tests/test_flake8_works.py modified: tests/test_other_magics.py
Changes to be committed: modified: nbqa/handle_magics.py modified: nbqa/save_source.py
Changes to be committed: modified: nbqa/handle_magics.py modified: nbqa/notebook_info.py modified: nbqa/replace_source.py modified: nbqa/save_source.py
Changes to be committed: modified: nbqa/handle_magics.py modified: nbqa/notebook_info.py modified: nbqa/replace_source.py modified: nbqa/save_source.py
On branch line_magic_with_type Your branch is ahead of 'upstream/master' by 4 commits. (use "git push" to publish your local commits)
… verified output against one complete notebook manually. No crashes. Changes to be committed: modified: nbqa/__main__.py new file: tests/test_pylint_works.py modified: tests/test_return_code.py
…e issues with path patterns unescaped. Changes to be committed: modified: nbqa/__main__.py
Changes to be committed: modified: nbqa/__main__.py
Changes to be committed: modified: nbqa/__main__.py
…ndows path. Changes to be committed: modified: nbqa/__main__.py
Changes to be committed: modified: tests/test_pylint_works.py
…to be less then the replacement python code used to substitute ipython magics. Changes to be committed: modified: nbqa/handle_magics.py
Cool, this one works! Thanks, excellent work! I think we're ready for a 0.3.0 release (as this is a new feature), I'll just try to get #305 in as well |
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'll have another quick look later to double-check but I'm pretty sure this is good to go, thanks!
Changes to be committed: modified: README.md
b7afedf
to
0a9d37a
Compare
Awesome, look good!! |
This is required to support pylint. Using
pass
causes pylint to raiseunnecessary pass
warnings. Instead if we use builtin functiontype
we are clean of any flake8 and pylint warnings.