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

hatch fmt leads to unresolvable ruff setting conflict with from __future__ import annotations #1459

Open
warsaw opened this issue May 3, 2024 · 18 comments

Comments

@warsaw
Copy link
Contributor

warsaw commented May 3, 2024

Let's start with this branch: https://gitlab.com/warsaw/public/-/tree/hatch-fmt

Specifically, the src/public/public.py file starts with these lines:

# https://docs.astral.sh/ruff/rules/future-rewritable-type-annotation/
from __future__ import annotations

import sys

from typing import Any, TYPE_CHECKING, overload

Running hatch fmt check gives me this error:

$ hatch fmt --check
src/public/public.py:2:1: I001 [*] Import block is un-sorted or un-formatted
Found 1 error.
[*] 1 fixable with the `--fix` option.

and would make this change:

$ hatch fmt --diff
--- src/public/public.py
+++ src/public/public.py
@@ -1,9 +1,10 @@
 # https://docs.astral.sh/ruff/rules/future-rewritable-type-annotation/
-from __future__ import annotations
-
 import sys
 
-from typing import Any, TYPE_CHECKING, overload
+from typing import TYPE_CHECKING, Any, overload
+
+from __future__ import annotations
+
 
 if TYPE_CHECKING:
     from .types import ModuleAware

Would fix 1 error.

Let's let hatch fix it:

$ hatch fmt
src/public/public.py:6:1: F404 `from __future__` imports must occur at the beginning of the file
Found 2 errors (1 fixed, 1 remaining).
$ git diff
git diff
diff --git a/src/public/public.py b/src/public/public.py
index b31fd19..d731a41 100644
--- a/src/public/public.py
+++ b/src/public/public.py
@@ -1,9 +1,10 @@
 # https://docs.astral.sh/ruff/rules/future-rewritable-type-annotation/
-from __future__ import annotations
-
 import sys
 
-from typing import Any, TYPE_CHECKING, overload
+from typing import TYPE_CHECKING, Any, overload
+
+from __future__ import annotations
+
 
 if TYPE_CHECKING:
     from .types import ModuleAware

Uh oh! The I001 and F404 can't be resolved without ignoring one or the other. Since __future__ imports should go at the top, it's probably better to ignore the I001 but such a blanket ignore could hide other import sorting issues.

@ofek
Copy link
Collaborator

ofek commented May 3, 2024

The solution would be to run just the formatter first via -f

@warsaw
Copy link
Contributor Author

warsaw commented May 3, 2024

Hmm, running hatch fmt -f turns all my single quotes to double quotes!

@ofek
Copy link
Collaborator

ofek commented May 3, 2024

You would have to add configuration for that, see the config file in this repo

@warsaw
Copy link
Contributor Author

warsaw commented May 3, 2024

Ah, so hatch fmt -f doesn't use the same defaults as hatch fmt?

@ofek
Copy link
Collaborator

ofek commented May 3, 2024

No it does but what's happening is that the linter comes first followed by the formatter and you're encountering an error in the former so the only way is to run the other one separately. This is a known issue where everything is not yet cohesive which is why they have a plan to only have a single command, and also the reason I have implemented this as a single command in preparation for that future.

@warsaw
Copy link
Contributor Author

warsaw commented May 3, 2024

I see! Thanks. Let me continue to play around with it.

@warsaw
Copy link
Contributor Author

warsaw commented May 3, 2024

I still have to add the noqa: I001 to the from __future__ import annotations line, but I'm going to chalk this up to ruff's problem, not hatch's.

https://gitlab.com/warsaw/public/-/blob/hatch-fmt/src/public/public.py#L3

@ofek
Copy link
Collaborator

ofek commented May 3, 2024

I'm confused, why is it trying to move the import somewhere other than the top?

@warsaw
Copy link
Contributor Author

warsaw commented May 3, 2024

I'm confused, why is it trying to move the import somewhere other than the top?

Great question! I'm confused why it wants to do that too.

@ofek
Copy link
Collaborator

ofek commented May 4, 2024

Fixed?

@warsaw
Copy link
Contributor Author

warsaw commented May 4, 2024

Nope, even with ruff 0.4.3.

@ofek
Copy link
Collaborator

ofek commented May 4, 2024

You provide a minimal reproducible example file and open up an issue over there?

@warsaw
Copy link
Contributor Author

warsaw commented May 4, 2024

So, you don't think it's a hatch fmt problem specifically?

@ofek
Copy link
Collaborator

ofek commented May 4, 2024

If you can provide a very small file to reproduce we could test by just enabling that single offending rule.

@warsaw
Copy link
Contributor Author

warsaw commented May 4, 2024

I don't know if this is reproducible outside the repository, but the file triggering the problem is public.py.

Another way to reproduce it is:

$ git clone https://gitlab.com/warsaw/public.git
$ cd public
# Edit src/public/public.py to remove the noqa comment from the future import line
$ hatch fmt --check src
src/public/public.py:7:1: I001 [*] Import block is un-sorted or un-formatted
Found 1 error.
[*] 1 fixable with the `--fix` option.
$ hatch fmt src
src/public/public.py:11:1: F404 `from __future__` imports must occur at the beginning of the file
Found 2 errors (1 fixed, 1 remaining).
$ git diff
diff --git a/src/public/public.py b/src/public/public.py
index 96bd404..bbe8d7f 100644
--- a/src/public/public.py
+++ b/src/public/public.py
@@ -4,11 +4,12 @@
 # and linter will be in conflict between I001 and F404 (which wants to move
 # this import to below `import sys`.  Ruff's unified linter and formatter
 # will hopefully resolve this: https://github.com/astral-sh/ruff/issues/8232
-from __future__ import annotations  # noqa: I001
-
 import sys
 
-from typing import Any, TYPE_CHECKING, overload
+from typing import TYPE_CHECKING, Any, overload
+
+from __future__ import annotations
+
 
 if TYPE_CHECKING:
     from .types import ModuleAware

@ofek
Copy link
Collaborator

ofek commented May 4, 2024

Disable Hatch static analysis config: https://hatch.pypa.io/latest/config/internal/static-analysis/#no-config

@ofek ofek closed this as completed May 5, 2024
@warsaw
Copy link
Contributor Author

warsaw commented May 6, 2024

Sorry, are you saying that this is not a bug in hatch, or it is caused by the ruff lint/formatter problem? One thing that I think would help to debug further is to be able to print the commands hatch is running with higher verbosity in hatch fmt, but I believe you're fixing that already.

@ofek
Copy link
Collaborator

ofek commented May 6, 2024

I'm not precisely sure what caused the issue but I thought that you weren't extending when in fact you seem to be doing so. I wonder what happens when you try the persistent config approach.

@ofek ofek reopened this May 6, 2024
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