-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
bpo-37953: Fix ForwardRef equality checks #15400
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
Maybe this will cut down on hash collisions
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 reasonable, but let's make sure @ilevkivskyi agrees.
Can you add a test (preferably one that shows what's not working without the fix)?
It's fairly unorthodox to have a PR without a bug in the tracker -- can you create a bug describing the problem and link this to it? Also please add a news item.
TBH I don't understand what problem is being solved. This definitely requires tests. |
The problem seems clear enough to me: The I'm not clear however on why someone would use a |
OK, I see how the current status is wrong. On the other hand, just removing the value for evaluated forward references will make two references referring to different classes with the same name equal. I think that the first one is more important, so I am fine with just removing the |
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.
Mostly looks good, just one question.
Please also add a NEWS
item.
I try a test in this PR:
Output I'm afraid of removing the |
[edited] @hongweipeng, I see what you mean now. This is definitely tricky. I think combining the |
@brandtbucher Yeah, I want to use Now I think that |
TBH I don't really like making IOW I am fine with
|
Removing |
Do you mean removing it from Generally, I would propose to keep only one PR open, because currently the discussion is scattered between two PRs. @plokmijnuhby Are you interested in continuing to work on this PR? |
@ilevkivskyi Yes, we can remove |
@ilevkivskyi I'm happy to keep working on it, but I'm not quite sure how where we want to take this. It seems like there are still a few ideas bouncing around here. I mean, in theory we could remove the caching thing entirely, and just perform a lookup of the class name in a function's |
I'm not entirely certain if this is something we'd consider to be a behavioral changing enhancement or if the current behavior is an actual bug. For now, I'll categorize it as enhancement but feel free to change it. |
Alright, I don't really agree with this but I seem to be in the minority, so I'll change it. Co-Authored-By: Brandt Bucher <[email protected]>
Okay, here's my thoughts on forward references. @hongweipeng's code example doesn't make much sense to me because users are unlikely to evaluate references right after creating them, i.e. in the same calling context, but much more likely to have someone else's code evaluate them a little later, in a completely different calling context. The trouble is that we have no good way of determining the correct context after we've left it. There are several possible solutions to this:
|
So we now have two competing almost identical PRs: this one and #15650. This one was first, but the other has more tests. I think the best way is to polish this one, so I am going to close the alternative one. |
@@ -0,0 +1,3 @@ | |||
Forward references in the typing module now have different hash and eq methods. References will now compare equal properly if only one of them has been evaluated and the other hasn't. Hash values are now consistent across the lifetime of a forward reference. |
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.
Use `__hash__`
and `__eq__`
instead of just hash
and eq
. Also this news item is too long, couple short sentences (or even one) should be enough.
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 changed it, is that better?
Add extra tests from pull request #15650.
Misc/NEWS.d/next/Library/2019-09-06-17-40-34.bpo-37953.db5FQq.rst
Outdated
Show resolved
Hide resolved
Co-Authored-By: Kyle Stanley <[email protected]>
Also, thanks for the PR and welcome @plokmijnuhby. (: |
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 updates! LGTM now.
Thanks everyone for help! @plokmijnuhby To make things consistent, you might also want to add this fix to the backport at https://github.com/python/typing. |
Thanks @plokmijnuhby for the PR, and @ilevkivskyi for merging it 🌮🎉.. I'm working now to backport this PR to: 3.8. |
GH-16128 is a backport of this pull request to the 3.8 branch. |
Ideally if we stick a ForwardRef in a dictionary we would like to reliably be able to get it out again. https://bugs.python.org/issue37953 (cherry picked from commit e082e7c) Co-authored-by: plokmijnuhby <[email protected]>
Ideally if we stick a ForwardRef in a dictionary we would like to reliably be able to get it out again. https://bugs.python.org/issue37953 (cherry picked from commit e082e7c) Co-authored-by: plokmijnuhby <[email protected]>
Ideally if we stick a ForwardRef in a dictionary we would like to reliably be able to get it out again.
https://bugs.python.org/issue37953