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

4 views
Skip to first unread message

Django

unread,
Jan 30, 2025, 12:49:12 PM1/30/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: dev
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Salvo Polizzi):

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


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:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
Reply all
Reply to author
Forward
0 new messages