-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
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
Fix ForwardRef equality checks #82134
Comments
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 |
There are deprecation warnings in test_typing. ./python -m test test_typing == Tests result: SUCCESS == 1 test OK. Total duration: 215 ms I've created PR 16133 to fix them. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: