Skip to content

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

Closed

Conversation

hongweipeng
Copy link
Contributor

@hongweipeng hongweipeng commented Aug 31, 2019

Copy link
Member

@brandtbucher brandtbucher left a 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)
Copy link
Member

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))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I will add some tests. By the way, params is a list, tuple(dict.fromkeys(params)) seems not faster than tuple(params), I think it would be better to keep tuple(params):

20190901004654

Copy link
Member

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.

Copy link
Member

@brandtbucher brandtbucher Sep 1, 2019

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?

Copy link
Member

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))
Copy link
Member

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.

@ilevkivskyi
Copy link
Member

See also my comment #15400 (comment) in the other PR.

@hongweipeng
Copy link
Contributor Author

PR #15650 is better.

@hongweipeng hongweipeng closed this Sep 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants