Open roshanjrajan-zip opened 10 months ago
Any help would be greatly appreciated! I hoped it would be fixed here - https://github.com/astral-sh/ruff/pull/8431? But it does seem to be along the same avenue.
Can you provide the original source code? The way you're describing the comment place, is it like this:
# pyright: ignore[reportUnknownVariableType]
for a in
(
Or,
for a in # pyright: ignore[reportUnknownVariableType]
(
It should be the second one. The code that I shared should show the same result that I am describing. Are you not able to reproduce it?
Unfortunately I can't even use # fmt:skip
on the same line as the #pyright pragma comment to skip this change. The only way to get past this is to add the # fmt:skip
on the line with just the parentheses.
def test_function(
very_long_dictionary: dict, # pyright:ignore
):
something = [
a["foo"]
for a in # pyright: ignore[reportUnknownVariableType]
( # fmt:skip <- Needed to add this here
very_long_dictionary.get(
"longer_key_name" # pyright: ignore
)
or []
)
]
Okay I got a way to reproduce this on terminal as it happens consistently in VSCode but not in terminal... I suspect I see this in VSCode consistently because its probably run without cache across runs since it only formats the file on save.
Context: The most important line is where the # pyright: ignore[reportUnknownVariableType]
is placed. It is supposed to be applied to the variable a
and putting it on a different line invalidates its application when using pyright.
Code is in file test.py Step 1. Copy code snippet to test.py
# test.py
def test_function(
very_long_dictionary: dict, # pyright:ignore
):
something = [
a["foo"]
for a in # pyright: ignore[reportUnknownVariableType]
(
very_long_dictionary.get(
"longer_key_name" # pyright: ignore
)
or []
)
]
Step 2. Run ruff format --no-cache test.py
.
It should look like this. This is a valid case and is a perfectly valid solution.
# test.py
def test_function(
very_long_dictionary: dict, # pyright:ignore
):
something = [
a["foo"]
for a in ( # pyright: ignore[reportUnknownVariableType]
very_long_dictionary.get(
"longer_key_name" # pyright: ignore
)
or []
)
]
Step 3. Run ruff format --no-cache test.py
.
It should now look like this which is incorrect. The comment isn't on the correct line and any further ruff format --no-cache test.py
will keep the code as is.
# test.py
def test_function(
very_long_dictionary: dict, # pyright:ignore
):
something = [
a["foo"]
for a in
( # pyright: ignore[reportUnknownVariableType]
very_long_dictionary.get(
"longer_key_name" # pyright: ignore
)
or []
)
]
I think this maybe be related to this other issue that I am creating to take about differences in no_cache. Uncertain whether it is the no_cache causing the problem or the way that the pragma comment is formatted.
@MichaReiser Do you have any code pointers of why there might be instability here? I'm trying to look at https://github.com/astral-sh/ruff/pull/8431 to see if there is anything there but uncertain.
This isn't related to caching but is an instability bug where the formatter requires two passes to reach the ultimate formatting (unfortunately, the worse). You can see this in the Playground where I copied the formatted code of the input as the second example.
The issue comes from that the comment is a dangling comment of the comprehension when formatting the input source, but becomes a leading comment of b.get
when formatting it the second time.
A workaround to unblock you is to suppress formatting of the entire statement:
something = [
a
for a in # pyright: ignore[reportUnknownVariableType]
(
b.get(
"longer_key_name",
)
)
] # fmt: skip
Trying to enable ruff formatter on my repo and am getting an error when using in conjunction with a #pyright pragma comment. Its a bit convoluted of an example but this does error out. The
# pyright: ignore[reportUnknownVariableType]
should apply to thefor a in
line however keeps getting moved to where the parentheses is. If I try and move the parentheses or comment to the same line, it moves it back to what it looks like below. Interesting enough, in VsCode it moves the parentheses on the first save and then moves parentheses and comment later again on subsequent saves/running from terminal.Code example:
Running on ruff v0.1.3. Edit: Error still shows up on v0.1.4.
No specific rules for ruff format and config looks like this