astral-sh / uv

An extremely fast Python package and project manager, written in Rust.
https://docs.astral.sh/uv
Apache License 2.0
26.02k stars 762 forks source link

Inconsistency with comments on uv add #8982

Open inoa-jboliveira opened 2 days ago

inoa-jboliveira commented 2 days ago

uv 0.5.1 (f399a5271 2024-11-08)

When you have a comment after a dependency that is not the last one and a package is to be added right after it, the new package will take the comment for itself

$ cat pyproject.toml
[project]
name = "myproject"
version = "0.1.0"
description = "Add your description here"
requires-python = ">=3.12"
dependencies = [
    "django>5",
    "numpy>=2.1.3", # Hello I like numpy
    "zeep",
]

Add new package that will be right after numpy

$ uv add pandas
Resolved 22 packages in 21ms
Installed 3 packages in 340ms
 + pandas==2.2.3
 + python-dateutil==2.9.0.post0
 + six==1.16.0

Unexpected

$ cat pyproject.toml
[project]
name = "myproject"
version = "0.1.0"
description = "Add your description here"
requires-python = ">=3.12"
dependencies = [
    "django>5",
    "numpy>=2.1.3",
    "pandas>=2.2.3", # Hello I like numpy
    "zeep",
]
charliermarsh commented 2 days ago

Thanks! I hate comments.

inoa-jboliveira commented 2 days ago

In case it helps, I added "zeep" to the example above because if numpy were the last package, the comment would not be misplaced.

flyaroundme commented 2 days ago

Yeah it is like that because when parsing with toml_edit this kind of comment is not treated as "the suffix of the second entry", but as "the prefix to the last line", and the invariant that is sustained here after adding is that this is "the prefix to the last line" 🤯