astral-sh / astral-sh/ruff

RUF012 triggers many false positives (are they really? they are correct) in some projects

Open
#5,243 34 comments 15 reactions 1 assignee Claimed by @charliermarsh View on GitHub
needs-decision rule
Dominant language
Rust
Stars
49.6k
Forks
2.4k
Avg merge
2d 1h
Merged PRs (30d)
458

Description

Hello 👋🏽

In this [PR](https://github.com/astral-sh/ruff/pull/4390) RUF012 was extended to apply to non-dataclass classes.

This has the (maybe desirable, maybe not, but was surprising to me) effect of _forcing typing_ for a RUF rule, in places where you would not necessarily use typing (if you did not want to).

For instance, in Django Rest Framework ([from their docs](https://www.django-rest-framework.org/api-guide/serializers/#modelserializer)):

```python
class AccountSerializer(serializers.ModelSerializer):
class Meta:
model = Account
fields = ['id', 'account_name', 'users', 'created'] # RUF012
```

Or in Django [itself](https://docs.djangoproject.com/en/4.2/ref/models/options/#django.db.models.Options.indexes):

```python
class Customer(models.Model):
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)

class Meta:
indexes = [
models.Index(fields=["last_name", "first_name"]), # RUF012
models.Index(fields=["first_name"], name="first_name_idx"), # RUF012
]
```

You could argue that's good, but at least to me, it's _different_ from dataclasses, since in there you are required to use typing to even use them, so they are opt-in even before you use ruff. You could also argue both Django and Django Rest Framework should be recommending tuples instead.

Isn't this too trigger happy for a RUF rule? Does it make sense to split it from the dataclass one?

EDIT: Actually, it is a separated code already 🤦🏽

In any case, feel free to close if this is just what it is! Ignoring the rule is easy for these kind of projects.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.