Skip to content

Commit

Permalink
Avoid underflow in get_model matching (#8965)
Browse files Browse the repository at this point in the history
Closes #8962.
  • Loading branch information
charliermarsh authored Dec 2, 2023
1 parent 35082b2 commit 22d8a98
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,6 @@ def model_assign() -> None:

Bad = import_string("django.core.exceptions.ValidationError") # N806
ValidationError = import_string("django.core.exceptions.ValidationError") # OK

Bad = apps.get_model() # N806
Bad = apps.get_model(model_name="Stream") # N806
6 changes: 5 additions & 1 deletion crates/ruff_linter/src/rules/pep8_naming/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,15 @@ pub(super) fn is_django_model_import(name: &str, stmt: &Stmt, semantic: &Semanti
return false;
};

if arguments.is_empty() {
return false;
}

// Match against, e.g., `apps.get_model("zerver", "Attachment")`.
if let Some(call_path) = collect_call_path(func.as_ref()) {
if matches!(call_path.as_slice(), [.., "get_model"]) {
if let Some(argument) =
arguments.find_argument("model_name", arguments.args.len() - 1)
arguments.find_argument("model_name", arguments.args.len().saturating_sub(1))
{
if let Some(string_literal) = argument.as_string_literal_expr() {
return string_literal.value.to_str() == name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,20 @@ N806.py:53:5: N806 Variable `Bad` in function should be lowercase
54 | ValidationError = import_string("django.core.exceptions.ValidationError") # OK
|

N806.py:56:5: N806 Variable `Bad` in function should be lowercase
|
54 | ValidationError = import_string("django.core.exceptions.ValidationError") # OK
55 |
56 | Bad = apps.get_model() # N806
| ^^^ N806
57 | Bad = apps.get_model(model_name="Stream") # N806
|

N806.py:57:5: N806 Variable `Bad` in function should be lowercase
|
56 | Bad = apps.get_model() # N806
57 | Bad = apps.get_model(model_name="Stream") # N806
| ^^^ N806
|


0 comments on commit 22d8a98

Please sign in to comment.