Skip to content
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

Avoid underflow in get_model matching #8965

Merged
merged 1 commit into from
Dec 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
|


Loading