Fixed #35515 -- Added auto-importing to shell command.#18158
Fixed #35515 -- Added auto-importing to shell command.#18158nessita merged 1 commit intodjango:mainfrom
Conversation
4f90a52 to
4d9eb51
Compare
adamchainz
left a comment
There was a problem hiding this comment.
Following up from my post on the forum :) You got this !
4d9eb51 to
abc0225
Compare
|
Thank you so much, @adamchainz, for the feedback. Currently, I'm concentrating on writing tests for these features. Do you have any references or suggestions on where to start? I'm considering creating new functions within the shellCommandTestCase to test the auto-imports. |
|
Yes, the tests should be within You can unit-test It seems the existing tests don't actually cover launching the various shells. I think it’s worth trying adding coverage, at least for the default |
b0a2562 to
aa2080a
Compare
adamchainz
left a comment
There was a problem hiding this comment.
Great work getting some tests written! 💪
ca191f6 to
c2a6351
Compare
adamchainz
left a comment
There was a problem hiding this comment.
Thanks for your continued iteration. I hope you learn some things from my comments here.
|
Thanks @adamchainz again for your reviews. I left some questions in the comments |
df3323c to
a1582f0
Compare
DevilsAutumn
left a comment
There was a problem hiding this comment.
Hi @salvo-polizzi , Thankyou for your constants efforts on this! And special thanks to Adam for reviewing. This PR looks in a great shape already. 🥇
I finally have some time to push this further.
07c514f to
10a17aa
Compare
bcf6559 to
4de4e1e
Compare
DevilsAutumn
left a comment
There was a problem hiding this comment.
Thanks @salvo-polizzi , I left some comments on the docs. Also, it will be better to add one more docs section under how to override the shell to show how to override the shell to use it for a custom python shell runner.
|
Hi @DevilsAutumn, thanks for your review. I've started writing the new section on how to override the shell for a customized shell runner. I'll push it in the next few days. |
adamchainz
left a comment
There was a problem hiding this comment.
Hello! I just left DjangoCon Europe this morning. I have been discussing this project with other attendees and I only heard positive reactions.
It’s great to see further progress on this PR.
I’ve added some specific comments, but I think now is a good time to also address a couple of pieces of groundwork.
First, we don’t have a Trac ticket, and we should do for any notable change. Salvo, can you create one on Trac, mention it in the PR description (update to match the PR template), and retitle the PR/commit something like “Fixed # -- Added auto-importing to shell command.”.
The ticket description doesn’t need to be long and can link to the wiki and your proposal.
Second, let’s add some missing test coverage in a separate PR. Specifically, I noted that we don’t have any test coverage of the methods that run shells: ipython(), bpython(), and python().
Let’s add those tests in a separate PR with a title like “Refs # -- Improved test coverage of shell command.”. The tests can use this technique to mock the imports of the IPython and bpython, so they don’t actually launch the shell, and don’t need the corresponding packages installed. Similar mocking can be done for the code module’s code.interact() in python().
The tests in this PR can then build on that, checking that the right arguments are passed. They won’t be perfect, since we won’t actually be testing the integration, but they’ll be better than nothing.
|
Hi @adamchainz, I've just changed the PR title and created a Trac ticket for this new feature. It's great to hear that this functionality is very well appreciated by other Django users and contributors, and I'm glad to contribute to this project. In the next few days, I'll work on writing tests to improve test coverage for methods that run the shell. I'm looking forward to seeing how this will turn out. |
4fa9414 to
31d51df
Compare
d1afe8b to
77727da
Compare
77727da to
b1d62f7
Compare
|
Thank you @salvo-polizzi for this work, I'm performing an in-depth review with the goal to land in the next 1-2 weeks. Sarah and I would love to have this feature merged before the 5.2 feature freeze. I'm also doing some trivial fixes and pushes, such as resolving conflicts. There is one issue from the printed models (with (I'm adding this comment in isolation to start this conversation, other comments may come later as comments or new revnos pushed.) Specifically, for a simple project with a single custom Django app ( (MY EYES MY EYES) I would rather we indent one level in for the list, and we sort with isort if possible? Also, I think I have all the comments in the PR history and I did see the mention of including extra imports, but I think I don't see those in this PR? Are the ones that we agreed on https://forum.djangoproject.com/t/default-automatic-imports-in-the-shell/33708 being added in a follow up PR? Thanks everyone!!! |
b1d62f7 to
af76daa
Compare
|
Hi @nessita, Regarding the sorting of imports, I’d like to hear Adam’s thoughts, but personally, I think using isort could be a good solution. The PR related to default imports is #18560. I haven’t worked on it yet because I was waiting for this PR to be merged first, so we can build the new changes upon it. |
No worries!
So I've been thinking about this, and I think that we should only sort if the env already has I also think we should indent the listing, I can push a proposal soon if you don't object.
Thank you! Makes sense to not update that until this one is completed and merged. |
|
Note that the current order printed is not the same as the order imported and we should probably update this (as duplicate model names overwrite eachother) |
I was thinking whether we could only show the final list of "really" imported paths, that would mean removing from the list the shadowed imports. @salvo-polizzi Before changing anything, do you think that is doable and reasonably simple? Or does it add too much complexity? |
Do you mean to only show the models that aren't overwritten?
I'm sorry if this is straightforward but how can we check if the env has |
You could probably take some inspiration from |
Thank you for this valuable advice! I do have some concerns about how we should run isort in the shell prompt. Am I correct in understanding that isort can only be run on Python files? One potential solution could be to import isort and use the |
|
Oh, good point that we are unlikely to want to launch an executable like we're doing with Could I ask you to flesh out a bit what you mean when you say the difference between django & user environments? Are you thinking about having to adjust the development dependencies for django itself to run the tests for this feature? |
Yes, that was what i meant - I didn't expressed myself clearly. |
|
@nessita should I proceed with sorting the imports, or should we wait for more feedback? |
I'm very much in favor of this, could you please confirm if this is doable in a reasonable timeframe? Also, do you have the availability? And lastly, do you think this would be a desirable improvement? Thanks! |
Yes, I think it is a good improvement showing the imports in the right order and I think it is a pretty straightforward change to implement (i'm just about to complete the implementation 😊) . Let me know if you have any additional thoughts or requirements! |
Thank you @salvo-polizzi! I ran out of time this week but this PR will be my priority next week. I may push some fixes and let you know. Until then, would you please consider fixing the conflict in the release notes? |
|
@nessita Following our discussion on the forum, I plan to complete all the requested changes by the end of this week. |
Thank you so much, I really appreciate this! |
nessita
left a comment
There was a problem hiding this comment.
Thank you @salvo-polizzi for your latest changes. I have pushed a set of tweaks, including a minor refactor of the tests. I think this is ready for merge! 🌟
Independently of that, I have one question that arises from the tests. Specifically, for test_message_with_stdout_listing_objects_with_isort_not_installed, the info message says ... 5 objects imported... but the rest, for -v 2, shows 3 objects:
from django.contrib.contenttypes.models import ContentType
from shell.models import Phone, Marker
I understand that the other two are the customized MyClass and constant, but somehow these are not showing in the "full list" and it feels unexpected. Have you given this any consideration?
|
Thank you, @nessita, for your work on this PR—I really appreciate it.
Regarding |
@salvo-polizzi Thank you, this answers my question. I guess then that my proposal of appending "Full list" should be revisited since the list that we show is not a complete list. Perhaps we should say "Including" or similar, like Sarah suggested? Do you have a preference/recommendation? |
I think that something like "including" should be good. |
…mand. Thanks to Bhuvnesh Sharma and Adam Johnson for mentoring this Google Summer of Code 2024 project. Thanks to Sarah Boyce, David Smith, Jacob Walls and Natalia Bidart for reviews.
pauloxnet
left a comment
There was a problem hiding this comment.
Great job. 👏
Sorry I was late with the review, I had added a couple of comments that were absolutely not blocking for the PR. 😅
|
Thanks @pauloxnet for your valuable feedback 👍 |
Branch description
This would be an update of the existing Django shell that auto-imports models for you from your app/project. Also, the goal would be to allow the user to subclass this shell to customize its behavior and import extra things.
TODO
Checklist
mainbranch.