[Django] #36252: Duplicate Display of Imports in Django Shell

11 views
Skip to first unread message

Django

unread,
Mar 12, 2025, 6:06:14 PM3/12/25
to django-...@googlegroups.com
#36252: Duplicate Display of Imports in Django Shell
-------------------------------------+-------------------------------------
Reporter: Raffaella | Type: Bug
Status: new | Component: Core
| (Management commands)
Version: 5.2 | Severity: Normal
Keywords: shell | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
When customizing the automatic imports into the shell by adding the same
import twice:
{{{
from django.core.management.commands import shell


class Command(shell.Command):
def get_auto_imports(self):
return super().get_auto_imports() + [
"django.urls.reverse",
"django.urls.reverse",
]
}}}

After opening the shell, while the total number of imports is correct, the
`reverse` import is displayed twice in the output:


{{{
12 objects imported automatically:

from project.newsletter.models import SubscriptionNotification,
Subscription, Post, Category
from django.contrib.sessions.models import Session
from django.contrib.sites.models import Site
from django.contrib.contenttypes.models import ContentType
from django.contrib.auth.models import User, Group, Permission
from django.contrib.admin.models import LogEntry
from django.urls import reverse, reverse

}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36252>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Mar 12, 2025, 9:14:57 PM3/12/25
to django-...@googlegroups.com
#36252: Duplicate Display of Imports in Django Shell
-------------------------------------+-------------------------------------
Reporter: Raffaella | Owner: hesham
| hatem
Type: Bug | Status: assigned
Component: Core (Management | Version: 5.2
commands) |
Severity: Normal | Resolution:
Keywords: shell | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by hesham hatem):

* owner: (none) => hesham hatem
* status: new => assigned

--
Ticket URL: <https://code.djangoproject.com/ticket/36252#comment:1>

Django

unread,
Mar 12, 2025, 10:41:39 PM3/12/25
to django-...@googlegroups.com
#36252: Duplicate Display of Imports in Django Shell
-------------------------------------+-------------------------------------
Reporter: Raffaella | Owner: hesham
| hatem
Type: Bug | Status: assigned
Component: Core (Management | Version: 5.2
commands) |
Severity: Normal | Resolution:
Keywords: shell | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by hesham hatem):

* has_patch: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/36252#comment:2>

Django

unread,
Mar 13, 2025, 2:59:18 AM3/13/25
to django-...@googlegroups.com
#36252: Duplicate Display of Imports in Django Shell
-------------------------------------+-------------------------------------
Reporter: Raffaella | Owner: hesham
| hatem
Type: Bug | Status: closed
Component: Core (Management | Version: 5.2
commands) | Resolution:
Severity: Normal | worksforme
Keywords: shell | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Clifford Gama):

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

Comment:

Thanks Raffaella for the report and for testing. However, I can't
replicate this with the provided information. For me, imports repeated in
`get_auto_imports` are displayed only once, no matter how many times they
are repeated. I tried this with verbosity of 2 and 3 and on ipython and
regular python shell.
--
Ticket URL: <https://code.djangoproject.com/ticket/36252#comment:3>

Django

unread,
Mar 13, 2025, 5:51:46 AM3/13/25
to django-...@googlegroups.com
#36252: Duplicate Display of Imports in Django Shell
-------------------------------------+-------------------------------------
Reporter: Raffaella | Owner: hesham
| hatem
Type: Bug | Status: closed
Component: Core (Management | Version: 5.2
commands) | Resolution:
Severity: Normal | worksforme
Keywords: shell | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Raffaella):

Replying to [comment:3 Clifford Gama]:
> Thanks Raffaella for the report and for testing. However, I can't
replicate this with the provided information. For me, imports repeated in
`get_auto_imports` are displayed only once, no matter how many times they
are repeated. I tried this with verbosity of 2 and 3 and on ipython and
regular python shell.

Thanks Cliffort for your reply.
I tried it on a different project, and I do have multiple imports of the
same `reverse` when running `python manage.py shell -v=2` with this code
{{{
from django.core.management.commands import shell


class Command(shell.Command):
def get_auto_imports(self):
return super().get_auto_imports() + [
"django.urls.reverse",
"django.urls.reverse",
"django.urls.reverse",
"django.urls.reverse",
"django.urls.reverse",
"django.urls.reverse",
]
}}}

When I open the shell:


{{{
9 objects imported automatically:

from page.models import Tag, Blog
from django.contrib.sessions.models import Session
from django.contrib.contenttypes.models import ContentType
from django.contrib.auth.models import User, Group, Permission
from django.contrib.admin.models import LogEntry
from django.urls import reverse, reverse, reverse, reverse, reverse,
reverse

}}}

My current django version is `5.2b1`.
The number of the 9 imports is correct.
--
Ticket URL: <https://code.djangoproject.com/ticket/36252#comment:4>

Django

unread,
Mar 13, 2025, 8:43:58 AM3/13/25
to django-...@googlegroups.com
#36252: Duplicate Display of Imports in Django Shell
-------------------------------------+-------------------------------------
Reporter: Raffaella | Owner: hesham
| hatem
Type: Bug | Status: closed
Component: Core (Management | Version: 5.2
commands) | Resolution:
Severity: Normal | worksforme
Keywords: shell | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Natalia Bidart):

