Skip to content

PIE804 autofix is destructive #18036

@tpgillam

Description

@tpgillam

Tested with ruff 0.11.8

Consider this (contrived) case of comments appearing in a dictionary definition:

from dataclasses import dataclass

@dataclass
class Moo:
    x: float
    y: float

Moo(**{
    # Interesting observation about x
    "x": 1.0,
    # Interesting observation about y
    "y": 2.0
    })

applying autofix for PIE804 (which is not marked unsafe) gives the following:

from dataclasses import dataclass


@dataclass
class Moo:
    x: float
    y: float


Moo(x=1.0, y=2.0)

which has dropped the comment.

Ideally I think the comments should be preserved, but otherwise perhaps this autofix should be marked as unsafe?


I came across this in the wild where previously some code constructed a big dictionary, and I was then porting it into a dataclass.. so was using the PIE804 autofix as above to save some typing. But then discovered that the comments got thrown away.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixesRelated to suggested fixes for violationshelp wantedContributions especially welcome

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions