Re: [Django] #36158: get_and_report_namespace() incorrectly reports the location where an object was created.

12 views
Skip to first unread message

Django

unread,
Jan 31, 2025, 12:36:14 PM1/31/25
to django-...@googlegroups.com
#36158: get_and_report_namespace() incorrectly reports the location where an object
was created.
-------------------------------------+-------------------------------------
Reporter: Salvo Polizzi | Owner: Salvo
| Polizzi
Type: Bug | Status: assigned
Component: Core (Management | Version: 5.2
commands) |
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* severity: Normal => Release blocker
* stage: Unreviewed => Accepted
* version: dev => 5.2

Comment:

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
{{{#!diff
--- a/tests/shell/tests.py
+++ b/tests/shell/tests.py
@@ -271,6 +271,22 @@ class
ShellCommandAutoImportsTestCase(SimpleTestCase):
" from shell.models import Phone, Marker",
)

+ def test_message_with_stdout_listing_objects_location(self):
+ class TestCommand(shell.Command):
+ def get_namespace(self):
+ from django.db import connection
+
+ return {"connection": connection}
+
+ with captured_stdout() as stdout:
+ TestCommand().get_and_report_namespace(verbosity=2)
+
+ self.assertEqual(
+ stdout.getvalue().strip(),
+ "1 object imported automatically, including:\n\n"
+ " from django.db import connection"
+ )
+
@override_settings(INSTALLED_APPS=["shell",
"django.contrib.contenttypes"])
def test_message_with_stdout_listing_objects_with_isort(self):
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36158#comment:2>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Feb 3, 2025, 2:22:02 AM2/3/25
to django-...@googlegroups.com
#36158: get_and_report_namespace() incorrectly reports the location where an object
was created.
-------------------------------------+-------------------------------------
Reporter: Salvo Polizzi | Owner: Salvo
| Polizzi
Type: Bug | Status: assigned
Component: Core (Management | Version: 5.2
commands) |
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Salvo Polizzi):

* has_patch: 0 => 1


Old description:

> 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
> [https://docs.djangoproject.com/en/dev/howto/custom-shell/#customizing-
> shell-auto-imports 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,
> }
> }}}
>
> 2. Start the Django shell with:
> {{{
> python manage.py shell -v 2
> }}}
>
> 3. 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.

New description:

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
[https://docs.djangoproject.com/en/dev/howto/custom-shell/#customizing-
shell-auto-imports 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,
}
}}}

2. Start the Django shell with:
{{{
python manage.py shell -v 2
}}}

3. 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.

--
--
Ticket URL: <https://code.djangoproject.com/ticket/36158#comment:3>

Django

unread,
Feb 7, 2025, 2:27:34 PM2/7/25
to django-...@googlegroups.com
#36158: get_and_report_namespace() incorrectly reports the location where an object
was created.
-------------------------------------+-------------------------------------
Reporter: Salvo Polizzi | Owner: Natalia
| Bidart
Type: Bug | Status: assigned
Component: Core (Management | Version: 5.2
commands) |
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Natalia Bidart):

* owner: Salvo Polizzi => Natalia Bidart

--
Ticket URL: <https://code.djangoproject.com/ticket/36158#comment:4>

Django

unread,
Feb 13, 2025, 8:27:17 AM2/13/25
to django-...@googlegroups.com
#36158: get_and_report_namespace() incorrectly reports the location where an object
was created.
-------------------------------------+-------------------------------------
Reporter: Salvo Polizzi | Owner: Natalia
| Bidart
Type: Bug | Status: assigned
Component: Core (Management | Version: 5.2
commands) |
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* stage: Accepted => Ready for checkin

--
Ticket URL: <https://code.djangoproject.com/ticket/36158#comment:5>

Django

unread,
Feb 13, 2025, 10:01:25 AM2/13/25
to django-...@googlegroups.com
#36158: get_and_report_namespace() incorrectly reports the location where an object
was created.
-------------------------------------+-------------------------------------
Reporter: Salvo Polizzi | Owner: Natalia
| Bidart
Type: Bug | Status: closed
Component: Core (Management | Version: 5.2
commands) |
Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce <42296566+sarahboyce@…>):

* resolution: => fixed
* status: assigned => closed

Comment:

In [changeset:"56e23b2319cc29e6f8518f8f21f95a530dddb930" 56e23b23]:
{{{#!CommitTicketReference repository=""
revision="56e23b2319cc29e6f8518f8f21f95a530dddb930"
Fixed #36158 -- Refactored shell command to improve auto-imported objects
reporting.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36158#comment:6>

Django

unread,
Feb 13, 2025, 10:03:45 AM2/13/25
to django-...@googlegroups.com
#36158: get_and_report_namespace() incorrectly reports the location where an object
was created.
-------------------------------------+-------------------------------------
Reporter: Salvo Polizzi | Owner: Natalia
| Bidart
Type: Bug | Status: closed
Component: Core (Management | Version: 5.2
commands) |
Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Sarah Boyce <42296566+sarahboyce@…>):

In [changeset:"6f934989df00be4c7213c6d9efec374da1e300da" 6f93498]:
{{{#!CommitTicketReference repository=""
revision="6f934989df00be4c7213c6d9efec374da1e300da"
[5.2.x] Fixed #36158 -- Refactored shell command to improve auto-imported
objects reporting.

Backport of 56e23b2319cc29e6f8518f8f21f95a530dddb930 from main.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36158#comment:7>
Reply all
Reply to author
Forward
0 new messages