question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

autoreload and function decorator

See original GitHub issue

I posted this on SO here: https://stackoverflow.com/q/49825052/5545005

I am not sure where I should have posted. Thinking here would have been better. Is there any advice on this issue?

Thanks.

My System: ‘commit_hash’: ‘ca5443062’, ‘commit_source’: ‘installation’, ‘default_encoding’: ‘utf-8’, ‘ipython_path’: ‘C:\Anaconda3\lib\site-packages\IPython’, ‘ipython_version’: ‘6.2.1’, ‘os_name’: ‘nt’, ‘platform’: ‘Windows-10-10.0.16299-SP0’, ‘sys_executable’: ‘C:\Anaconda3\python.exe’, ‘sys_platform’: ‘win32’, ‘sys_version’: '3.6.3 |Anaconda custom (64-bit)| (default, Oct 15 2017, ’ ‘03:27:45) [MSC v.1900 64 bit (AMD64)]’

I am fairly new to decorators but am experiencing unexpected behavior revolving around autoreload in an interactive workflow with decorated functions. Its best explained by example (note these are all cells in a jupyter notebook):

The decorator:

%%file testdec.py
def decorated(func):
    print("decorating")
    def wrapped(*args, **kwargs):
        return func(*args, **kwargs)
    return wrapped

Where the decorator is used:

%%file testmod.py
from testdec import decorated
@decorated
def thisfunc():
    print("old output")
def _thisfunc1():
    print("old output 1")
thisfunc1 = decorated(_thisfunc1)

I would use the following to call the decorated functions:

from testmod import *
thisfunc()
thisfunc1()

outputs:

decorating
decorating
old output
old output 1

Now updating testmod.py with:

%%file testmod.py
from testdec import decorated
@decorated
def thisfunc():
    print("new output")
def _thisfunc1():
    print("new output 1")
thisfunc1 = decorated(_thisfunc1)

and calling the functions again:

thisfunc()
thisfunc1()

gives the following, note the old output from the first method:

decorating
decorating
old output
new output 1

However, explicitly reimporting from this module:

from testmod import *
thisfunc()
thisfunc1()

results in:

new output
new output 1

Ideally the @decorated function (e.g. with the @ and not the second method) would autoreload transparently as the second method does. Is there something I can do to achieve this? What am I missing for decorated functions. For now we’re manually disabling decorators when editing interactively in order to have the benefits of autoreload.

Thanks.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:1
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
WestXucommented, Apr 24, 2019

This bug is really annoying. None of the solutions mentioned so far in this post works, when it’s a decorated method rather than a decorated function. Perhaps I just rely too much on autoreload in my work flow. Hope this get’s fixed soon, or I am open to alternatives similar to this, by which I can use both jupyter and IDE at the same time.

1reaction
PietroPasotticommented, Jul 26, 2019

Just stumbled on this issue too, took me quite some time to figure out what was going wrong where. Hoping on a fix as well…

Read more comments on GitHub >

github_iconTop Results From Across the Web

autoreload and function decorator - ipython - Stack Overflow
I am fairly new to decorators but am experiencing unexpected behavior revolving around autoreload in an interactive workflow with decorated ...
Read more >
autoreload — IPython 8.7.0 documentation
%autoreload tries to work around common pitfalls by replacing function code objects and parts of classes previously in the module with new versions....
Read more >
Useful Python decorators for data scientists - Hacker News
I created a decorator for validating arguments for functions that are receiving arguments from user input. ... %load_ext autoreload %autoreload 2.
Read more >
Python – How to make VSCode auto-reload external *.py modules ...
To understand decorators, you must first understand that functions are objects in Python. This has important consequences. Let's see why with a simple ......
Read more >
Django Utils - Django documentation
django.utils.decorators ¶ ... Converts a function decorator into a method decorator. It can be used to decorate methods or classes; in the latter...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found