-
Notifications
You must be signed in to change notification settings - Fork 24.6k
[dynamo] Replace __str__ with __repr__ in some places #136316
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
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/136316
Note: Links to docs will display an error until the docs builds have been completed. ✅ You can merge normally! (1 Unrelated Failure)As of commit c7182dd with merge base f568d48 ( FLAKY - The following job failed but was likely due to flakiness present on trunk:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
@pytorchbot rebase |
@pytorchbot started a rebase job onto refs/remotes/origin/viable/strict. Check the current status here |
Successfully rebased |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 for better debug info, can you show a before/after example for context?
@amjames: I decided to simply set breakpoints at all the constructors of classes changed and then ran every test in
|
@amjames: ideas for reviewers? The |
@pytorchbot merge |
Merge failedReason: Approvers from one of the following sets are needed:
|
@pytorchbot merge |
Merge startedYour change will be merged once all checks pass (ETA 0-4 Hours). Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
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
The problem
In a typical debugger,
repr()
is used to display variables and notstr()
.Several classes in Dynamo have a
__str__()
method that returns useful information and a__repr__()
that does not. Having to callstr(x)
or[str(i) for i in x]
in the debugger all the time is a chore.str()
should be "informal, nicely printable" andrepr()
should "attempt to return a string that would yield an object with the same value when passed to eval()".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
Stack from ghstack (oldest at bottom):
cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @chenyang78 @kadeng @chauhang @amjames