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

fmt:off not respected for last line of block for comments #3588

Open
alexandrenorman opened this issue Feb 27, 2023 · 2 comments
Open

fmt:off not respected for last line of block for comments #3588

alexandrenorman opened this issue Feb 27, 2023 · 2 comments
Labels
T: bug Something isn't working

Comments

@alexandrenorman
Copy link

Describe the bug

Black doesn't fully honor #fmt:off on line before #fmt:on, it moves comments.

To Reproduce

lambda_fields = {
    # fmt: off
    "full_name": lambda x: x.display_name,
    "publication_status_as_label": lambda x: x.professional_validation_workflow.get_publication_status_display() if x.professional_validation_workflow else None,  # NOQA: E501
    "transition_state": lambda x: x.professional_validation_workflow.transition_state if x.professional_validation_workflow else None,  # NOQA: E501
    # fmt: on
}

Is formatted as

$ black --diff test.py 
--- test.py	2023-02-27 15:36:15.874401 +0000
+++ test.py	2023-02-27 15:36:20.252077 +0000
@@ -1,7 +1,8 @@
 lambda_fields = {
     # fmt: off
     "full_name": lambda x: x.display_name,
     "publication_status_as_label": lambda x: x.professional_validation_workflow.get_publication_status_display() if x.professional_validation_workflow else None,  # NOQA: E501
-    "transition_state": lambda x: x.professional_validation_workflow.transition_state if x.professional_validation_workflow else None,  # NOQA: E501
+    "transition_state": lambda x: x.professional_validation_workflow.transition_state if x.professional_validation_workflow else None,
+    # NOQA: E501
     # fmt: on
 }

The # NOQA directive is dissociated from it's line on the last line. Causing Flake8 to fail.

If we invert lines, it works and the last line is not formatted :

lambda_fields = {
    # fmt: off
    "publication_status_as_label": lambda x: x.professional_validation_workflow.get_publication_status_display() if x.professional_validation_workflow else None,  # NOQA: E501
    "transition_state": lambda x: x.professional_validation_workflow.transition_state if x.professional_validation_workflow else None,  # NOQA: E501
    "full_name": lambda x: x.display_name,
    # fmt: on
}

Environment

  • Black's version: 23.1.1.dev8+g25d886f
  • OS and Python version: Debian GNU/Linux 11 - Python 3.10.9
@alexandrenorman alexandrenorman added the T: bug Something isn't working label Feb 27, 2023
@mpearce25
Copy link

Don't have a fix but here is a shorter example with the same behavior. This shows it's not related to line length limits. The issue feels similar to the recently fixed #3610

Raw Input

lambda_fields = {
    # fmt: off
    "a": "b",  # c
    # fmt: on
}

After formatting

lambda_fields = {
    # fmt: off
    "a": "b",
    # c
    # fmt: on
}

@randolf-scholz
Copy link

Still happens in the latest version: playground example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants