astral-sh / rye

a Hassle-Free Python Experience
https://rye.astral.sh
MIT License
13.82k stars 467 forks source link

`rye add` moves inline TOML comments #1255

Open jamesbraza opened 3 months ago

jamesbraza commented 3 months ago

Steps to Reproduce

Before running rye add, here is part of my pyproject.toml:

[tool.rye]
dev-dependencies = [
    "pre-commit",
    "pylint-pydantic",
    "pylint>=3.2",  # Pin to keep recent"
    "pytest",
    "pytest-asyncio",
    "pytest-cov>=5",  # Pin to keep recent
    "pytest-sugar",
    "pytest-xdist",
    "refurb>=2",  # Pin to keep recent"
]
managed = true

Then running rye add --dev ipython, it becomes:

[tool.rye]
dev-dependencies = [
    "pre-commit",
    "pylint-pydantic",
    "pylint>=3.2",
    # Pin to keep recent"
    "pytest",
    "pytest-asyncio",
    "pytest-cov>=5",
    # Pin to keep recent
    "pytest-sugar",
    "pytest-xdist",
    "refurb>=2",
    # Pin to keep recent"
    "ipython>=8.26.0",
]
managed = true

We can see the inline comments were auto wrapped to the next line, which is undesirable

Also more of an aside (that can be auto-fixed by https://github.com/pappasam/toml-sort), rye add didn't respect the alphabetization of my dependencies.

Expected Result

Not moving inline TOML comments

Actual Result

Rye moved inline TOML comments to the next line

Version Info

rye 0.36.0 commit: 0.36.0 (2024-07-07) platform: macos (aarch64) self-python: cpython@3.12.3 symlink support: true uv enabled: true

Stacktrace

No response

mitsuhiko commented 3 months ago

Unfortunately that is a limitation of the underlying toml-edit library. The actual source of the issue though from where I'm standing is that using a massive array for dependencies is always going to cause problems for tools like rye when they poke around in toml files as the "meaningful" comment is basically after the comma so on the next item.

jamesbraza commented 3 months ago

Thanks for the response, I hear what you're saying. I wonder if toml-sort can come to the rescue here, so I made https://github.com/pappasam/toml-sort/issues/70