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 Michał Pasternak)

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 Michał Pasternak, 2 years ago

Description: modified (diff)

comment:2 by Mariusz Felisiak, 2 years ago

Component: Uncategorizedcontrib.admin
Summary: ``inlines`` variable not bound in loop in ModelAdmin._create_formsetsinlines variable not bound in loop in ModelAdmin._create_formsets
Triage Stage: UnreviewedAccepted
Type: UncategorizedCleanup/optimization

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 pass inline to user_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.

comment:3 by Michał Pasternak, 2 years ago

OK, I will.

comment:4 by Mariusz Felisiak, 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 Mariusz Felisiak, 2 years ago

Maybe it's more readable with user_deleted_form(), let's leave it for now.

Version 0, edited 2 years ago by Mariusz Felisiak (next)

comment:6 by Michał Pasternak, 2 years ago

Has patch: set

comment:7 by Mariusz Felisiak, 2 years ago

Owner: changed from nobody to Michał Pasternak
Status: newassigned
Triage Stage: AcceptedReady for checkin

comment:8 by GitHub <noreply@…>, 2 years ago

Resolution: fixed
Status: assignedclosed

In 0f31d10:

Fixed #34023 -- Added inline argument to user_deleted_form().

This binds the loop variable.

Note: See TracTickets for help on using tickets.
Back to Top