Apply nullable-model-string-field to all classes (#2928)

This commit is contained in:
Charlie Marsh 2023-02-15 10:54:14 -05:00 committed by GitHub
parent 9168a12679
commit 58269a918a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 87 additions and 17 deletions

View file

@ -12,7 +12,16 @@ class IncorrectModel(models.Model):
urlfield = models.URLField(max_length=255, null=True) urlfield = models.URLField(max_length=255, null=True)
class IncorrectModelWithAliasedBase(DjangoModel): class IncorrectModelWithAlias(DjangoModel):
charfield = DjangoModel.CharField(max_length=255, null=True)
textfield = SmthCharField(max_length=255, null=True)
slugfield = models.SlugField(max_length=255, null=True)
emailfield = models.EmailField(max_length=255, null=True)
filepathfield = models.FilePathField(max_length=255, null=True)
urlfield = models.URLField(max_length=255, null=True)
class IncorrectModelWithoutSuperclass:
charfield = DjangoModel.CharField(max_length=255, null=True) charfield = DjangoModel.CharField(max_length=255, null=True)
textfield = SmthCharField(max_length=255, null=True) textfield = SmthCharField(max_length=255, null=True)
slugfield = models.SlugField(max_length=255, null=True) slugfield = models.SlugField(max_length=255, null=True)

View file

@ -786,7 +786,7 @@ where
if self.settings.rules.enabled(&Rule::NullableModelStringField) { if self.settings.rules.enabled(&Rule::NullableModelStringField) {
self.diagnostics self.diagnostics
.extend(flake8_django::rules::nullable_model_string_field( .extend(flake8_django::rules::nullable_model_string_field(
self, bases, body, self, body,
)); ));
} }
if self.settings.rules.enabled(&Rule::ModelWithoutDunderStr) { if self.settings.rules.enabled(&Rule::ModelWithoutDunderStr) {

View file

@ -1,11 +1,14 @@
use super::helpers; use rustpython_parser::ast::Constant::Bool;
use rustpython_parser::ast::{Expr, ExprKind, Stmt, StmtKind};
use ruff_macros::{define_violation, derive_message_formats};
use crate::ast::types::Range; use crate::ast::types::Range;
use crate::checkers::ast::Checker; use crate::checkers::ast::Checker;
use crate::registry::Diagnostic; use crate::registry::Diagnostic;
use crate::violation::Violation; use crate::violation::Violation;
use ruff_macros::{define_violation, derive_message_formats};
use rustpython_parser::ast::Constant::Bool; use super::helpers;
use rustpython_parser::ast::{Expr, ExprKind, Stmt, StmtKind};
define_violation!( define_violation!(
/// ## What it does /// ## What it does
@ -58,21 +61,13 @@ const NOT_NULL_TRUE_FIELDS: [&str; 6] = [
]; ];
/// DJ001 /// DJ001
pub fn nullable_model_string_field( pub fn nullable_model_string_field(checker: &Checker, body: &[Stmt]) -> Vec<Diagnostic> {
checker: &Checker,
bases: &[Expr],
body: &[Stmt],
) -> Vec<Diagnostic> {
if !bases.iter().any(|base| helpers::is_model(checker, base)) {
return vec![];
}
let mut errors = Vec::new(); let mut errors = Vec::new();
for statement in body.iter() { for statement in body.iter() {
let StmtKind::Assign {value, ..} = &statement.node else { let StmtKind::Assign {value, ..} = &statement.node else {
continue continue
}; };
if let Some(field_name) = check_nullable_field(checker, value) { if let Some(field_name) = is_nullable_field(checker, value) {
errors.push(Diagnostic::new( errors.push(Diagnostic::new(
NullableModelStringField { NullableModelStringField {
field_name: field_name.to_string(), field_name: field_name.to_string(),
@ -84,7 +79,7 @@ pub fn nullable_model_string_field(
errors errors
} }
fn check_nullable_field<'a>(checker: &'a Checker, value: &'a Expr) -> Option<&'a str> { fn is_nullable_field<'a>(checker: &'a Checker, value: &'a Expr) -> Option<&'a str> {
let ExprKind::Call {func, keywords, ..} = &value.node else { let ExprKind::Call {func, keywords, ..} = &value.node else {
return None; return None;
}; };

View file

@ -134,4 +134,70 @@ expression: diagnostics
column: 57 column: 57
fix: ~ fix: ~
parent: ~ parent: ~
- kind:
NullableModelStringField:
field_name: CharField
location:
row: 25
column: 16
end_location:
row: 25
column: 64
fix: ~
parent: ~
- kind:
NullableModelStringField:
field_name: CharField
location:
row: 26
column: 16
end_location:
row: 26
column: 56
fix: ~
parent: ~
- kind:
NullableModelStringField:
field_name: SlugField
location:
row: 27
column: 16
end_location:
row: 27
column: 59
fix: ~
parent: ~
- kind:
NullableModelStringField:
field_name: EmailField
location:
row: 28
column: 17
end_location:
row: 28
column: 61
fix: ~
parent: ~
- kind:
NullableModelStringField:
field_name: FilePathField
location:
row: 29
column: 20
end_location:
row: 29
column: 67
fix: ~
parent: ~
- kind:
NullableModelStringField:
field_name: URLField
location:
row: 30
column: 15
end_location:
row: 30
column: 57
fix: ~
parent: ~