Opened 11 hours ago

Last modified 39 minutes ago

#36158 assigned Bug

get_and_report_namespace() incorrectly reports the location where an object was created.

Reported by: Salvo Polizzi Owned by: Salvo Polizzi
Component: Core (Management commands) Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Salvo Polizzi)

When get_and_report_namespace is called with verbosity=2, it misreports the actual location where an object was created. This seems to happen because the function only considers objects with a __module__ attribute. While this works correctly for functions and classes (which are defined in a module), it fails for instances. Since instances can be created elsewhere, the function incorrectly reports the module where the class was defined rather than where the instance was created.

Steps to reproduce

  1. Override get_namespace() using this tutorial as follows:
    from django.core.management.commands import shell
    
    
    class Command(shell.Command):
        def get_namespace(self):
            from django.db import connection
            return {
                "connection": connection,
            }
    
  1. Start the Django shell with:
    python manage.py shell -v 2
    
  1. The output should report:
    1 objects imported automatically, including:
    
      from django.utils.connection import connection
    

However, this is incorrect because connection is an instance, and it should report where it was actually created, not just the module where its class was defined.

Change History (1)

comment:1 by Salvo Polizzi, 39 minutes ago

Description: modified (diff)
Summary: `get_and_report_namespace` incorrectly reports the location where an object was created.get_and_report_namespace() incorrectly reports the location where an object was created.
Note: See TracTickets for help on using tickets.
Back to Top