[New-bugs-announce] [issue37953] Fix ForwardRef equality checks

Dominic Littlewood report at bugs.python.org
Mon Aug 26 10:06:04 EDT 2019


New submission from Dominic Littlewood <11dlittlewood at gmail.com>:

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

----------
components: Library (Lib)
messages: 350531
nosy: plokmijnuhby
priority: normal
severity: normal
status: open
title: Fix ForwardRef equality checks
type: behavior
versions: Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue37953>
_______________________________________


More information about the New-bugs-announce mailing list