Allow class names when `apps.get_model` is a non-string (#9065)

See:
https://github.com/astral-sh/ruff/issues/7675#issuecomment-1848206022
This commit is contained in:
Charlie Marsh 2023-12-08 22:59:05 -05:00 committed by GitHub
parent b7dd2b5941
commit 20e33bf514
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 2 deletions

View File

@ -55,3 +55,6 @@ def model_assign() -> None:
Bad = apps.get_model() # N806
Bad = apps.get_model(model_name="Stream") # N806
Address: Type = apps.get_model("zerver", variable) # OK
ValidationError = import_string(variable) # N806

View File

@ -112,7 +112,11 @@ pub(super) fn is_django_model_import(name: &str, stmt: &Stmt, semantic: &Semanti
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;
if string_literal.value.to_str() == name {
return true;
}
} else {
return true;
}
}
}
@ -127,7 +131,9 @@ pub(super) fn is_django_model_import(name: &str, stmt: &Stmt, semantic: &Semanti
if let Some(argument) = arguments.find_argument("dotted_path", 0) {
if let Some(string_literal) = argument.as_string_literal_expr() {
if let Some((.., model)) = string_literal.value.to_str().rsplit_once('.') {
return model == name;
if model == name {
return true;
}
}
}
}

View File

@ -52,6 +52,15 @@ 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
58 |
59 | Address: Type = apps.get_model("zerver", variable) # OK
|
N806.py:60:5: N806 Variable `ValidationError` in function should be lowercase
|
59 | Address: Type = apps.get_model("zerver", variable) # OK
60 | ValidationError = import_string(variable) # N806
| ^^^^^^^^^^^^^^^ N806
|