Skip to content

repr () not used in window displays (Issue #1661 continued) #114

Description

@mrolle45

In safe_repr.py, ptvsd estimates whether repr (obj) might be too long to send to the IDE. In my code, I have a class derived from list, and if I have more than two levels of nesting, it is considered too long.

Please consider the following solution: If the obj being displayed has a method called 'safe_repr', then this is called directly. I put the following in my local copy of safe_repr.py:
def call(self, obj):

  •    try: return obj.__safe_repr__ ()
    
  •    except Exception: pass
    

The name 'safe_repr' is something I made up. You might want to provide some other way to tell SafeRepr (obj) that repr (obj) is OK to use.

Environment data

  • VS Code version: 2017
  • OS and version: Windows 10
  • Python version: 3.7

Activity

  1. int19h commented on Apr 15, 2020

    @int19h
    Contributor

    This is something that really should be tackled on the Python side of things, because it's a problem that affects all IDEs (and really anything that has to visualize Python data structures). This is not a trivial design issue, because such a hook needs to allow objects to coordinate - e.g. in your case you don't want to apply nesting limitations, but other collections might.

    OTOH if Python does it, it can be designed much more flexibly than our implementation - for example, instead of expecting the repr to return the value, it can supply a builder object with methods to append text, and to recursively invoke repr on a contained value; and the builder can then keep track of things like nesting level or number of elements rendered. The stdlib collections could then also implement it properly.

    Brett Cannon (@brettcannon), is that something that would be PEP-worthy? The gist is to have a form of repr() that can limit the size of the output, for cases like collections of thousands of elements, or lists that nest so deeply that you get too many [...].

  2. brettcannon commented on Apr 16, 2020

    @brettcannon
    Member

    Pavel Minaev (@int19h) you could propose it on python-ideas and see if people are positive on the idea.

  3. added this to the Dev 17.x milestone on Oct 20, 2021
  4. locked and limited conversation to collaborators on Feb 1, 2023
  5. converted this issue into a discussion #1203 on Feb 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions