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

Warn about incompatible formatter options #8088

Merged
merged 1 commit into from Oct 23, 2023

Conversation

MichaReiser
Copy link
Member

Summary

Warn users about options incompatible with the formatter when using ruff format

Closes #7646

Test Plan

Added integration test

@MichaReiser
Copy link
Member Author

MichaReiser commented Oct 20, 2023

Current dependencies on/for this PR:

This comment was auto-generated by Graphite.

Rule::BadQuotesInlineString,
Rule::BadQuotesMultilineString,
Rule::BadQuotesDocstring,
Rule::AvoidableEscapedQuote,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One tricky thing here is that these don't necessarily conflict with the formatter. Some of them do overlap, but in a way that's at least consistent. Some of them are also arguably complimentary (like MissingTrailingComma, I think, is arguably complimentary, since it will insert trailing commas that the formatter will then respect).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

COM812 is conflicting because it disallows:

class PhotoMetadata:
    """Metadata about a photo."""

    def __init__(
        self, file: Path, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u
    ):
        ...

My goal is to only warn about rules that are incompatible. E.g. I excluded

  • E501 because there are valid use cases to use it alongside the formatter, even though it's mostly redundant
  • E701: while redundant, it doesn't conflict with the formatter.

Is your feedback mainly about the naming of the variables or the wording of the warning message? I'm trying to understand what I should change.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'm just trying to understand what we consider to be a conflict (and, based on that answer, perhaps looking to refine the list, or change the wording of the message).

For example, above, if you ran the linter, wouldn't COM812 just insert a trailing comma at the end, and then the formatter would in turn split the arguments over multiple lines? So it's not like the two tools would go back and forth endlessly disagreeing with one another, as they would if you set BadQuotes to divergent values. The COM812 case feels like it fits some definition of a conflict though, like: formatted code can introduce these violations.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example, above, if you ran the linter, wouldn't COM812 just insert a trailing comma at the end, and then the formatter would in turn split the arguments over multiple lines?

That's correct. But it requires that you need to run formatter, linter, and formatter again to get a stable result which doesn't seem ideal. I see incompatibilities as everything that leads to a bad experience.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, anything that might trigger or require some command to be repeated, even if that cycle isn't infinite. In that case, we probably want to include ISC001?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably we should. This rule won't be conflicting with the preview style but the formatter could produce code today that conflicts with it


if !incompatible_rules.is_empty() {
incompatible_rules.sort();
warn!("The following rules can cause conflicts when used with the formatter: {}. We recommend disabling these rules when using Ruff's formatter to avoid unexpected behavior. To disable the rules, change your configuration and either remove the rules from the `select` or `extend-select` options or add the rules to the `ignore` option.", incompatible_rules.join(", "))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small nudges on the comment:

The following rules may cause conflicts when used with the formatter: {}.
To avoid unexpected behavior, we recommend disabling these rules, either
by removing them from your `select` or `extend-select`, or adding them to
your `ignore`.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"your ignore" sounds a bit strange. I used your suggestion but changed it to "from the select or extend-select configuration.

}

if !incompatible_options.is_empty() {
warn!("The following isort options are incompatible with the formatter: {}. We recommend disabling these isort options when using Ruff's formatter by removing them from the configuration.", incompatible_options.join(", "))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small nudges on the comment:

The following isort options may cause conflicts when used with the formatter: {}.
To avoid unexpected behavior, we recommend disabling these options by removing
them from your configuration.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much better

@MichaReiser MichaReiser force-pushed the warn-about-incompatible-options branch from d9c9718 to e1e53d2 Compare October 23, 2023 08:59
@MichaReiser MichaReiser changed the base branch from rename-tab-size-to-tab-width to main October 23, 2023 08:59
@MichaReiser MichaReiser enabled auto-merge (squash) October 23, 2023 09:00
@github-actions
Copy link

github-actions bot commented Oct 23, 2023

PR Check Results

Ecosystem

✅ ecosystem check detected no changes.

@MichaReiser MichaReiser reopened this Oct 23, 2023
@MichaReiser MichaReiser merged commit 08519e2 into main Oct 23, 2023
31 checks passed
@MichaReiser MichaReiser deleted the warn-about-incompatible-options branch October 23, 2023 10:04
@smackesey
Copy link

warning: The following isort options may cause conflicts when used with the formatter: 'isort.force-wrap-aliases'. To avoid unexpected behavior, we recommend disabling these options by removing them from the configuration.

This rule is fairly important to us and it's not clear how it conflicts, as we never had a conflict using this together with black.

@MichaReiser
Copy link
Member Author

MichaReiser commented Oct 26, 2023

@smackesey Thanks for your feedback. I'm sorry, I was over-cautious with the warnings. #8192 refines the warnings. You should no longer see it, as long as you don't set format.skip-magic-trailing-comma to true.

@charliermarsh
Copy link
Member

@smackesey - We're planning to cut a release today that will include that fix.

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

Successfully merging this pull request may close these issues.

Warn about options conflicting with the formatter
3 participants