PIE794: Removes value assignment when type annotation is on the line above
To avoid long lines for my variable declarations, sometimes I put the type hint for the variable on the line above. This avoids it getting reformatted into three lines by Black. A silly example:
class Hello:
world: str
world = "Something with enough characters that I want to put the type on a separate line above to avoid ugly parenthesis."
(Reformatted with Black because my line was too long when I put the type in it.)class Hello: world: str = ( "Something with enough characters that I want to put the type on a separate line above to avoid ugly parenthesis." )
When I run ruff with this config, the second line gets removed:
[tool.ruff]
select = ["PIE794"]
fix = true
Leaving just this behind...
class Hello:
world: str
At first I thought this could be updated to not count type type annotations as a a variable "definition". But given the context of the rule ("...can occur in large ORM model definitions"), I am not sure that would work. I think FastAPI uses dataclass-like classes for ORM stuff. And those just declare the type annotations.
# A Pydantic model
class User(BaseModel):
id: int
name: str
joined: date
The solution I am thinking of right now is not allowing two declarations of the same "type", where the types are assigning a value or just adding a type annotation. That would catch duplicate actual definitions in dataclass-like things and regular classes, while allowing me to do my formatting trick.
But the parenthesis really are not that bad, and maybe that solution is a lot of work. So this is a very tentative feature request.
Edit: Oh, I was running this on version 0.0.237.