Avoid underflow in get_model matching (#8965)

Closes https://github.com/astral-sh/ruff/issues/8962.
This commit is contained in:
Charlie Marsh 2023-12-02 08:56:57 -05:00 committed by GitHub
parent 35082b28cd
commit 22d8a989d4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 1 deletions

View file

@ -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

View file

@ -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;

View file

@ -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
|