Skip to content

Commit

Permalink
Replace __str__ with __repr__ in some places (#136316)
Browse files Browse the repository at this point in the history
Summary:
## The problem

In a typical debugger, `repr()` is used to display variables and not `str()`.

Several classes in Dynamo have a `__str__()` method that returns useful information and a  `__repr__()` that does not. Having to call `str(x)` or `[str(i) for i in x]` in the debugger all the time is a chore.

`str()` should be ["informal, nicely printable"](https://docs.python.org/3/library/stdtypes.html#str) and `repr()` should ["attempt to return a string that would yield an object with the same value when passed to eval()](https://docs.python.org/3/library/functions.html#repr)".

## The solution

In the Python object model, if there is no `__str__` method, `__repr__`  is used instead (but not the other way around).

So renaming `__str__` to `__repr__` in a few cases where no `__repr__` method exists now should not change observable behavior, and should make debugging easier.

The specific classes changed were all in `torch._dynamo.variables`:

* `builtin.BuiltinVariable`
* `constant.ConstantVariable`
* `constant.EnumVariable`
* `functions.UserMethodVariable`
* `lazy.LazyVariableTracker`
* `lazy.LazySymNodeFormatString`
* `misc.GetAttrVariable`
* `misc.NullVariable`
* `user_defined.UserDefinedObjectVariable`

X-link: pytorch/pytorch#136316
Approved by: https://github.com/XuehaiPan, https://github.com/jansel

Reviewed By: wdvr

Differential Revision: D64714511

fbshipit-source-id: 322f2f0110e5b45afe6a27c52a0bcc91d91d1d6a
  • Loading branch information
rec authored and facebook-github-bot committed Oct 22, 2024
1 parent a21b30e commit 173774d
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions userbenchmark/dynamo/dynamobench/_dynamo/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2479,7 +2479,7 @@ def __init__(self, f):
self.f = f
self.__name__ = "wrapped_" + self.f.__name__

def __repr__(self):
def __repr__(self) -> str:
return f"<Wrapped function <original {self.f.__name__}>>"

def __call__(self, *args, **kwargs):
Expand All @@ -2503,7 +2503,7 @@ def __init__(self, method: str):
self.method = method
self.__name__ = "wrapped_" + self.method

def __repr__(self):
def __repr__(self) -> str:
return f"<Wrapped method <original {self.method}>>"

def __call__(self, *args, **kwargs):
Expand All @@ -2522,7 +2522,7 @@ def __init__(self, op: Callable[..., Any]):
self.op = op
self.__name__ = f"wrapped_{op.__name__}"

def __repr__(self):
def __repr__(self) -> str:
return f"<Wrapped operator <original {self.__name__}>>"

def __call__(self, *args, **kwargs):
Expand Down Expand Up @@ -3097,7 +3097,7 @@ class Lit:
def __init__(self, s):
self.s = s

def __repr__(self):
def __repr__(self) -> str:
return self.s


Expand Down

0 comments on commit 173774d

Please sign in to comment.