Skip to content

Commit 173774d

Browse files
recfacebook-github-bot
authored andcommitted
Replace __str__ with __repr__ in some places (#136316)
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
1 parent a21b30e commit 173774d

File tree

1 file changed

+4
-4
lines changed
  • userbenchmark/dynamo/dynamobench/_dynamo

1 file changed

+4
-4
lines changed

userbenchmark/dynamo/dynamobench/_dynamo/utils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2479,7 +2479,7 @@ def __init__(self, f):
24792479
self.f = f
24802480
self.__name__ = "wrapped_" + self.f.__name__
24812481

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

24852485
def __call__(self, *args, **kwargs):
@@ -2503,7 +2503,7 @@ def __init__(self, method: str):
25032503
self.method = method
25042504
self.__name__ = "wrapped_" + self.method
25052505

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

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

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

25282528
def __call__(self, *args, **kwargs):
@@ -3097,7 +3097,7 @@ class Lit:
30973097
def __init__(self, s):
30983098
self.s = s
30993099

3100-
def __repr__(self):
3100+
def __repr__(self) -> str:
31013101
return self.s
31023102

31033103

0 commit comments

Comments
 (0)