-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[flake8-django] Apply DJ001 to annotated fields
#20907
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
| match statement { | ||
| Stmt::Assign(ast::StmtAssign { value, .. }) => { | ||
| if let Some(field_name) = is_nullable_field(value, checker.semantic()) { | ||
| checker.report_diagnostic( | ||
| DjangoNullableModelStringField { | ||
| field_name: field_name.to_string(), | ||
| }, | ||
| value.range(), | ||
| ); | ||
| } | ||
| } | ||
| Stmt::AnnAssign(ast::StmtAnnAssign { | ||
| value: Some(value), .. | ||
| }) => { | ||
| if let Some(field_name) = is_nullable_field(value, checker.semantic()) { | ||
| checker.report_diagnostic( | ||
| DjangoNullableModelStringField { | ||
| field_name: field_name.to_string(), | ||
| }, | ||
| value.range(), | ||
| ); | ||
| } | ||
| } | ||
| _ => continue, | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should avoid duplicating that much code. All we need is to extract value. Something like this should work
| match statement { | |
| Stmt::Assign(ast::StmtAssign { value, .. }) => { | |
| if let Some(field_name) = is_nullable_field(value, checker.semantic()) { | |
| checker.report_diagnostic( | |
| DjangoNullableModelStringField { | |
| field_name: field_name.to_string(), | |
| }, | |
| value.range(), | |
| ); | |
| } | |
| } | |
| Stmt::AnnAssign(ast::StmtAnnAssign { | |
| value: Some(value), .. | |
| }) => { | |
| if let Some(field_name) = is_nullable_field(value, checker.semantic()) { | |
| checker.report_diagnostic( | |
| DjangoNullableModelStringField { | |
| field_name: field_name.to_string(), | |
| }, | |
| value.range(), | |
| ); | |
| } | |
| } | |
| _ => continue, | |
| } | |
| let value = match statement { | |
| 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()) { | |
| checker.report_diagnostic( | |
| DjangoNullableModelStringField { | |
| field_name: field_name.to_string(), | |
| }, | |
| value.range(), | |
| ); | |
| } |
| class IncorrectModelWithAnnotations(models.Model): | ||
| charfield: models.CharField[str, str] = models.CharField(max_length=255, null=True) | ||
| textfield: models.TextField[str, str] = models.TextField(max_length=255, null=True) | ||
| slugfield: models.SlugField[str, str] = models.SlugField(max_length=255, null=True) | ||
| emailfield: models.EmailField[str, str] = models.EmailField(max_length=255, null=True) | ||
| filepathfield: models.FilePathField[str, str] = models.FilePathField(max_length=255, null=True) | ||
| urlfield: models.URLField[str, str] = models.URLField(max_length=255, null=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) | ||
| emailfield: models.EmailField = models.EmailField(max_length=255, null=True) | ||
| filepathfield: models.FilePathField = models.FilePathField(max_length=255, null=True) | ||
| urlfield: models.URLField = models.URLField(max_length=255, null=True) | ||
|
|
||
|
|
||
| class CorrectModelWithAnnotations(models.Model): | ||
| charfield: models.CharField[str, str] = models.CharField(max_length=255, null=False, blank=True) | ||
| textfield: models.TextField[str, str] = models.TextField(max_length=255, null=False, blank=True) | ||
| slugfield: models.SlugField[str, str] = models.SlugField(max_length=255, null=False, blank=True) | ||
| emailfield: models.EmailField[str, str] = models.EmailField(max_length=255, null=False, blank=True) | ||
| filepathfield: models.FilePathField[str, str] = models.FilePathField(max_length=255, null=False, blank=True) | ||
| urlfield: models.URLField[str, str] = models.URLField(max_length=255, null=False, blank=True) | ||
|
|
||
| charfieldu: models.CharField[str, str] = models.CharField(max_length=255, null=True, blank=True, unique=True) | ||
| textfieldu: models.TextField[str, str] = models.TextField(max_length=255, null=True, blank=True, unique=True) | ||
| slugfieldu: models.SlugField[str, str] = models.SlugField(max_length=255, null=True, blank=True, unique=True) | ||
| emailfieldu: models.EmailField[str, str] = models.EmailField(max_length=255, null=True, blank=True, unique=True) | ||
| filepathfieldu: models.FilePathField[str, str] = models.FilePathField( | ||
| max_length=255, null=True, blank=True, unique=True | ||
| ) | ||
| urlfieldu: models.URLField[str, str] = models.URLField(max_length=255, null=True, blank=True, unique=True) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need all those tests? They seem redundant to me, considering that we ignore the annotation.
flake8-django] Fix DJ001 not detecting null=True violations in annotated assignments (DJ001)flake8-django] Apply DJ001 to annotated fields
ntBre
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me, if Micha's happy with it!
* origin/main: Respect `--output-format` with `--watch` (#21097) [`pyflakes`] Revert to stable behavior if imports for module lie in alternate branches for `F401` (#20878) Fix finding keyword range for clause header after statement ending with semicolon (#21067) [ty] Fix bug where ty would think all types had an `__mro__` attribute (#20995) Restore `indent.py` (#21094) [`flake8-django`] Apply `DJ001` to annotated fields (#20907) Clearer error message when `line-length` goes beyond threshold (#21072) Update upload and download artifacts github actions (#21083) Update dependency mdformat-mkdocs to v4.4.2 (#21088) Update cargo-bins/cargo-binstall action to v1.15.9 (#21086) Update Rust crate clap to v4.5.50 (#21090) Update Rust crate get-size2 to v0.7.1 (#21091) Update Rust crate bstr to v1.12.1 (#21089) Add missing docstring sections to the numpy list (#20931) [`pydoclint`] Fix false positive on explicit exception re-raising (`DOC501`, `DOC502`) (#21011) [ty] Use constructor parameter types as type context (#21054)
Summary
Fixes #20906