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

VSCode's "Run and Debug" or the Debugger itself seems to break Python code related to Protocols and isinstance checks #757

Closed
smbache opened this issue Oct 15, 2021 · 4 comments

Comments

@smbache
Copy link

smbache commented Oct 15, 2021

Environment data

  • VS Code version: 1.59, 1.60
  • Extension version (available under the Extensions sidebar): v2021.10.1336267007
  • OS and version: MacOS 11.5.2, Ubuntu 20.04
  • Python version (& distribution if applicable, e.g. Anaconda): 3.8, 3.9
  • Type of virtual environment used (N/A | venv | virtualenv | conda | ...): venv
  • Relevant/affected Python packages and their versions: N/A
  • Relevant/affected Python-related VS Code extensions and their versions: N/A
  • Value of the python.languageServer setting: default

Expected behaviour

To not error

Actual behaviour

Error raised when using "Debug and Run". No errors when running normally.

Steps to reproduce:

This is an issue I can only reproduce by running the code below using "Run and Debug" from vscode, and it relates to the Protocol feature introduced in Python 3.8, and using the debugger in vscode. I am not sure this is the right place to raise the issue, but could not think of a better place. I could not reproduce it with debugpy alone, so posting it here, rather than in the debugpy issues.

Consider the following simplified example:

from typing import Any, Protocol, runtime_checkable

@runtime_checkable
class Proto(Protocol):
    some_attr: int

class Foo(object):
    def __getattribute__(self, attr: str) -> Any:
        print(f"I have been summoned to deliver {attr}")
        result = object.__getattribute__(self, attr)

        print(f"I am delivering {result}")
        return result

if __name__ == "__main__":
    isinstance(Foo(), Proto)

Running this with "Run and Debug" gives this error
Screenshot 2021-10-15 at 08 50 15

However, running the file normally exits normally and gives the output:

I have been summoned to deliver some_attr
I have been summoned to deliver __class__
I am delivering <class '__main__.Foo'>

The isinstance call silently handles the error that occurs when accessing the attribute some_attr on an object on which it does not exist, which is also seen from the output where the second print statement is not reached in both invocations of __getatttribute__. Exactly what happens when the "Debug and Run" approach to execution is used is unclear to me, but it seems to break otherwise working code.

@hgarridoco
Copy link

Same error here. If I run without debuging works fine, but I need to debug!

@karthiknadig karthiknadig transferred this issue from microsoft/vscode-python Oct 15, 2021
@int19h
Copy link
Contributor

int19h commented Oct 15, 2021

The exception itself has always been there - you're seeing it now because now it gets displayed where it previously wasn't, but it's not actually fatal (i.e. you can press F5 and it'll continue running normally).

To prevent debugger from breaking on this exception, uncheck "User-uncaught exceptions" in the Breakpoints list in the Debug pane. We'll change the default for this checkbox in the next release of debugpy.

@anhvu-sg
Copy link

anhvu-sg commented Oct 16, 2021

The same problem, maybe it's related to the latest update, I just turn off my machine last night and this morning it doesn't work

File: "/Users/osx/.pyenv/versions/3.7.8/lib/python3.7/_collections_abc.py", line 662, in get return default File: "/Users/osx/.vscode/extensions/ms-python.python-2021.10.1336267007/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_trace_dispatch_regular.py", line 204, in trace_dispatch return thread_trace_func(frame, event, arg) File: "/Users/osx/.vscode/extensions/ms-python.python-2021.10.1336267007/pythonFiles/lib/python/debugpy/_vendored/pydevd/pydevd.py", line 1068, in enable_tracing pydevd_tracing.set_trace_to_threads(thread_trace_func) File: "/Users/osx/.vscode/extensions/ms-python.python-2021.10.1336267007/pythonFiles/lib/python/debugpy/_vendored/pydevd/pydevd_tracing.py", line 90, in SetTrace return File: "/Users/osx/.vscode/extensions/ms-python.python-2021.10.1336267007/pythonFiles/lib/python/debugpy/_vendored/pydevd/pydevd_tracing.py", line 374, in set_trace_to_threads return ret

Note that:

  • Without debugging -> works fine
  • Debugging -> fail

image

@fabioz
Copy link
Collaborator

fabioz commented Oct 16, 2021

This is a duplicate of #751

Please uncheck User Uncaught Exceptions to avoid this.

@fabioz fabioz closed this as completed Oct 16, 2021
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

No branches or pull requests

5 participants