Skip to content

hatch fmt not placing noqa comments in the correct location #1883

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

Open
benjunmun opened this issue Jan 10, 2025 · 1 comment
Open

hatch fmt not placing noqa comments in the correct location #1883

benjunmun opened this issue Jan 10, 2025 · 1 comment

Comments

@benjunmun
Copy link

Running hatch fmt can move noqa comments to the wrong line when line-splitting. This caused me additional confusion because a second run of the formatter removed the now-extraneous noqa, and started showing me errors again.

Here is a minimal reproducer, used in a clean hatch new project. The first run of hatch fmt will reformat and return no other errors, but will split the noqa to the wrong line. A second run of hatch fmt will then strip the "invalid" noqa and start showing errors again.

def main():
    some_list = [1, 2, 3]
    another_list = []

    if True:
        if True:
            for item in some_list:
                another_list.append(generate_item( # noqa: PERF401 # Personal preference
                    item=item,
                    stuff=1))

def generate_item(item, stuff):
    return item + stuff

Personally, the biggest usability issue for me was that the first fmt run returned no errors - I usually assume the code would be stable after a run of fmt with no errors.

Hatch version: 1.14.0

@Grub4K
Copy link
Contributor

Grub4K commented Mar 20, 2025

hatch fmt uses ruff as backend, running first the linter and then the formatter. Similar behavior can be reproduced using plain ruff:

$ ruff version
ruff 0.11.0

$ ruff check --select PERF401 .
All checks passed!

$ ruff format .
1 file reformatted, 4 files left unchanged

$ ruff check --select PERF401 .
testing/src/testing/__main__.py:11:17: PERF401 Use `list.extend` to create a transformed list
   |
 9 |           if True:
10 |               for item in some_list:
11 | /                 another_list.append(
12 | |                     generate_item(  # noqa: PERF401 # Personal preference
13 | |                         item=item, stuff=1
14 | |                     )
15 | |                 )
   | |_________________^ PERF401
   |
   = help: Replace for loop with list.extend

Found 1 error.

I think this is rather a ruff problem and not much a hatch problem. The reason is that the formatter breaks the line, moves the comment and then it no longer targets the correct line.

I do not think there is a good solution to this problem from the hatch side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants