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

Consider removing ISC001 from the conflict list with ruff format #8272

Closed
yakMM opened this issue Oct 27, 2023 · 16 comments · Fixed by #13928
Closed

Consider removing ISC001 from the conflict list with ruff format #8272

yakMM opened this issue Oct 27, 2023 · 16 comments · Fixed by #13928
Labels
formatter Related to the formatter needs-decision Awaiting a decision from a maintainer

Comments

@yakMM
Copy link

yakMM commented Oct 27, 2023

Reminder on the rule: Checks for implicitly concatenated strings on a single line.

Why I believe there is actually no conflict with ruff format on default settings:

Imagine a situation where the formatter puts two strings on the same line:
foo = "foo" "bar"

If the rule is activated (and a fix available), the line will become:
foo = "foobar"

Which will not be further modified by ruff format.

However, if the rule is not activated, it will stay like:
foo = "foo" "bar"

I assume some people will want to be warned about this appearing because of the formatter and will want to fix it, hence ISC001 being useful alongside ruff format.

Am I missing something on the incompatibility between this rule and ruff format?

@MichaReiser MichaReiser added formatter Related to the formatter needs-decision Awaiting a decision from a maintainer labels Oct 27, 2023
@MichaReiser
Copy link
Member

Thanks for reporting and opening a new issue.

I think we didn't make an explicit decision, but we merged the improvements we had to include them in 0.1.3

Rules and options that are compatible with the formatter should have the following properties:

  1. Formatting the code doesn't introduce new violations; to prevent that, it is necessary to run check --fix, format, check --fix, ... until you get a stable result. This especially becomes important when we integrate format into check (and check --fix)
  2. Formatting doesn't undo an intent that you explicitly expressed. For example, split-on-trailing-comma=trueconflicts with format.skip-magic-trailing-comma=false because the formatter would remove the trailing comma, undoing your expressed intent that the aliases should be wrapped. This is an extension of 1, but I like to list it separately because it is not just incompatible with the rule, it does not uphold the expressed intent of users.

I do see how ISC001 is useful in combination with the formatter, and it doesn't violate the 2nd property, but it does violate the first property (so does E501, so maybe I shouldn't have removed that one).

I don't have a good answer yet about what we should do about these rules. Warning seems appropriate because you can run into situations where you must run ruff check --fix a second time after running ruff format.

@yakMM
Copy link
Author

yakMM commented Oct 27, 2023

I fully agree with the intent 2., no doubt about it.

I'm mixed about 1. It is easily fixed if you run the formatter before the linter, thing that was mandatory at the time of black and flake8: the formatter would preemptively fix some code that would otherwise be a violation for the linter (lines too long for example, would be fixed by the formatter).

I get that this way of thinking is a bit changed because of the --fix flag, but still I believe it's more appropriate to have a pipeline with formatting first and linter as second, as you don't expect check --fix to "break the format" of some code, unless you have actual contradictions within your rules (breaking point 2.)

@yakMM
Copy link
Author

yakMM commented Oct 27, 2023

If we imagine a world in which format and check are combined, I believe we can expect format + fix according to the rules, and the the remaining violations are displayed to the user. In this context, having ISC001 on would not bring any contradiction, it would just specify "format my code and make sure strings are concatenated", which is exactly what I want to express in my pipeline, by running format and then check with ISC001

@yakMM
Copy link
Author

yakMM commented Oct 27, 2023

Anyways, thank you very much for the openness and allowing discussions about all this, as well as the quick iterative fixes

@MichaReiser
Copy link
Member

I get that this way of thinking is a bit changed because of the --fix flag, but still I believe it's more appropriate to have a pipeline with formatting first and linter as second, as you don't expect check --fix to "break the format" of some code, unless you have actual contradictions within your rules (breaking point 2.)

Unfortunately, this is not guaranteed. Applying an autofixes may produce code that now exceeds the preferred line width or the formatter can now collapse multiple lines. That means, it is generally necessary to run the formatter after linting.

@yakMM
Copy link
Author

yakMM commented Oct 27, 2023

I get that this way of thinking is a bit changed because of the --fix flag, but still I believe it's more appropriate to have a pipeline with formatting first and linter as second, as you don't expect check --fix to "break the format" of some code, unless you have actual contradictions within your rules (breaking point 2.)

Unfortunately, this is not guaranteed. Applying an autofixes may produce code that now exceeds the preferred line width or the formatter can now collapse multiple lines. That means, it is generally necessary to run the formatter after linting.

Oh really? I'm surprised as rules like SIM108 do not trigger if there is no place for the fix within the specified line-length. Maybe it depends on the rules then

@MichaReiser
Copy link
Member

Yeah, some rules have this check, but not all of them. However, all fixes that remove (or change code) may reduce the length of lines, possibly causing reformats.

@yakMM
Copy link
Author

yakMM commented Oct 29, 2023

Yeah, some rules have this check, but not all of them. However, all fixes that remove (or change code) may reduce the length of lines, possibly causing reformats.

Ah okay I get it. It would be best if the fixes of ruff check --fix would be compliant with the formatter settings imo, but I see the ruff approach is more to run format after check.

In that case should I go back advocating for a way to suppress warning about rules breaking point 1?

My use case is to be warned about implicitly concatenated strings on a single line without warnings ideally.

@tylerlaprade
Copy link
Contributor

I really don't want warnings every time I run ruff format. I might personally know that it's safe to ignore, but this is bound to confuse more junior members of my team. For reference, when I previously pre-emptively included FURB in our config so it would be enabled as soon as it was out of preview, they saw the warnings about --preview when running ruff format, thought they had to run ruff format --preview instead, and ended up with >80 changed files in an otherwise tiny PR.

This rule is not incompatible with the formatter in the same way as ISC003 or E501. Moreover, disabling this rule would easily allow bugs to creep in, because it's never the case that someone intentionally concatenates two strings in a line without an f-string or + sign.

@MichaReiser
Copy link
Member

I hear your feedback and I see how ISC001 is useful even when used with the formatter. The issue is on my to do list I just haven't gotten around to look at it in more detail.

@MichaReiser MichaReiser added this to the Formatter: Stable milestone Dec 14, 2023
@arthur-st
Copy link

@tylerlaprade What do you mean under ISC003 incompatibility with the formatter? Doesn't seem to be incompatible as per the docs or the formatter behaviour, at least.

@MichaReiser
Copy link
Member

MichaReiser commented Jan 10, 2024

One option for addressing this issue is to change our formatter, see #9457. However, there's a caveat. The new formatting would be non-reversible and I'm undecided if we should accept that or wait until we implement Black's string processing preview style.

Please comment on the linked issue if you would object to the formatter making this change automatically for you.

@steve-mavens
Copy link

steve-mavens commented Apr 25, 2024

I don't know if this is useful to anyone, but my current local lint script runs:

ruff . --fix
ruff format .
ruff . --extend-select ISC001

If the final check fails then I assume manual intervention will be required to make sure my check/format config is compatible, or perhaps to manually reformat some bit of code that's triggering incompatibilities. In some cases re-runs without intervention could reach a stable state.

Is this the expected usage of ruff? If so then it does feel like ruff itself could provide that workflow in a combined "fix my code using all my config" command. Currently it feels like ruff check and ruff format are two separate tools that happen to be in the same executable, with a bit of work left to do meet the potential goal of bringing black formatting into ruff "proper", in the way that (for example) isort formatting already has been. Which is fine, because both ruff commands work really well, it just requires a bit of research to figure out how to run ruff at the moment.

DimitriPapadopoulos added a commit to DimitriPapadopoulos/mriqc that referenced this issue May 4, 2024
	ISC001 Implicitly concatenated string literals on one line

This rule is currently disabled because it conflicts with the formatter:
	astral-sh/ruff#8272
DimitriPapadopoulos added a commit to DimitriPapadopoulos/mriqc that referenced this issue May 4, 2024
	ISC001 Implicitly concatenated string literals on one line

This rule is currently disabled because it conflicts with the formatter:
	astral-sh/ruff#8272
DimitriPapadopoulos added a commit to DimitriPapadopoulos/nireports that referenced this issue May 4, 2024
	ISC001 Implicitly concatenated string literals on one line

This rule is currently disabled because it conflicts with the formatter:
	astral-sh/ruff#8272
DimitriPapadopoulos added a commit to DimitriPapadopoulos/niworkflows that referenced this issue May 4, 2024
	ISC001 Implicitly concatenated string literals on one line

This rule is currently disabled because it conflicts with the formatter:
	astral-sh/ruff#8272
DimitriPapadopoulos added a commit to DimitriPapadopoulos/fmriprep that referenced this issue May 4, 2024
	ISC001 Implicitly concatenated string literals on one line

This rule is currently disabled because it conflicts with the formatter:
	astral-sh/ruff#8272
