8000 Do not always import * with autoreload3 by jaysarva · Pull Request #14872 · ipython/ipython · GitHub
[go: up one dir, main page]

Skip to content

Do not always import * with autoreload3 #14872

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

jaysarva
Copy link
Contributor
@jaysarva jaysarva commented Apr 13, 2025

Fixes #14839

Previously, %autoreload 3 always had the effect of from module import * on reloads. This PR fixes this functionality to the expected utility: if users specifically import some symbols from a module (for example from X import Y or from X import Y as Z), only those symbols are reloaded.

Unit tests are added to demonstrate the new behavior as well.

@jaysarva jaysarva force-pushed the autoreload3-star-import branch from 09e8eb3 to 918f1b0 Compare April 13, 2025 20:59
@jaysarva jaysarva force-pushed the autoreload3-star-import branch from 918f1b0 to df9cb12 Compare April 16, 2025 02:02
@krassowski krassowski added this to the 9.2 milestone Apr 25, 2025
krassowski
krassowski previously approved these changes Apr 25, 2025
Copy link
Member
@krassowski krassowski left a comment

Choose a reason for hiding this comment

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

Thank you @jaysarva, this looks good to me (unit tests included make it much easier to review!).

I will run copilot on this PR to test it out, let's see if this is useful.

@krassowski krassowski requested a review from Copilot April 25, 2025 09:37
Copy link
@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes the behavior of %autoreload 3 so that only explicitly imported symbols are reloaded instead of always reloading all module contents. It also adds unit tests to verify the behavior and updates the autoreload machinery to track "from X import Y" imports.

  • Fix autoreload 3 to update only explicitly imported symbols.
  • Add comprehensive tests for various import scenarios.
  • Update the autoreload code to support import-from tracking.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
tests/test_zzz_autoreload.py Added tests covering different import variants and overloading scenarios for autoreload 3
IPython/extensions/autoreload.py Extended the autoreload mechanism to track imported symbols using AST parsing, and updated superreload to apply the new behavior
Comments suppressed due to low confidence (1)

tests/test_zzz_autoreload.py:54

  • [nitpick] Since FakeShell already initializes user_ns["In"], verify whether this duplicate initialization is needed for these tests or if it can be removed to avoid redundancy.
self.user_ns["In"] = []

Comment on lines 550 to 551
if name in symbol_map[module.__name__]:
name = symbol_map[module.__name__][name]
Copy link
Preview
Copilot AI Apr 25, 2025

Choose a reason for hiding this comment

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

To avoid potential KeyErrors if module.__name__ is not present in symbol_map, consider using symbol_map.get(module.__name__, {}) in the conditional check.

Suggested change
if name in symbol_map[module.__name__]:
name = symbol_map[module.__name__][name]
if name in symbol_map.get(module.__name__, {}):
name = symbol_map.get(module.__name__, {})[name]

Copilot uses AI. Check for mistakes.

Copy link
Member

Choose a reason for hiding this comment

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

This was a good catch, I was able to invoke an exception on runtime:

image

although this was not even a KeyError but the fact that symbol_map can be None. I guess mypy should have caught it? I guess the problem is that superreload is not typed so mypy did not run on it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for catching this! It seems I had some local changes that I didn't add to this pr so my testing was still passing :) I believe this should no longer be a problem, as we do a check on symbol_map to make sure it isn't null. @krassowski

@krassowski krassowski dismissed their stale review April 25, 2025 09:46

The PR introduces a critical bug as of now.

@krassowski krassowski modified the milestones: 9.2, 9.3 Apr 25, 2025
@jaysarva jaysarva force-pushed the autoreload3-star-import branch from 1d26aad to 6b98baf Compare April 26, 2025 20:38
@jaysarva jaysarva requested a review from krassowski April 26, 2025 20:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

autoreload 3 has the effect of import *
2 participants
34AB @jaysarva @krassowski
0