ruff: RUF012 triggers many false positives (are they really? they are correct) in some projects
Hello šš½
In this PR 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):
class AccountSerializer(serializers.ModelSerializer):
class Meta:
model = Account
fields = ['id', 'account_name', 'users', 'created'] # RUF012
Or in Django itself:
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.
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 9
- Comments: 34 (14 by maintainers)
Commits related to this issue
- Allow `typing.Final` for `mutable-class-default annotations` (`RUF012`) (#5274) ## Summary See: https://github.com/astral-sh/ruff/issues/5243. — committed to astral-sh/ruff by charliermarsh 3 years ago
- discable RUF012 see https://github.com/astral-sh/ruff/issues/5243 — committed to xi/django-mfa3 by xi 3 years ago
- disable RUF012 see https://github.com/astral-sh/ruff/issues/5243 — committed to xi/django-mfa3 by xi 3 years ago
- Avoid `mutable-class-default` violations for Pydantic subclasses (#9187) Only applies to subclasses defined within the same file, as elsewhere. See: https://github.com/astral-sh/ruff/issues/5243#... — committed to astral-sh/ruff by charliermarsh 3 years ago
- Add `ClassVar` annotations on mutable `ModelAdmin` types for `ruff` See https://github.com/astral-sh/ruff/issues/5243 for RUF012 discussion Signed-off-by: Yury V. Zaytsev <yury@shurup.com> — committed to zyv/django-stubs by zyv 2 years ago
- Add `ClassVar` annotations on `Migration` class attributes See also: - https://github.com/astral-sh/ruff/issues/5243 - https://github.com/typeddjango/django-stubs/pull/2524 Signed-off-by: Yury V. Z... — committed to zyv/django-stubs by zyv 2 years ago
- Add `ClassVar` annotations on `Migration` class attributes (#2530) See also: - https://github.com/astral-sh/ruff/issues/5243 - https://github.com/typeddjango/django-stubs/pull/2524 Signed-off-by: Y... — committed to typeddjango/django-stubs by zyv 2 years ago
One of my use cases is the following:
This triggers
RUF012on the subclass, where I would have expected this to pass.Adding to the false positives, this is also flagging pydantic models which function similar to dataclasses in how they are defined but actually create new instances of objects when instantiating the class.
In this example,
foois flagged withRUF012: Mutable class attributes should be annotated with typing.ClassVarbut pydantic creates a new instance of the empty dict each time the class is instantiated. Itās not well documented if at all (https://github.com/pydantic/pydantic/discussions/3877) but is how it functions.Iād suggest a setting that lets you whitelist certain ancestor classes, with maybe default set to
Pydantic.BaseModelor whatever. Since there are multiple libs where the āproblemā syntax is part of a DSL.I am not sure if this is possible yet, but it might be with the new import resolver.
I think things can improve with this when we have red knot. Right now, ruff canāt really see the relationship between base classes and subclasses.
So you might have to put
ClassVarin thousands of subclasses, when it should be sufficient to just put it in one place.What
RUF012is trying to do, canāt be done well without a type-checker.My issue with RUF012 is that it triggers even when a parent class (type stub) has the attribute properly annotated.
Type:
Code:
No, itās not bad but just a limitation of Ruff which will be resolved with the ongoing red knot project.
@charliermarsh, I appreciate the fix in #5274 to omit pydantic models however, it does not omit classes that subclass user defined models.
If this is not something that ruff can determine on itās own, an option like
runtime-evaluated-base-classesfor this case would be an acceptable solution.For what itās worth, the Django project is against adding type hints, at least in the near future. You can read more in this PR and a technical board statement. This might be why it doesnāt recommend the
ClassVartype annotation (and annotations in general).For balance, however, the typed-django project also hasnāt gone all-in with
ClassVaras suggested in this PR (though a quick search of the codebase showsClassVaris still used in some places).Is it perhaps worth resolving a balance of only flagging a violation when type annotations are already used to annotate a variable?
I think this might be because it uses type alias.
I found Django migrations are an area where
ruffandmypymight not agree as far as this rule is concerned.Sample 1
ruffisnāt satisfied:Sample 2
ruffis satisfied, but notmypy:Sample 3
mypyis fine with it,ruffinsists:Related suggestion, have you considered a new category for āRuff Warningsā (
RUW?)? I think for things likeRUF012it could be nice to put them in a new category so that people could opt in to these kinds of lints separate from the other lints inRUF?Thanks for this! Iām a little busy this morning but Iāll get back to it later today and share my perspective š