Rename flake8-django rules to match convention (#2861)

This commit is contained in:
Charlie Marsh
2023-02-13 10:30:04 -05:00
committed by GitHub
parent aeae63b7ea
commit 3c03e2cb2e
14 changed files with 61 additions and 55 deletions

View File

@@ -1111,9 +1111,9 @@ For more, see [flake8-django](https://pypi.org/project/flake8-django/) on PyPI.
| Code | Name | Message | Fix |
| ---- | ---- | ------- | --- |
| DJ001 | [model-string-field-nullable](https://github.com/charliermarsh/ruff/blob/main/docs/rules/model-string-field-nullable.md) | Avoid using `null=True` on string-based fields such as {field_name} | |
| DJ008 | [model-dunder-str](https://github.com/charliermarsh/ruff/blob/main/docs/rules/model-dunder-str.md) | Model does not define `__str__` method | |
| DJ013 | [receiver-decorator-checker](https://github.com/charliermarsh/ruff/blob/main/docs/rules/receiver-decorator-checker.md) | `@receiver` decorator must be on top of all the other decorators | |
| DJ001 | [nullable-model-string-field](https://github.com/charliermarsh/ruff/blob/main/docs/rules/nullable-model-string-field.md) | Avoid using `null=True` on string-based fields such as {field_name} | |
| DJ008 | [model-without-dunder-str](https://github.com/charliermarsh/ruff/blob/main/docs/rules/model-without-dunder-str.md) | Model does not define `__str__` method | |
| DJ013 | [non-leading-receiver-decorator](https://github.com/charliermarsh/ruff/blob/main/docs/rules/non-leading-receiver-decorator.md) | `@receiver` decorator must be on top of all the other decorators | |
### flake8-errmsg (EM)

View File

@@ -465,9 +465,13 @@ where
body,
..
} => {
if self.settings.rules.enabled(&Rule::ReceiverDecoratorChecker) {
if self
.settings
.rules
.enabled(&Rule::NonLeadingReceiverDecorator)
{
self.diagnostics
.extend(flake8_django::rules::receiver_decorator_checker(
.extend(flake8_django::rules::non_leading_receiver_decorator(
decorator_list,
|expr| self.resolve_call_path(expr),
));
@@ -779,15 +783,15 @@ where
decorator_list,
body,
} => {
if self.settings.rules.enabled(&Rule::ModelStringFieldNullable) {
if self.settings.rules.enabled(&Rule::NullableModelStringField) {
self.diagnostics
.extend(flake8_django::rules::model_string_field_nullable(
.extend(flake8_django::rules::nullable_model_string_field(
self, bases, body,
));
}
if self.settings.rules.enabled(&Rule::ModelDunderStr) {
if self.settings.rules.enabled(&Rule::ModelWithoutDunderStr) {
if let Some(diagnostic) =
flake8_django::rules::model_dunder_str(self, bases, body, stmt)
flake8_django::rules::model_without_dunder_str(self, bases, body, stmt)
{
self.diagnostics.push(diagnostic);
}

View File

@@ -557,9 +557,9 @@ ruff_macros::define_rule_mapping!(
RUF005 => rules::ruff::rules::UnpackInsteadOfConcatenatingToCollectionLiteral,
RUF100 => rules::ruff::rules::UnusedNOQA,
// flake8-django
DJ001 => rules::flake8_django::rules::ModelStringFieldNullable,
DJ008 => rules::flake8_django::rules::ModelDunderStr,
DJ013 => rules::flake8_django::rules::ReceiverDecoratorChecker,
DJ001 => rules::flake8_django::rules::NullableModelStringField,
DJ008 => rules::flake8_django::rules::ModelWithoutDunderStr,
DJ013 => rules::flake8_django::rules::NonLeadingReceiverDecorator,
);
#[derive(EnumIter, Debug, PartialEq, Eq, RuleNamespace)]

View File

@@ -12,9 +12,9 @@ mod tests {
use crate::test::test_path;
use crate::{assert_yaml_snapshot, settings};
#[test_case(Rule::ModelStringFieldNullable, Path::new("DJ001.py"); "DJ001")]
#[test_case(Rule::ModelDunderStr, Path::new("DJ008.py"); "DJ008")]
#[test_case(Rule::ReceiverDecoratorChecker, Path::new("DJ013.py"); "DJ013")]
#[test_case(Rule::NullableModelStringField, Path::new("DJ001.py"); "DJ001")]
#[test_case(Rule::ModelWithoutDunderStr, Path::new("DJ008.py"); "DJ008")]
#[test_case(Rule::NonLeadingReceiverDecorator, Path::new("DJ013.py"); "DJ013")]
fn rules(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!("{}_{}", rule_code.code(), path.to_string_lossy());
let diagnostics = test_path(

View File

@@ -1,8 +1,10 @@
mod helpers;
mod model_dunder_str;
mod model_string_field_nullable;
mod receiver_decorator_checker;
pub use model_without_dunder_str::{model_without_dunder_str, ModelWithoutDunderStr};
pub use non_leading_receiver_decorator::{
non_leading_receiver_decorator, NonLeadingReceiverDecorator,
};
pub use nullable_model_string_field::{nullable_model_string_field, NullableModelStringField};
pub use model_dunder_str::{model_dunder_str, ModelDunderStr};
pub use model_string_field_nullable::{model_string_field_nullable, ModelStringFieldNullable};
pub use receiver_decorator_checker::{receiver_decorator_checker, ReceiverDecoratorChecker};
mod helpers;
mod model_without_dunder_str;
mod non_leading_receiver_decorator;
mod nullable_model_string_field;

View File

@@ -40,9 +40,9 @@ define_violation!(
/// def __str__(self):
/// return f"{self.field}"
/// ```
pub struct ModelDunderStr;
pub struct ModelWithoutDunderStr;
);
impl Violation for ModelDunderStr {
impl Violation for ModelWithoutDunderStr {
#[derive_message_formats]
fn message(&self) -> String {
format!("Model does not define `__str__` method")
@@ -50,7 +50,7 @@ impl Violation for ModelDunderStr {
}
/// DJ008
pub fn model_dunder_str(
pub fn model_without_dunder_str(
checker: &Checker,
bases: &[Expr],
body: &[Stmt],
@@ -61,7 +61,7 @@ pub fn model_dunder_str(
}
if !has_dunder_method(body) {
return Some(Diagnostic::new(
ModelDunderStr,
ModelWithoutDunderStr,
Range::from_located(class_location),
));
}

View File

@@ -38,9 +38,9 @@ define_violation!(
/// def my_handler(sender, instance, created, **kwargs):
/// pass
/// ```
pub struct ReceiverDecoratorChecker;
pub struct NonLeadingReceiverDecorator;
);
impl Violation for ReceiverDecoratorChecker {
impl Violation for NonLeadingReceiverDecorator {
#[derive_message_formats]
fn message(&self) -> String {
format!("`@receiver` decorator must be on top of all the other decorators")
@@ -48,7 +48,7 @@ impl Violation for ReceiverDecoratorChecker {
}
/// DJ013
pub fn receiver_decorator_checker<'a, F>(
pub fn non_leading_receiver_decorator<'a, F>(
decorator_list: &'a [Expr],
resolve_call_path: F,
) -> Vec<Diagnostic>
@@ -66,7 +66,7 @@ where
};
if i > 0 && is_receiver && !seen_receiver {
diagnostics.push(Diagnostic::new(
ReceiverDecoratorChecker,
NonLeadingReceiverDecorator,
Range::from_located(decorator),
));
}

View File

@@ -36,14 +36,14 @@ define_violation!(
/// class MyModel(models.Model):
/// field = models.CharField(max_length=255, default="")
/// ```
pub struct ModelStringFieldNullable {
pub struct NullableModelStringField {
pub field_name: String,
}
);
impl Violation for ModelStringFieldNullable {
impl Violation for NullableModelStringField {
#[derive_message_formats]
fn message(&self) -> String {
let ModelStringFieldNullable { field_name } = self;
let NullableModelStringField { field_name } = self;
format!("Avoid using `null=True` on string-based fields such as {field_name}")
}
}
@@ -58,7 +58,7 @@ const NOT_NULL_TRUE_FIELDS: [&str; 6] = [
];
/// DJ001
pub fn model_string_field_nullable(
pub fn nullable_model_string_field(
checker: &Checker,
bases: &[Expr],
body: &[Stmt],
@@ -74,7 +74,7 @@ pub fn model_string_field_nullable(
};
if let Some(field_name) = check_nullable_field(checker, value) {
errors.push(Diagnostic::new(
ModelStringFieldNullable {
NullableModelStringField {
field_name: field_name.to_string(),
},
Range::from_located(value),

View File

@@ -3,7 +3,7 @@ source: crates/ruff/src/rules/flake8_django/mod.rs
expression: diagnostics
---
- kind:
ModelStringFieldNullable:
NullableModelStringField:
field_name: CharField
location:
row: 7
@@ -14,7 +14,7 @@ expression: diagnostics
fix: ~
parent: ~
- kind:
ModelStringFieldNullable:
NullableModelStringField:
field_name: TextField
location:
row: 8
@@ -25,7 +25,7 @@ expression: diagnostics
fix: ~
parent: ~
- kind:
ModelStringFieldNullable:
NullableModelStringField:
field_name: SlugField
location:
row: 9
@@ -36,7 +36,7 @@ expression: diagnostics
fix: ~
parent: ~
- kind:
ModelStringFieldNullable:
NullableModelStringField:
field_name: EmailField
location:
row: 10
@@ -47,7 +47,7 @@ expression: diagnostics
fix: ~
parent: ~
- kind:
ModelStringFieldNullable:
NullableModelStringField:
field_name: FilePathField
location:
row: 11
@@ -58,7 +58,7 @@ expression: diagnostics
fix: ~
parent: ~
- kind:
ModelStringFieldNullable:
NullableModelStringField:
field_name: URLField
location:
row: 12
@@ -69,7 +69,7 @@ expression: diagnostics
fix: ~
parent: ~
- kind:
ModelStringFieldNullable:
NullableModelStringField:
field_name: CharField
location:
row: 16
@@ -80,7 +80,7 @@ expression: diagnostics
fix: ~
parent: ~
- kind:
ModelStringFieldNullable:
NullableModelStringField:
field_name: CharField
location:
row: 17
@@ -91,7 +91,7 @@ expression: diagnostics
fix: ~
parent: ~
- kind:
ModelStringFieldNullable:
NullableModelStringField:
field_name: SlugField
location:
row: 18
@@ -102,7 +102,7 @@ expression: diagnostics
fix: ~
parent: ~
- kind:
ModelStringFieldNullable:
NullableModelStringField:
field_name: EmailField
location:
row: 19
@@ -113,7 +113,7 @@ expression: diagnostics
fix: ~
parent: ~
- kind:
ModelStringFieldNullable:
NullableModelStringField:
field_name: FilePathField
location:
row: 20
@@ -124,7 +124,7 @@ expression: diagnostics
fix: ~
parent: ~
- kind:
ModelStringFieldNullable:
NullableModelStringField:
field_name: URLField
location:
row: 21

View File

@@ -3,7 +3,7 @@ source: crates/ruff/src/rules/flake8_django/mod.rs
expression: diagnostics
---
- kind:
ModelDunderStr: ~
ModelWithoutDunderStr: ~
location:
row: 6
column: 0
@@ -13,7 +13,7 @@ expression: diagnostics
fix: ~
parent: ~
- kind:
ModelDunderStr: ~
ModelWithoutDunderStr: ~
location:
row: 21
column: 0
@@ -23,7 +23,7 @@ expression: diagnostics
fix: ~
parent: ~
- kind:
ModelDunderStr: ~
ModelWithoutDunderStr: ~
location:
row: 36
column: 0

View File

@@ -3,7 +3,7 @@ source: crates/ruff/src/rules/flake8_django/mod.rs
expression: diagnostics
---
- kind:
ReceiverDecoratorChecker: ~
NonLeadingReceiverDecorator: ~
location:
row: 15
column: 1
@@ -13,7 +13,7 @@ expression: diagnostics
fix: ~
parent: ~
- kind:
ReceiverDecoratorChecker: ~
NonLeadingReceiverDecorator: ~
location:
row: 35
column: 1

View File

@@ -1,4 +1,4 @@
# model-dunder-str (DJ008)
# model-without-dunder-str (DJ008)
Derived from the **flake8-django** linter.

View File

@@ -1,4 +1,4 @@
# receiver-decorator-checker (DJ013)
# non-leading-receiver-decorator (DJ013)
Derived from the **flake8-django** linter.

View File

@@ -1,4 +1,4 @@
# model-string-field-nullable (DJ001)
# nullable-model-string-field (DJ001)
Derived from the **flake8-django** linter.