Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
gh-116647: Fix recursive child in dataclasses#116790
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
Uh oh!
There was an error while loading. Please reload this page.
Conversation
ghost commented Mar 14, 2024 • edited by bedevere-app bot
Loading Uh oh!
There was an error while loading. Please reload this page.
edited by bedevere-app bot
Uh oh!
There was an error while loading. Please reload this page.
ghost commented Mar 14, 2024 • edited by ghost
Loading Uh oh!
There was an error while loading. Please reload this page.
edited by ghost
Uh oh!
There was an error while loading. Please reload this page.
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
Lib/dataclasses.py Outdated
| # since python will call __eq__ and negate it. | ||
| cmp_fields= (fieldforfieldinfield_listiffield.compare) | ||
| terms= [f'self.{field.name}==other.{field.name}'forfieldincmp_fields] | ||
| terms= [f'(self.{field.name},)==(other.{field.name},)'forfieldincmp_fields] |
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.
Is this an improvement over just reverting to the code before 18cfc1e?
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.
I think this is just reverting to the code before 18cfc1e
due to (object_a, ) == (object_b) will first compare ids of objects, if it's not same then call eq of the object, that may not cause RecursionError: maximum recursion depth exceeded when the objects pointers are the same
but object_a == object_b directly calls object.eq whether the pointers is same or not, so i think remove 18cfc1e will solve the errors and provides better performance when the child have the same pointers will cause early return without calling eq of objects
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.
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.
Thanks for the comment, I've improved the efficiency base on 18cfc1e now it'll compare id(obj) first to make sure if the child point to themselves will not cause maximum recursion depth and it'll be faster when compare exact same objects.
Jason-Y-Z left a comment
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.
Overall LGTM, a small comment on the test case
| classTestEq(unittest.TestCase): | ||
| deftest_recursive_eq(self): |
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.
Thanks for the change! Per discussions in the issue, maybe also worth adding a test case for mutual recursive classes (given that's intended to be included in the scope of this PR)?
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.
Thanks for the comment, but I don't think mutual recursive classes can be implement with good performance, I've think ways like maintenance dict like id(obj) as key to check if it's recursive but it'll cost spaces and times to make compare like that.
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.
Make sense, thanks for the clarification!
Uh oh!
There was an error while loading. Please reload this page.
#116647