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

Variable viewer: Class value info is shown for expensive getter functions #10385

Closed
1 of 2 tasks
pvanschendel opened this issue Jun 9, 2022 · 4 comments
Closed
1 of 2 tasks
Assignees
Labels
bug Issue identified by VS Code Team member as probable bug triage-needed Issue needs to be triaged

Comments

@pvanschendel
Copy link

pvanschendel commented Jun 9, 2022

Applies To

  • Notebooks (.ipynb files)
  • Interactive Window and/or Cell Scripts (.py files with #%% markers)

What happened?

Unsure if this formally is a bug, but it is definitely bugging me.

The problem that I experience is that mouse hover (and the Variable Viewer) calls the str function on member variables of the class, which in turn call expensive getters. I hope the code below more clearly demonstrates the issue.
When the first cell has been exectuted, the hover shows the text "return value of expensive getter function".

I think these settings.json entries might prevent the issue, but they do not:

  • "debug.inlineValues": "off",
  • "jupyter.showVariableViewWhenDebugging": false,

Can you let me know if this is a bug, or my misunderstanding?

This settings.json entry does work for the code below, but is not really feasible, because we have a lot of generated classes, so the class names may vary:

  • "jupyter.variableExplorerExclude": "module;function;builtin_function_or_method;ABCMeta;type;GetterIssue",

Perhaps a second issue: I would expect hover and Variable View to call __repr__ rather than __str__ .

# %%

class GetterIssue:
    """Class that has an expensive getter property."""
    def __init__(self):
        pass

    def __str__(self):
        return "getter_property: " + self.getter_property

    def __repr__(self):
        return "getter_property: <getter_property(self)>"

    @property
    def getter_property(self):
        """Calling this getter on hover or Variable View is problematic."""
        return "return value of expensive getter function"


# The problem occurs when the cursor hovers over the text 'my_issue' below:
my_issue = GetterIssue()

# %%

print(str(my_issue)) # This is the function I would like to keep
repr(my_issue) # Hacked to not use the getter, probably not generally acceptable

VS Code Version

1.67.2

Jupyter Extension Version

v2022.4.1021342353

Jupyter logs

No response

Coding Language and Runtime Version

No response

Language Extension Version (if applicable)

v2022.6.3

Anaconda Version (if applicable)

No response

Running Jupyter locally or remotely?

Local

@pvanschendel pvanschendel added bug Issue identified by VS Code Team member as probable bug triage-needed Issue needs to be triaged labels Jun 9, 2022
@IanMatthewHuff
Copy link
Member

@pvanschendel Looping back to check if I'm understanding the issue correctly here. Currently with hover (and variable view) you'd see something like this:
image
image

From my knowledge of python this is expect, for example when just debugging a .py file (not using Jupyter / interactive window) we see similar:
image

I would think the issue here is that str or repr should not be calling an expensive getter. Am I maybe not getting part of this scenario that you are asking about?

Also, I believe that debugging would expect to show str over repr as that's the expected "reading friendly" representation of the object.

@pvanschendel
Copy link
Author

Thanks for taking the trouble to check this. It is already good to know that this is expected behavior, so we have to live with it.

What was unexepcted for me is that these calls are (sometimes, when no cached value is available ?) also mode when only editing the code, so not debugging or otherwise running it, so I have to be really careful where to put my cursor,
I guess my mistake is that, if I implement a special function like str, I should be prepared that any editor or other tool may be calling this, not just user code.

@IanMatthewHuff
Copy link
Member

Yeah, str and repr can be called implicitly, especially by something like a debugger not just directly. In general you don't want them to be slow. This article here might help a bit with understanding them:
https://stackoverflow.com/questions/1436703/what-is-the-difference-between-str-and-repr

@pvanschendel
Copy link
Author

Thanks, I am a bit wiser again.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 10, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue identified by VS Code Team member as probable bug triage-needed Issue needs to be triaged
Projects
None yet
Development

No branches or pull requests

2 participants