Opened 2 years ago
Closed 2 years ago
#34023 closed Cleanup/optimization (fixed)
inlines variable not bound in loop in ModelAdmin._create_formsets
Reported by: | Michał Pasternak | Owned by: | Michał Pasternak |
---|---|---|---|
Component: | contrib.admin | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Ready for checkin | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
Hello,
if you analyze this function with flake8 with bugbear extension:
https://github.com/django/django/blob/cd11664066087051ef11311708ebc55def48d1da/django/contrib/admin/options.py#L2254-L2278
you will get an information about unbound variable inlines
, on this line:
https://github.com/django/django/blob/cd11664066087051ef11311708ebc55def48d1da/django/contrib/admin/options.py#L2265
Not sure how big is the thread, but as inlines is likely a class and creating code to actually trigger this bug would be a bit complicated, perhaps it would be useful to just bind the variable in the function parameters and don't worry about eventual problems in the future?
... like:
def user_deleted_form(request, obj, formset, index, inline=inline):
Change History (8)
comment:1 by , 2 years ago
Description: | modified (diff) |
---|
comment:2 by , 2 years ago
Component: | Uncategorized → contrib.admin |
---|---|
Summary: | ``inlines`` variable not bound in loop in ModelAdmin._create_formsets → inlines variable not bound in loop in ModelAdmin._create_formsets |
Triage Stage: | Unreviewed → Accepted |
Type: | Uncategorized → Cleanup/optimization |
comment:4 by , 2 years ago
TBH, since creating and calling internal functions is expensive, maybe it's better to immediately check these conditions instead of using user_deleted_form()
🤔
comment:5 by , 2 years ago
Maybe it's more readable with user_deleted_form()
, let's leave it for now.
comment:7 by , 2 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
Triage Stage: | Accepted → Ready for checkin |
Thanks for the report. As far as I'm aware it's not a bug in this particular case as
user_deleted_form
is created and used immediately inside a loop. However I agree that we can passinline
touser_deleted_form()
to avoid a bug in the future. Would you like to prepare a patch?Ticket are not required for small cleanups, by the way.