Skip to content

Commit

Permalink
Apply nullable-model-string-field to all classes (astral-sh#2928)
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Feb 15, 2023
1 parent 9168a12 commit 58269a9
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 17 deletions.
11 changes: 10 additions & 1 deletion crates/ruff/resources/test/fixtures/flake8_django/DJ001.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,16 @@ class IncorrectModel(models.Model):
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)
textfield = SmthCharField(max_length=255, null=True)
slugfield = models.SlugField(max_length=255, null=True)
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff/src/checkers/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,7 @@ where
if self.settings.rules.enabled(&Rule::NullableModelStringField) {
self.diagnostics
.extend(flake8_django::rules::nullable_model_string_field(
self, bases, body,
self, body,
));
}
if self.settings.rules.enabled(&Rule::ModelWithoutDunderStr) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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::checkers::ast::Checker;
use crate::registry::Diagnostic;
use crate::violation::Violation;
use ruff_macros::{define_violation, derive_message_formats};
use rustpython_parser::ast::Constant::Bool;
use rustpython_parser::ast::{Expr, ExprKind, Stmt, StmtKind};

use super::helpers;

define_violation!(
/// ## What it does
Expand Down Expand Up @@ -58,21 +61,13 @@ const NOT_NULL_TRUE_FIELDS: [&str; 6] = [
];

/// DJ001
pub fn nullable_model_string_field(
checker: &Checker,
bases: &[Expr],
body: &[Stmt],
) -> Vec<Diagnostic> {
if !bases.iter().any(|base| helpers::is_model(checker, base)) {
return vec![];
}

pub fn nullable_model_string_field(checker: &Checker, body: &[Stmt]) -> Vec<Diagnostic> {
let mut errors = Vec::new();
for statement in body.iter() {
let StmtKind::Assign {value, ..} = &statement.node else {
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(
NullableModelStringField {
field_name: field_name.to_string(),
Expand All @@ -84,7 +79,7 @@ pub fn nullable_model_string_field(
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 {
return None;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,70 @@ expression: diagnostics
column: 57
fix: ~
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: ~

0 comments on commit 58269a9

Please sign in to comment.