mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-30 05:44:56 +00:00
Apply nullable-model-string-field to all classes (#2928)
This commit is contained in:
parent
9168a12679
commit
58269a918a
4 changed files with 87 additions and 17 deletions
|
@ -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)
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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
|
||||
|
@ -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(),
|
||||
|
@ -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;
|
||||
};
|
||||
|
|
|
@ -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: ~
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue