mirror of https://github.com/astral-sh/ruff
[`flake8-django`] Apply `DJ001` to annotated fields (#20907)
This commit is contained in:
parent
fa12fd0184
commit
8a73519b25
|
|
@ -46,3 +46,9 @@ class CorrectModel(models.Model):
|
||||||
max_length=255, null=True, blank=True, unique=True
|
max_length=255, null=True, blank=True, unique=True
|
||||||
)
|
)
|
||||||
urlfieldu = models.URLField(max_length=255, null=True, blank=True, unique=True)
|
urlfieldu = models.URLField(max_length=255, null=True, blank=True, unique=True)
|
||||||
|
|
||||||
|
|
||||||
|
class IncorrectModelWithSimpleAnnotations(models.Model):
|
||||||
|
charfield: models.CharField = models.CharField(max_length=255, null=True)
|
||||||
|
textfield: models.TextField = models.TextField(max_length=255, null=True)
|
||||||
|
slugfield: models.SlugField = models.SlugField(max_length=255, null=True)
|
||||||
|
|
|
||||||
|
|
@ -61,9 +61,14 @@ pub(crate) fn nullable_model_string_field(checker: &Checker, body: &[Stmt]) {
|
||||||
}
|
}
|
||||||
|
|
||||||
for statement in body {
|
for statement in body {
|
||||||
let Stmt::Assign(ast::StmtAssign { value, .. }) = statement else {
|
let value = match statement {
|
||||||
continue;
|
Stmt::Assign(ast::StmtAssign { value, .. }) => value,
|
||||||
|
Stmt::AnnAssign(ast::StmtAnnAssign {
|
||||||
|
value: Some(value), ..
|
||||||
|
}) => value,
|
||||||
|
_ => continue,
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(field_name) = is_nullable_field(value, checker.semantic()) {
|
if let Some(field_name) = is_nullable_field(value, checker.semantic()) {
|
||||||
checker.report_diagnostic(
|
checker.report_diagnostic(
|
||||||
DjangoNullableModelStringField {
|
DjangoNullableModelStringField {
|
||||||
|
|
|
||||||
|
|
@ -186,3 +186,32 @@ DJ001 Avoid using `null=True` on string-based fields such as `URLField`
|
||||||
30 | urlfield = models.URLField(max_length=255, null=True)
|
30 | urlfield = models.URLField(max_length=255, null=True)
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
|
|
||||||
|
|
||||||
|
DJ001 Avoid using `null=True` on string-based fields such as `CharField`
|
||||||
|
--> DJ001.py:52:35
|
||||||
|
|
|
||||||
|
51 | class IncorrectModelWithSimpleAnnotations(models.Model):
|
||||||
|
52 | charfield: models.CharField = models.CharField(max_length=255, null=True)
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
53 | textfield: models.TextField = models.TextField(max_length=255, null=True)
|
||||||
|
54 | slugfield: models.SlugField = models.SlugField(max_length=255, null=True)
|
||||||
|
|
|
||||||
|
|
||||||
|
DJ001 Avoid using `null=True` on string-based fields such as `TextField`
|
||||||
|
--> DJ001.py:53:35
|
||||||
|
|
|
||||||
|
51 | class IncorrectModelWithSimpleAnnotations(models.Model):
|
||||||
|
52 | charfield: models.CharField = models.CharField(max_length=255, null=True)
|
||||||
|
53 | textfield: models.TextField = models.TextField(max_length=255, null=True)
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
54 | slugfield: models.SlugField = models.SlugField(max_length=255, null=True)
|
||||||
|
|
|
||||||
|
|
||||||
|
DJ001 Avoid using `null=True` on string-based fields such as `SlugField`
|
||||||
|
--> DJ001.py:54:35
|
||||||
|
|
|
||||||
|
52 | charfield: models.CharField = models.CharField(max_length=255, null=True)
|
||||||
|
53 | textfield: models.TextField = models.TextField(max_length=255, null=True)
|
||||||
|
54 | slugfield: models.SlugField = models.SlugField(max_length=255, null=True)
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue