Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add __hash__ for a bunch of types that set it to None #13286

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

Merged
merged 2 commits into from
Dec 24, 2024

Conversation

tungol
Copy link
Contributor

@tungol tungol commented Dec 23, 2024

I found these by removing __hash__ from stubtest's list of ignorable dunders.

_collections_abc.Mapping.__hash__ and _collections_abc.Set.__hash__ should also be here, but their counterparts in typing do have a __hash__ method, so I left that alone.

Related to #2148

This unfortunately has little practical effect until python/mypy#4266 is fixed.

Copy link
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@JelleZijlstra JelleZijlstra merged commit 3944c78 into python:main Dec 24, 2024
66 checks passed
@tungol tungol deleted the hash branch December 24, 2024 10:25
@@ -102,6 +102,7 @@ class Complex(Number, _ComplexLike):
def conjugate(self) -> _ComplexLike: ...
@abstractmethod
def __eq__(self, other: object) -> bool: ...
__hash__: ClassVar[None] # type: ignore[assignment]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Number class states that "Concrete numeric types must provide their own hash implementation"

Feel free to enlighten me if I've missed something, but doesn't the change here mean that you can't actually do that? So for example, doesn't this line mean that the following wouldn't work?

from numbers import Complex

class Test(Complex):
	def __hash__(self) -> int:
		return 0

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. I tested it, and this requires the use of # type: ignore[override] to type check with mypy correctly. That's not exactly wrong, but isn't a great experience either. I can see an argument for either way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants