Skip to content

Allow modifying global variables from the variables panel #1857

Open

Description

@Andrej730

Environment data

  • debugpy version: 1.8.13
  • OS and version: Windows 11
  • Python version (& distribution if applicable, e.g. Anaconda): 3.12.7
  • Using VS Code or Visual Studio: VS Code 1.93.1

Actual behavior

Cursor_z3UFWwrl0Y.mp4

Expected behavior

To either prohibit editing global variable in that case as it's not editable or actually edit it.

Steps to reproduce:

  1. Set breakpoint at b = 35 in the snippet below and run it.
  2. Attach to Python using debugger.
  3. Change c value in Variables section to 1000.
  4. Step Over (F10), global variable is moved to locals and global variable value is reset.

What I've also noticed that if on step 3 I run globals()['c'] in Debug Console, it prints 0 and Variables are reset as we would be on the step 4.

c = 0

def main():
    a = 25

    import debugpy
    debugpy.listen(5678)
    debugpy.wait_for_client()

    b = 35
    print(a + b + c)

if __name__ == "__main__":
    main()

Activity

  1. added
    needs repro
    on Mar 5, 2025
  2. rchiodo commented on Mar 5, 2025

    @rchiodo
    Contributor

    I believe this is by design. It sets the value of 'c' in the local frame. This code here:

    If you want to force an eval to change a global, you have to do something like so:

    globals()['c'] = 1000
    
  3. Andrej730 commented on Mar 5, 2025

    @Andrej730
    Author

    But shouldn't it be not changable from UI or appear show it as local variable right away to prevent confusion?
    Or it's https://github.com/microsoft/vscode-python-debugger issue?

  4. rchiodo commented on Mar 5, 2025

    @rchiodo
    Contributor

    I agree it's confusing. Right now the logic for eval doesn't take into account the parent of the value.

    We do get the 'parent' for the variable though, so maybe we can tell that it's coming from the global list.

    Reopening as an enhancement request.

  5. added
    enhancement
    and removed
    needs repro
    on Mar 5, 2025
  6. changed the title [-]Global variables set from "Variables" moved to locals on Step[/-] [+]Allow modifying global variables from the variables panel[/+] on Mar 5, 2025
  7. rchiodo commented on Mar 5, 2025

    @rchiodo
    Contributor

    Here's the request in this example:

    D+00009.609: /handling #22 request "setVariable" from Client[1]/
                 Server[1] <-- {
                     "seq": 24,
                     "type": "request",
                     "command": "setVariable",
                     "arguments": {
                         "variablesReference": 4,
                         "name": "c",
                         "value": "1000"
                     }
                 }
    

    And you can see in the scopes request, that variables reference 4 is 'globals':

    D+00003.891: Server[1] --> {
                     "pydevd_cmd_id": 502,
                     "seq": 52,
                     "type": "response",
                     "request_seq": 20,
                     "success": true,
                     "command": "scopes",
                     "body": {
                         "scopes": [
                             {
                                 "name": "Locals",
                                 "variablesReference": 3,
                                 "expensive": false,
                                 "presentationHint": "locals",
                                 "source": {}
                             },
                             {
                                 "name": "Globals",
                                 "variablesReference": 4,
                                 "expensive": false,
                                 "source": {}
                             }
                         ]
                     }
                 }
    
  8. added
    Fixed in next release
    on Mar 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Fixed in next releaseenhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions