Skip to content

Location of inline comment not preserved when using uv add for an optional dependencyΒ #21006

Closed

Description

@namurphy

Summary

When running a uv add command to update an optional dependency, the position of an inline comment is shifted to a different line. This particular optional dependency is at the end of a category of optional dependencies and lacks a trailing comma before the inline comment.

Minimum reproducible example

I start from the following pyproject.toml:

[project]
name = "grumblemuffins"
license = "BSD-3-Clause"
license-files = ["LICENSE.rst", "licenses/*.rst"]
requires-python = ">=3.11"
dependencies = [ ]
dynamic = [ "version" ]

[project.optional-dependencies]
typing = [
    "pandas-stubs>=2.0.2",
    "narwhals>=1.42.0" # narwhals are toothed whales native to the Arctic
]

[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"

I run the command:

uv add --optional=typing 'narwhals>=1.42'

The section is changed to

[project.optional-dependencies]
typing = [
    "pandas-stubs>=2.0.2", # narwhals are toothed whales native to the Arctic
    "narwhals>=1.42",
]

The inline comment is shifted to the line above. I originally found this for Astropy's pyproject.toml.

Workaround

Adding a trailing comma prevents the comment from being shifted to a different line. Use trailing commas, everyone! πŸŽ‚πŸŽ†πŸͺ©πŸŒŒπŸ₯¦πŸ˜…

Platform

Linux 5.14.0-687.24.1.el9_8.x86_64 x86_64 GNU/Linux

Version

uv 0.12.2 (x86_64-unknown-linux-gnu)

Python version

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions