Skip to content

DJ001 not raised when field has type annotation #20906

@majutsushi

Description

@majutsushi

Summary

from django.db import models


class TestModel(models.Model):
    charfield1 = models.CharField(null=True) # Raises DJ001
    charfield2: models.CharField[str, str] = models.CharField(null=True) # No error
$ ruff check --select=DJ001 test.py
DJ001 Avoid using `null=True` on string-based fields such as `CharField`
 --> test.py:5:18
  |
4 | class TestModel(models.Model):
5 |     charfield1 = models.CharField(null=True)
  |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^
6 |     charfield2: models.CharField[str, str] = models.CharField(null=True)
  |

Found 1 error.

I discovered this when using https://github.com/typeddjango/django-stubs with pyright, which doesn't detect the types correctly without explicit annotations.

Version

ruff 0.14.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinggood first issueGood for newcomers

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions