tox-dev / toml-fmt

Format Python TOML configurations.
MIT License
3 stars 1 forks source link

Flattening of maps to sequences causing downstream errors in ruff #4

Open badrobit opened 1 month ago

badrobit commented 1 month ago

the following except from a pyproject.toml will be reformatted to the follow on example which when then passed into ruff the tool complains is invalid (errors provided). The expected behavior would be to leave them in the embedded sub-section or allow an option to say these specific sections must stay a "map" (example input option at the very bottom)

[tool.ruff]
target-version = "py312"
line-length = 120

[tool.ruff.lint]
select = [
  "A",
  "ARG",
  "BLE",
  "C4",
  "C90",
  "COM",
  "D",
  "E",
  "EM",
  "F",
  "FBT",
  "I",
  "INP",
  "ISC",
  "NPY",
  "PD",
  "PERF",
  "PIE",
  "PL",
  "PT",
  "PTH",
  "RUF",
  "S",
  "SIM",
  "T20",
  "TD",
]
per-file-ignores = [ "S101", "ARG001", "D" ]
pydocstyle = "google"
ignore-init-module-imports = true

Is converted to this by pyproject-fmt:

[tool.ruff]
target-version = "py312"
line-length = 120

lint.select = [
  "A",
  "ARG",
  "BLE",
  "C4",
  "C90",
  "COM",
  "D",
  "E",
  "EM",
  "F",
  "FBT",
  "I",
  "INP",
  "ISC",
  "NPY",
  "PD",
  "PERF",
  "PIE",
  "PL",
  "PT",
  "PTH",
  "RUF",
  "S",
  "SIM",
  "T20",
  "TD",
]
lint.per-file-ignores = [ "S101", "ARG001", "D" ]
lint.pydocstyle = "google"
lint.ignore-init-module-imports = true

When the updated pyproject is then used by ruff (during pre-commit) I get the following errors:

ruff.....................................................................Failed
- hook id: ruff
- exit code: 2

ruff failed
  Cause: Failed to parse <redacted>/pyproject.toml
  Cause: TOML parse error at line 60, column 1
   |
60 | lint.select = [
   | ^^^^
invalid type: sequence, expected a map

ruff-format..............................................................Failed
- hook id: ruff-format
- exit code: 2

ruff failed
  Cause: Failed to parse <redacted>/pyproject.toml
  Cause: TOML parse error at line 60, column 1
   |
60 | lint.select = [
   | ^^^^
invalid type: sequence, expected a map

How I have pyproject-fmt configured:

[tool.pyproject-fmt]
column_width = 120
indent = 2
max_supported_python = "3.12"

Possible configuration option:

[tool.pyproject-fmt]
column_width = 120
indent = 2
max_supported_python = "3.12"
retain_map_structure = [ "tool.ruff"] 

then in this case anything defined as tool.ruff or its subsections would keep the map while still allowing the flattening of maps -> sequences of others.