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

Black adds an extra blank line to an import line that ends with # fmt: skip #3438

Closed
yilei opened this issue Dec 13, 2022 · 2 comments · Fixed by #3610
Closed

Black adds an extra blank line to an import line that ends with # fmt: skip #3438

yilei opened this issue Dec 13, 2022 · 2 comments · Fixed by #3610
Labels
F: comments The syntactic kind. Not in the language grammar, always on our minds. Best bugs. F: fmtskip fmt: skip implementation T: bug Something isn't working

Comments

@yilei
Copy link
Contributor

yilei commented Dec 13, 2022

Describe the bug

It reformats:

import os
import re  # fmt: skip
import sys

to:

import os

import re  # fmt: skip
import sys

Example: https://black.vercel.app/?version=stable&state=_Td6WFoAAATm1rRGAgAhARYAAAB0L-Wj4ACSAFtdAD2IimZxl1N_WlwxRhR7MSgE-ANVmAcXiiHH_vLVBv6H9oJYdLgh3uJSbWM1qliEV5h8ji5l4G2iqd5AHPniBCkwXU5Gi97RWyWgW8b3Yvc90k3x9wXSWeryKEAAAIvWais0zjPAAAF3kwEAAAAB02j7scRn-wIAAAAABFla

Expected behavior

# fmt: skip shouldn't make it add an extra line.

Additional context

The issue is Black internally converts the import re # fmt: skip line to a STANDALONE_COMMENT, and for other "real" STANDALONE_COMMENT lines it will add the blank line as designed. Maybe internally it needs to distinguish between real STANDALONE_COMMENT nodes v.s. converted from fmt off/skip?

@yilei yilei added the T: bug Something isn't working label Dec 13, 2022
@ichard26 ichard26 added F: comments The syntactic kind. Not in the language grammar, always on our minds. Best bugs. F: fmtskip fmt: skip implementation labels Dec 14, 2022
@ichard26
Copy link
Collaborator

I didn't know we added a blank before a comment preceding imports, that's a bit surprising. Anyway, distinguishing between real and internal STANDALONE_COMMENTS sounds like a reasonable fix, but I haven't looked at the code. This also affects fmt: off/on FWIW:

--- test.py	2022-12-14 04:17:08.331940 +0000
+++ test.py	2022-12-14 04:17:22.664904 +0000
@@ -1,5 +1,6 @@
 import os
+
 # fmt: off
 import re
 # fmt: on
 import sys

@julian-west
Copy link

I have also come across this bug in the situation where I have a long import statement which black reformats across multiple lines. I tried to skip formatting on this import statement but instead an extra line is created. This causes problems with tools like isort which then tries to remove the new blank line.

For example:

from my_package.my_module import function
from my_package.my_module_with_a_very_very_long_name import function_with_a_very_very_long_name  # fmt: skip

Reformatted to:

from my_package.my_module import function

from my_package.my_module_with_a_very_very_long_name import function_with_a_very_very_long_name  # fmt: skip

I'm not sure if it helps solve/understand the bug better, but I also noticed that if I add # fmt: skip to all imports in the 'block', then the extra line is not created.

For example, the following:

import os
import sys  # fmt: skip
import re  # fmt: skip

import os  # fmt: skip
import sys  # fmt: skip
import re  # fmt: skip

Is reformatted to:

import os

import sys  # fmt: skip
import re  # fmt: skip

import os  # fmt: skip
import sys  # fmt: skip
import re  # fmt: skip

Playground example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F: comments The syntactic kind. Not in the language grammar, always on our minds. Best bugs. F: fmtskip fmt: skip implementation T: bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants