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.

"Run Selection" does not handle decorators properly.

See original GitHub issue
def my_decorator(func):
    def wrapper():
        print("Something is happening before the function is called.")
        func()
        print("Something is happening after the function is called.")
    return wrapper


@my_decorator
def say_whee():
    print("Whee!")


say_whee()

shift+enter or ‘run’ button in vscode

>>> def my_decorator(func):
...     def wrapper():
...         print("Something is happening before the function is called.")
...         func()
...         print("Something is happening after the function is called.")
...     return wrapper
... @my_decorator
  File "<stdin>", line 7
    @my_decorator
    ^
SyntaxError: invalid syntax
>>>
>>> def say_whee():
...     print("Whee!")
...
>>> say_whee()
Whee!
>>>

python extension v2021.1.442908725-dev

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:12
  • Comments:24 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
kimadelinecommented, Nov 29, 2021

Verification steps:

  • Download the latest Insiders version of the Python extension
  • Copy the code from the original message:
def my_decorator(func):
    def wrapper():
        print("Something is happening before the function is called.")
        func()
        print("Something is happening after the function is called.")
    return wrapper


@my_decorator
def say_whee():
    print("Whee!")


say_whee()
  • Select it all and press shift + enter or run the Run Selection/Line in Terminal command
  • Make sure that the code is passed to the terminal and executed without any errors
1reaction
JordyScriptcommented, May 14, 2021

What appears to be happening is the following:

  • the decorator gets executed as a single line in isolation, which produces a syntax error.
  • the undecorated/plain python statement beneath the decorator gets executed in isolation.

Ways of executing code with decorators that do seem to be working normally are:

  • Copying an pasting code into the REPL
  • Executing the file from the command line using python [script-path]
  • Using “Python: Run Selection/Line in Python Terminal” (shift + enter) if the decorator is indented as part of some scope (e.g. within an if statement or within a class definition)

Hence it is only the top level decorators that don’t work with “Python: Run Selection/Line in Python Terminal” (shift + enter) and it seems to be because the lines get evaluated / executed in isolation if they are not part of some indented block or pasted in as a whole. If “Python: Run Selection/Line in Python Terminal” (shift + enter) could be made to work like pasting from the clipboard works, then the problem would be solved.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Simple decorator does not work with Python multiprocessing
I have built a simple decorator that tracks if the function has been run or not. import functools from multiprocessing import Process, ...
Read more >
Primer on Python Decorators
In this introductory tutorial, we'll look at what Python decorators are and how to create and use them.
Read more >
Decorators - Python 3 Patterns, Recipes and Idioms
The only constraint on the result of a decorator is that it be callable, so it can properly replace the decorated function. In...
Read more >
Understanding Python Decorators - Techblog
We look at how python decorators work using an example from the python decorator library - one which retries function execution on failure....
Read more >
Decorators - Storybook - JS.ORG
A decorator is a way to wrap a story in extra “rendering” functionality. Many addons define decorators to augment your stories with extra...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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