astral-sh / ruff-vscode

A Visual Studio Code extension with support for the Ruff linter.
Other
1.06k stars 53 forks source link

VSCode extension question: Auto-fix and Formatter choose different quotes #393

Closed parametalol closed 8 months ago

parametalol commented 8 months ago

Hello,

The problem

When I choose to format the document, it puts strings into double quotes, but when I ask to fix problems, it changes them back to single quotes.

Configuration and environment

I installed the ruff (v2024.2.0) and Python (v2023.22.1) vscode extensions, and I guess I'm formatting Python files with 'ruff':

    "[python]": {
        "editor.defaultFormatter": "charliermarsh.ruff"
    },

I do not have the Microsoft black formatter extension installed.

My pyproject.toml says:

[tool.ruff]
select = [
    'E',
    'F',
    'W',
    'I',
    'YTT',
    'FA',
    'ISC',
    'ICN',
    'G',
    'C90',
    'N',
    'UP',
    'S',
    'B',
    'C4',
    'DTZ',
    'DJ',
    'PIE',
    'T20',
    'PT',
    'Q',
    'RSE',
    'RET',
    'SIM',
    'TID',
    'TCH',
    'INT',
    'PTH',
    'PL',
]
line-length = 120
target-version = 'py39'
src = ["src"]

[tool.ruff.per-file-ignores]
'*/tests/*' = ['S101']

[tool.ruff.pylint]
max-args = 8

[tool.ruff.flake8-quotes]
inline-quotes = 'single'

I didn't set extension to use external ruff binary, which is at version 0.1.14.

Python 3.12.1, Fedora 39.

Can I fix that somehow?

charliermarsh commented 8 months ago

It looks like you have flake8-quotes.inline-quotes enabled:

[tool.ruff.flake8-quotes]
inline-quotes = 'single'

So that'll cause the linter to use single quotes when fixing. You have two options...

  1. Remove that setting. Then the linter and formatter will both use double quotes (the defaults).
  2. Set the formatter to use single quotes, to match the linter:
[tool.ruff.format]
quote-style = "single"
parametalol commented 8 months ago

Awesome, thank you! I overlooked this setting somehow.

charliermarsh commented 8 months ago

No problem! I'm glad I could help.