Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GSOC] feat: auto-importing django shell #18158

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

salvo-polizzi
Copy link
Contributor

@salvo-polizzi salvo-polizzi commented May 11, 2024

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

  • Auto-import all models from the current project/app.
  • Test subclassing the shell command and override a method.
  • Test importing extra-things (modules, classes, etc.).
  • Write a tutorial and document the new features.

Checklist

  • This PR targets the main branch.
  • The commit message is written in past tense, mentions the ticket number, and ends with a period.
  • I have checked the "Has patch" ticket flag in the Trac system.
  • I have added or updated relevant tests.
  • I have added or updated relevant docs, including release notes if applicable.
  • I have attached screenshots in both light and dark modes for any UI changes.

Copy link
Sponsor Member

@adamchainz adamchainz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Following up from my post on the forum :) You got this !

django/core/management/commands/shell.py Outdated Show resolved Hide resolved
django/core/management/commands/shell.py Outdated Show resolved Hide resolved
django/core/management/commands/shell.py Outdated Show resolved Hide resolved
django/core/management/commands/shell.py Outdated Show resolved Hide resolved
@salvo-polizzi
Copy link
Contributor Author

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.

@adamchainz
Copy link
Sponsor Member

Yes, the tests should be within ShellCommandTestCase.

You can unit-test get_namespace() by calling it (imports = Command().get_namespace()) and making assertions on its contents. Then you can write integration tests similar to the existing ones that use command and captured_stdin to test those pathways.

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 python shell, though there’s a risk that calling the startup file, interactive hook, or readline are not a good idea during a test run.

* Deleted useless import_string function.

* Gave precedence to earlier apps.

* Loaded default imports with command option.
Copy link
Sponsor Member

@adamchainz adamchainz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work getting some tests written! 💪

django/core/management/commands/shell.py Outdated Show resolved Hide resolved
tests/shell/models.py Outdated Show resolved Hide resolved
django/core/management/commands/shell.py Outdated Show resolved Hide resolved
django/core/management/commands/shell.py Outdated Show resolved Hide resolved
django/core/management/commands/shell.py Outdated Show resolved Hide resolved
Fixed CI.

Ensured that command and stdin option had default imports.

Removed useless get_apps_and_models method.
Copy link
Sponsor Member

@adamchainz adamchainz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your continued iteration. I hope you learn some things from my comments here.

django/core/management/commands/shell.py Outdated Show resolved Hide resolved
django/core/management/commands/shell.py Outdated Show resolved Hide resolved
def get_namespace(self):
apps_models = apps.get_models()
imported_objects = {}
for model in reversed(apps_models):
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No reversed() so that earlier apps take precedence. Test precedence.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @adamchainz, I'm sorry to bother you with this question, but I have a doubt about testing the precedence for get_namespace. When you say earlier apps take precedence, do you mean that if we have a name collision, we have to import the models listed first in INSTALLED_APPS?

Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, in a collision the earlier model should be the one in the namespace. This follows the precedence order of other name resolutions in Django, such as for management commands.

As for second, replaced models, it would be nice to provide some way of still providing access, such as by having each app’s models module imported as <app_label>_models, so one could do e.g. auth_models.User. But let’s have a forum discussion about that first, since there are many different strategies we could copy from django-extensions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No reversed() so that earlier apps take precedence. Test precedence.

Hi @adamchainz,

Thank you for your advice. I'll be raising a discussion in the forum the next few days on what strategy to adopt for name collisions.

Apart from that, I've tested precedence with specific tests locally, and I've observed that if we loop over apps_models, we aren't actually giving precedence to the earlier apps because the latest would override the earlier ones. Instead, testing the precedence using reversed() seems to respect the precedence of the earlier apps, which would override the latest listed in the list.

Please let me know if I am saying something incorrect.

django/core/management/commands/shell.py Outdated Show resolved Hide resolved
tests/shell/tests.py Show resolved Hide resolved
tests/shell/tests.py Outdated Show resolved Hide resolved
tests/shell/tests.py Outdated Show resolved Hide resolved
tests/shell/tests.py Outdated Show resolved Hide resolved
@salvo-polizzi
Copy link
Contributor Author

Thanks @adamchainz again for your reviews. I left some questions in the comments

Deleted useless update_globals method.

Deleted useless test.

Minor improvements.

Setted maxDiff to None.

Updated installed apps for tests.
tests/shell/tests.py Outdated Show resolved Hide resolved
Copy link
Contributor

@DevilsAutumn DevilsAutumn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@salvo-polizzi salvo-polizzi force-pushed the auto-importing-shell branch 2 times, most recently from 30f5b5d to 07c514f Compare May 23, 2024 08:50
Fixed CI.

Fixed CI (II).

Fixed CI (III).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants