This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

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

Created on 2019-08-26 14:06 by plokmijnuhby, last changed 2022-04-11 14:59 by admin. 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
PR 18751 merged python-dev, 2020-03-02 21:00
Messages (7)
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
msg363313 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2020-03-03 22:29
New changeset 3eff46fc7d2e3c80c4dedba4177782f1fc8ad89b by Ryan Rowe in branch '3.7':
bpo-37953: Fix ForwardRef hash and equality checks (GH-15400) (GH-18751)
https://github.com/python/cpython/commit/3eff46fc7d2e3c80c4dedba4177782f1fc8ad89b
History
Date User Action Args
2022-04-11 14:59:19adminsetgithub: 82134
2020-03-03 22:30:18ned.deilysetversions: + Python 3.7, Python 3.8
2020-03-03 22:29:44ned.deilysetnosy: + ned.deily
messages: + msg363313
2020-03-02 21:00:30python-devsetnosy: + python-dev

pull_requests: + pull_request18106
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