Skip to content

RUF008/RUF009 False positive on ClassVar #4077

@mishamsk

Description

@mishamsk

Hi, I believe RUF008 should not be triggered for ClassVar's. E.g. for the following snippet:

from dataclasses import dataclass
from typing import ClassVar

@dataclass
class Foo:
    bar: ClassVar[list[str]] = [] # RUF008 Do not use mutable default values for dataclass attributes

The command you invoked: ruff check path/to/snippet.py
The current Ruff version: ruff 0.0.262
The current Ruff settings: "RUF" enabled.

Actually, apparently there is no other way to initialize a classvar. Cause trying to use field with default_factory raise TypeError:

from dataclasses import dataclass, field
from typing import ClassVar

@dataclass
class Foo:
    bar: ClassVar[list[str]] = field(default_factory=list)

Same applies to function calls, e.g.:

from dataclasses import dataclass
from typing import ClassVar
import weakref

@dataclass
class Foo:
    bar: ClassVar[weakref.WeakValueDictionary[str, object]] = weakref.WeakValueDictionary()

Metadata

Metadata

Assignees

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