DimitriPapadopoulos added a commit to DimitriPapadopoulos/mne-python that referenced this issue May 5, 2024
	ISC001 Implicitly concatenated string literals on one line

This rule is currently disabled because it conflicts with the formatter:
	astral-sh/ruff#8272
DimitriPapadopoulos added a commit to DimitriPapadopoulos/neurospin_to_bids that referenced this issue May 5, 2024
	ISC001 Implicitly concatenated string literals on one line

This rule is currently disabled because it conflicts with the formatter:
	astral-sh/ruff#8272
DimitriPapadopoulos added a commit to DimitriPapadopoulos/nibabel that referenced this issue May 5, 2024
	ISC001 Implicitly concatenated string literals on one line

This rule is currently disabled because it conflicts with the formatter:
	astral-sh/ruff#8272
DimitriPapadopoulos added a commit to DimitriPapadopoulos/mne-python that referenced this issue May 5, 2024
	ISC001 Implicitly concatenated string literals on one line

This rule is currently disabled because it conflicts with the formatter:
	astral-sh/ruff#8272
DimitriPapadopoulos added a commit to DimitriPapadopoulos/mne-python that referenced this issue May 5, 2024
	ISC001 Implicitly concatenated string literals on one line

This rule is currently disabled because it conflicts with the formatter:
	astral-sh/ruff#8272
DimitriPapadopoulos added a commit to DimitriPapadopoulos/cryptography that referenced this issue May 5, 2024
	ISC001 Implicitly concatenated string literals on one line

This rule is currently disabled because it conflicts with the formatter:
	astral-sh/ruff#8272
reaperhulk pushed a commit to pyca/cryptography that referenced this issue May 5, 2024
ISC001 Implicitly concatenated string literals on one line

This rule is currently disabled because it conflicts with the formatter:
	astral-sh/ruff#8272
@mscheifer
Copy link

mscheifer commented May 7, 2024

Another option could be a way to disable the warning per rule like:

[tool.ruff.format]
disable-rule-conflict-warning = ["ICS001"]

That way I'm explicitly stating that I understand that I might have to run the formatter and check --fix back and forth, and maybe do some manual intervention even, to get it to all pass, for this rule specifically.

DimitriPapadopoulos added a commit to neurospin/neurospin_to_bids that referenced this issue May 14, 2024
	ISC001 Implicitly concatenated string literals on one line

This rule is currently disabled because it conflicts with the formatter:
	astral-sh/ruff#8272
bpinsard pushed a commit to bpinsard/smriprep that referenced this issue May 16, 2024
ISC001 Implicitly concatenated string literals on one line

This rule is currently disabled because it conflicts with the formatter:
	astral-sh/ruff#8272
effigies pushed a commit to nipreps/niworkflows that referenced this issue Jun 10, 2024
ISC001 Implicitly concatenated string literals on one line

This rule is currently disabled because it conflicts with the formatter:
	astral-sh/ruff#8272
@VeckoTheGecko

This comment was marked as resolved.

tsalo pushed a commit to tsalo/nireports that referenced this issue Aug 16, 2024
	ISC001 Implicitly concatenated string literals on one line

This rule is currently disabled because it conflicts with the formatter:
	astral-sh/ruff#8272
StevenMaude added a commit to opensafely-core/job-server that referenced this issue Aug 22, 2024
It gives a warning when running the Ruff formatter.

This rule can cause the linter and formatter to conflict, meaning you need to
rerun `ruff check --fix` again after `ruff format`.

See astral-sh/ruff#8272 (comment)
StevenMaude added a commit to opensafely-core/job-server that referenced this issue Aug 22, 2024
It gives a warning when running the Ruff formatter.

This rule can cause the linter and formatter to conflict, meaning you need to
rerun `ruff check --fix` again after `ruff format`.

See astral-sh/ruff#8272 (comment)
@MichaReiser
Copy link
Member

MichaReiser commented Oct 25, 2024

Preview mode now comes with #13663. This isn't enough to fix the incompatibility because the preview style doesn't support triple quoted and raw strings.

A simple way of solving the incompatibility is to always format raw and triple quoted implicitly concatenated strings over multiple lines (except when in unparenthesized statement positions). This fixes the incompatibility and, arguably, also makes the implicit concat more visible.

a = r"test" "other"

would be formatted as

a = (
	r"test"
	"other"
)

Any thoughts on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
formatter Related to the formatter needs-decision Awaiting a decision from a maintainer
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants