-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
bpo-37953:Improve ForwardRef equality checks #15628
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
bpo-37953:Improve ForwardRef equality checks #15628
Conversation
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.
This looks like a good change. I found one optimization that can be made, below.
Can you also add regression tests as well?
Lib/typing.py
Outdated
for p in params: | ||
if p not in new_params: | ||
new_params.append(p) | ||
params = new_params | ||
return tuple(params) |
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.
This whole bit can be sped up by just replacing it with:
return tuple(dict.fromkeys(params))
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.
Sorry, I should have been clearer! I meant replacing this whole list + loop bit with that one line. dict.fromkeys
is a nice shortcut to remove duplicates while preserving order.
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.
Ah... I see now. You're trying to avoid the (broken) hashing.
I don't know how I feel about that. I think hashing should be fixed rather than just sidestepped here, since it's so tied to equality. At this point I'm actually in favor of a hybrid approach: combining your fix for __eq__
and the other PR #15400’s fix for __hash__
.
@gvanrossum and @ilevkivskyi, thoughts?
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.
@hongweipeng you can revert your most recent commit (3676a50, “code review”). My comment was misguided!
params = new_params | ||
assert not all_params, all_params | ||
return tuple(params) | ||
return tuple(dict.fromkeys(params)) |
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.
TBH this looks like a hack. If at some point we would make sets ordered the same way as dicts are, it would be good to update the code, but currently I think I like the old way more.
See also my comment #15400 (comment) in the other PR. |
PR #15650 is better. |
https://bugs.python.org/issue37953