Opened 5 months ago

Closed 5 months ago

Last modified 5 months ago

#36158 closed Bug (fixed)

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

Reported by: Salvo Polizzi Owned by: Natalia Bidart
Component: Core (Management commands) Version: 5.2
Severity: Release blocker 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 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 (7)

comment:1 by Salvo Polizzi, 5 months 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.

comment:2 by Sarah Boyce, 5 months ago

Severity: NormalRelease blocker
Triage Stage: UnreviewedAccepted
Version: dev5.2

I can replicate it. I also think it would be nice to say "1 object" rather than "1 objects".
Happy for you to take a look. Marking as a release blocker but I think this can be de-escalated if need be

  • tests/shell/tests.py

    a b class ShellCommandAutoImportsTestCase(SimpleTestCase):  
    271271            "  from shell.models import Phone, Marker",
    272272        )
    273273
     274    def test_message_with_stdout_listing_objects_location(self):
     275        class TestCommand(shell.Command):
     276            def get_namespace(self):
     277                from django.db import connection
     278
     279                return {"connection": connection}
     280
     281        with captured_stdout() as stdout:
     282            TestCommand().get_and_report_namespace(verbosity=2)
     283
     284        self.assertEqual(
     285            stdout.getvalue().strip(),
     286            "1 object imported automatically, including:\n\n"
     287            "  from django.db import connection"
     288        )
     289
    274290    @override_settings(INSTALLED_APPS=["shell", "django.contrib.contenttypes"])
    275291    def test_message_with_stdout_listing_objects_with_isort(self):

comment:3 by Salvo Polizzi, 5 months ago

Description: modified (diff)
Has patch: set

comment:4 by Natalia Bidart, 5 months ago

Owner: changed from Salvo Polizzi to Natalia Bidart

comment:5 by Sarah Boyce, 5 months ago

Triage Stage: AcceptedReady for checkin

comment:6 by Sarah Boyce <42296566+sarahboyce@…>, 5 months ago

Resolution: fixed
Status: assignedclosed

In 56e23b23:

Fixed #36158 -- Refactored shell command to improve auto-imported objects reporting.

comment:7 by Sarah Boyce <42296566+sarahboyce@…>, 5 months ago

In 6f93498:

[5.2.x] Fixed #36158 -- Refactored shell command to improve auto-imported objects reporting.

Backport of 56e23b2319cc29e6f8518f8f21f95a530dddb930 from main.

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