Thank you Raffaella and Clifford for the reporting and handling of this
ticket. I can't reproduce either, using Django `main`, with a custom shell
command like this:
{{{#!python
from django.core.management.commands import shell


class Command(shell.Command):
def get_auto_imports(self):
return super().get_auto_imports() + [
"django.urls.reverse",
"django.urls.reverse",
"django.urls.reverse",
"django.urls.reverse",
"django.urls.reverse",
"django.urls.reverse",
"django.urls.reverse",
"django.urls.reverse",
"django.urls.reverse",
]
}}}

I get (models are custom from my django test project where I do general
ticket triage):
{{{
$ python -Wall manage.py shell --verbosity=2
30 objects imported automatically:

from django.contrib.admin.models import LogEntry
from django.contrib.auth.models import Group, Permission, User
from django.contrib.contenttypes.models import ContentType
from django.contrib.sessions.models import Session
from django.urls import reverse

from abstractmodels.models import ArticlePage, Page
from complexapp.models.a import ModelA
from complexapp.models.b import ModelB
from complexapp.models.c import ModelC
from generatedapp.models import (Author, AuthorProfile, Book, BookGenre,
Genre, Review)
from testapp.models import (BasicModel, CollidingModelName, Course,
Student,
Ticket36031Model, UUIDModel,
VulnerableModel)
from ticket_35928.models import (BlogPost, CollidingModelName, Comment,
Image, Product, TaggedItem)

Python 3.13.2 (main, Feb 5 2025, 08:49:06) [GCC 13.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>>
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36252#comment:5>

Django

unread,
Mar 13, 2025, 8:58:46 AM3/13/25
to django-...@googlegroups.com
#36252: Duplicate Display of Imports in Django Shell
-------------------------------------+-------------------------------------
Reporter: Raffaella | Owner: hesham
| hatem
Type: Bug | Status: new
Component: Core (Management | Version: 5.2
commands) |
Severity: Release blocker | Resolution:
Keywords: shell | 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):

* resolution: worksforme =>
* severity: Normal => Release blocker
* stage: Unreviewed => Accepted
* status: closed => new

Comment:

On a second look, I'm able to reproduce. I think the key difference is
having `isort` installed or not. Details:
* When building the `auto_imports` dict we are accumulating duplicating
modules (output reduced for easier reading):
{{{
(Pdb) pprint(auto_imports)
defaultdict(<class 'list'>, {
'django.contrib.admin.models': [('LogEntry',
<class
'django.contrib.admin.models.LogEntry'>)],
'django.contrib.auth.models': [('User',
<class
'django.contrib.auth.models.User'>),
('Group',
<class
'django.contrib.auth.models.Group'>),
('Permission',
<class
'django.contrib.auth.models.Permission'>)],
'django.contrib.contenttypes.models': [('ContentType',
<class
'django.contrib.contenttypes.models.ContentType'>)],
'django.contrib.sessions.models': [('Session',
<class
'django.contrib.sessions.models.Session'>)],
'django.urls': [('reverse', <function reverse at
0x7d9628a5a700>),
('reverse', <function reverse at
0x7d9628a5a700>),
('reverse', <function reverse at
0x7d9628a5a700>),
('reverse', <function reverse at
0x7d9628a5a700>),
('reverse', <function reverse at
0x7d9628a5a700>),
('reverse', <function reverse at
0x7d9628a5a700>),
('reverse', <function reverse at
0x7d9628a5a700>),
('reverse', <function reverse at
0x7d9628a5a700>),
('reverse', <function reverse at
0x7d9628a5a700>)]})
}}}

* The above needs some cleanup, though the `namespace` dictionary items
are properly unique, see:
{{{
(Pdb) pprint(namespace)
{'ContentType': <class 'django.contrib.contenttypes.models.ContentType'>,
'Group': <class 'django.contrib.auth.models.Group'>,
'LogEntry': <class 'django.contrib.admin.models.LogEntry'>,
'Permission': <class 'django.contrib.auth.models.Permission'>,
'Session': <class 'django.contrib.sessions.models.Session'>,
'User': <class 'django.contrib.auth.models.User'>,
'reverse': <function reverse at 0x7d9628a5a700>}
}}}

* The resulting `import_string` has the duplicated entries, but later
`isort` would clean that up:
{{{
(Pdb) pprint(import_string)
(' from ticket_35928.models import TaggedItem, CollidingModelName, Image,
'
'Product, BlogPost, Comment\n'
' from testapp.models import UUIDModel, VulnerableModel, Course,
Student, '
'Ticket36031Model, CollidingModelName, BasicModel\n'
' from generatedapp.models import Review, AuthorProfile, BookGenre,
Genre, '
'Book, Author\n'
' from complexapp.models.c import ModelC\n'
' from complexapp.models.b import ModelB\n'
' from complexapp.models.a import ModelA\n'
' from abstractmodels.models import ArticlePage, Page\n'
' from django.contrib.sessions.models import Session\n'
' from django.contrib.contenttypes.models import ContentType\n'
' from django.contrib.auth.models import User, Group, Permission\n'
' from django.contrib.admin.models import LogEntry\n'
' from django.urls import reverse, reverse, reverse, reverse, reverse, '
'reverse, reverse, reverse, reverse')
}}}

Accepting since this should properly import and show unique entries, with
or without `isort`.
--
Ticket URL: <https://code.djangoproject.com/ticket/36252#comment:6>
Reply all
Reply to author
Forward
0 new messages