You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
deduperreload gives invalid tracebacks in reloaded functions #15359
When a function is reloaded with deduperreload, the line number information from the source file is lost. This breaks tracebacks, causing them to point to the start of the file instead of the correct source line. The reported line number matches the offset from the start of the function (plus one if the function is inside a class), but any comments and blank lines are ignored.
Tested with Python 3.13.14, IPython 9.15.0.
Python3.13.14|packagedbyconda-forge| (main, Jun122026, 09:50:25) [GCC14.3.0]
Type'copyright', 'credits'or'license'formoreinformationIPython9.15.0--AnenhancedInteractivePython. Type'?'forhelp.
In [1]: %load_extautoreloadIn [2]: %autoreload-p1In [3]: %%writefilemodule.py
...: """ ...: The traceback incorrectly points to this line (line 2 of the file) ...: """
...: deffunc():
...: assertFalse, "before reload"
...:
...:
In [4]: %aimportmoduleIn [5]: module.func()
---------------------------------------------------------------------------AssertionErrorTraceback (mostrecentcalllast)
CellIn[5], line1---->1module.func()
File/tmp/module.py:5, infunc()
4deffunc():
---->5assertFalse, "before reload"AssertionError: beforereloadIn [6]: %%writefilemodule.py
...: """ ...: The traceback incorrectly points to this line (line 2 of the file) ...: """
...: deffunc():
...: assertFalse, "after reload"
...:
...:
Overwritingmodule.pyIn [7]: module.func()
Reloading'module'.
---------------------------------------------------------------------------AssertionErrorTraceback (mostrecentcalllast)
CellIn[7], line1---->1module.func()
File/tmp/module.py:2, infunc()
1"""----> 2 The traceback incorrectly points to this line (line 2 of the file) 3 """4deffunc():
5assertFalse, "after reload"AssertionError: afterreload
I've been testing out #15369 locally, and found a deeper issue: the traceback of a non-reloaded function can point to the wrong line if the function definition was moved around in the file due to added or removed lines before it.
It looks like it might be possible to update the function line numbers in-place with func.__code__ = func.__code__.replace(co_firstlineno=<new line number>).
Activity
yut23 commentedon Aug 7, 2026
I'm not sure why
DeduperReloader._patch_namespace_inner()unparses the AST node to text here instead of directly wrapping it with anast.ClassDef:ipython/IPython/extensions/deduperreload/deduperreload.py
Lines 448 to 453 in dd41c52
Something like this should preserve all the line number information from the source file:
fix: preserve source line numbers in deduperreload reloaded functions
fix: preserve source line numbers in deduperreload reloaded functions
fix: preserve source line numbers in deduperreload reloaded functions
yut23 commentedon Aug 16, 2026
I've been testing out #15369 locally, and found a deeper issue: the traceback of a non-reloaded function can point to the wrong line if the function definition was moved around in the file due to added or removed lines before it.
It looks like it might be possible to update the function line numbers in-place with
func.__code__ = func.__code__.replace(co_firstlineno=<new line number>).