Opened 21 months ago

Closed 21 months ago

Last modified 21 months ago

#33917 closed Cleanup/optimization (wontfix)

Optimize ModelBase.__new__ app_label setup

Reported by: Ivan Dominic Baguio Owned by: Ivan Dominic Baguio
Component: Database layer (models, ORM) Version: 4.0
Severity: Normal Keywords: ModelBase
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

While browsing the ModelBase code, i noticed a possible improvement on the code logic.

under ModelBase.__new__, you will see:

        # Look for an application configuration to attach the model to.
        app_config = apps.get_containing_app_config(module)

        if getattr(meta, "app_label", None) is None:
            if app_config is None:
                # ...
            else:
                app_label = app_config.label

notice that app_config is defined above and outside the if getattr block.
the app_config variable is only used inside the if getattr block.

it would make sense to move the app_config initialization inside the if block, so it doesn't get unnecessarily called.

i've double checked the following:

  • get_containing_app_config doesn't have any side effects, and is only a getter method
  • app_config is only needed inside the if block

Attachments (1)

0001-Move-app_config-initialization-inside-if-block.patch (1010 bytes ) - added by Ivan Dominic Baguio 21 months ago.

Download all attachments as: .zip

Change History (4)

comment:1 by Mariusz Felisiak, 21 months ago

Has patch: set
Owner: changed from nobody to Ivan Dominic Baguio
Status: newassigned
Summary: Improve ModelBase.__new__ app_label setupOptimize ModelBase.__new__ app_label setup
Triage Stage: UnreviewedAccepted

Sounds reasonable. For the future, small cleanups don't need tickets.

PR

comment:2 by Mariusz Felisiak, 21 months ago

Resolution: wontfix
Status: assignedclosed
Triage Stage: AcceptedUnreviewed

apps.get_containing_app_config() has a side-effect, it calls check_apps_ready() that raises an exception if all apps haven't been imported yet. IMO we shouldn't change this.

comment:3 by Ivan Dominic Baguio, 21 months ago

I didn't notice that. I agree it shouldn't be modified.

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