classification
Title: Fix ForwardRef equality checks
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.9
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: ZackerySpytz, levkivskyi, miss-islington, plokmijnuhby
Priority: normal Keywords: patch

Created on 2019-08-26 14:06 by plokmijnuhby, last changed 2019-09-14 08:02 by miss-islington. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 15400 merged plokmijnuhby, 2019-08-28 11:56
PR 15628 closed hongweipeng, 2019-08-31 05:03
PR 15650 closed hongweipeng, 2019-09-02 16:26
PR 16128 merged miss-islington, 2019-09-13 19:41
PR 16133 merged ZackerySpytz, 2019-09-14 06:12
PR 16138 merged miss-islington, 2019-09-14 07:44
Messages (6)
msg350531 - (view) Author: Dominic Littlewood (plokmijnuhby) * Date: 2019-08-26 14:06
Apologies for issuing a pull request without an associated issue. I'm kind of new to this. Nevermind, I'm making one now.

The typing module currently contains a bug where ForwardRefs change their hash and equality once they are evaluated. Consider the following code:

import typing
ref = typing.ForwardRef('MyClass')
ref_ = typing.ForwardRef('MyClass')


class MyClass:
    def __add__(self, other: ref): ...


# We evaluate one forward reference, but not the other.
typing.get_type_hints(MyClass.__add__)

# Equality is violated
print(ref == ref_) # False

# This can cause duplication in Unions.
# The following prints:
# typing.Union[ForwardRef('MyClass'), ForwardRef('MyClass')]
# when it should be: typing.Union[ForwardRef('MyClass')]
wrong = typing.Union[ref, ref_]
print(wrong)

# The union also does not compare equality properly
should_be_equal = typing.Union[ref]
print(should_be_equal == wrong) # False

# In fact this applies to any generic
print(typing.Callable[[ref],None] == typing.Callable[[ref_],None]) # False
msg352389 - (view) Author: Ivan Levkivskyi (levkivskyi) * (Python committer) Date: 2019-09-13 19:41
New changeset e082e7cbe4a934b86f7a07354d97d4e14a9dd46a by Ivan Levkivskyi (plokmijnuhby) in branch 'master':
bpo-37953: Fix ForwardRef hash and equality checks (GH-15400)
https://github.com/python/cpython/commit/e082e7cbe4a934b86f7a07354d97d4e14a9dd46a
msg352391 - (view) Author: miss-islington (miss-islington) Date: 2019-09-13 20:00
New changeset e91edfed4214dbae17b8906b5dc7778769aac620 by Miss Islington (bot) in branch '3.8':
bpo-37953: Fix ForwardRef hash and equality checks (GH-15400)
https://github.com/python/cpython/commit/e91edfed4214dbae17b8906b5dc7778769aac620
msg352413 - (view) Author: Zackery Spytz (ZackerySpytz) * (Python triager) Date: 2019-09-14 06:18
There are deprecation warnings in test_typing.

./python -m test test_typing
Run tests sequentially
0:00:00 load avg: 0.16 [1/1] test_typing
/home/lubuntu2/cpython/Lib/test/test_typing.py:2382: DeprecationWarning: Please use assertEqual instead.
  self.assertEquals(Union[c1, c1_gth], Union[c1])
/home/lubuntu2/cpython/Lib/test/test_typing.py:2383: DeprecationWarning: Please use assertEqual instead.
  self.assertEquals(Union[c1, c1_gth, int], Union[c1, int])

== Tests result: SUCCESS ==

1 test OK.

Total duration: 215 ms
Tests result: SUCCESS


I've created PR 16133 to fix them.
msg352419 - (view) Author: miss-islington (miss-islington) Date: 2019-09-14 07:43
New changeset d057b896f97e6d7447b9bf9246770c41cf205299 by Miss Islington (bot) (Zackery Spytz) in branch 'master':
bpo-37953: Fix deprecation warnings in test_typing (GH-16133)
https://github.com/python/cpython/commit/d057b896f97e6d7447b9bf9246770c41cf205299
msg352421 - (view) Author: miss-islington (miss-islington) Date: 2019-09-14 08:02
New changeset 66da347ef0034ad9bddc7fad112025c886249f0d by Miss Islington (bot) in branch '3.8':
bpo-37953: Fix deprecation warnings in test_typing (GH-16133)
https://github.com/python/cpython/commit/66da347ef0034ad9bddc7fad112025c886249f0d
History
Date User Action Args
2019-09-14 08:02:23miss-islingtonsetmessages: + msg352421
2019-09-14 07:44:27miss-islingtonsetpull_requests: + pull_request15748
2019-09-14 07:43:02miss-islingtonsetmessages: + msg352419
2019-09-14 06:18:26ZackerySpytzsetnosy: + ZackerySpytz
messages: + msg352413
2019-09-14 06:12:05ZackerySpytzsetpull_requests: + pull_request15745
2019-09-13 20:05:54levkivskyisetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2019-09-13 20:00:42miss-islingtonsetnosy: + miss-islington
messages: + msg352391
2019-09-13 19:41:05miss-islingtonsetpull_requests: + pull_request15739
2019-09-13 19:41:01levkivskyisetmessages: + msg352389
2019-09-02 16:26:18hongweipengsetpull_requests: + pull_request15317
2019-08-31 14:38:17gvanrossumsetnosy: - gvanrossum
2019-08-31 05:03:02hongweipengsetpull_requests: + pull_request15296
2019-08-28 11:56:01plokmijnuhbysetkeywords: + patch
stage: patch review
pull_requests: + pull_request15235
2019-08-26 14:19:53SilentGhostsetnosy: + gvanrossum, levkivskyi
2019-08-26 14:06:04plokmijnuhbycreate