Skip to content
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

Match CPython's __hash__ overriding behavior when __eq__ exists #5501

Merged
merged 1 commit into from
Jul 14, 2023

Conversation

TheTripleV
Copy link
Contributor

https://github.com/python/cpython/blob/main/Objects/typeobject.c#L6794 checks if __hash__ is overridden (__eq__ or __hash__ is defined) and stops __hash__ from being copied from the base (https://github.com/python/cpython/blob/main/Objects/typeobject.c#L6953). https://github.com/python/cpython/blob/main/Objects/typeobject.c#L7354 sets __hash__ to None if __hash__ isn't found in the dict.

This should match CPython's behavior.

closes #5446

@erictraut erictraut merged commit 3efc3f2 into microsoft:main Jul 14, 2023
10 checks passed
@github-actions
Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

bidict (https://github.com/jab/bidict)
+   /tmp/mypy_primer/projects/bidict/tests/property_tests/test_properties.py:357:13 - error: Set entry must be hashable
+     Type "Bi" is not hashable (reportGeneralTypeIssues)
+   /tmp/mypy_primer/projects/bidict/tests/property_tests/test_properties.py:358:13 - error: Dictionary key must be hashable
+     Type "Bi" is not hashable (reportGeneralTypeIssues)
- 12 errors, 0 warnings, 0 informations 
+ 14 errors, 0 warnings, 0 informations 

pandas-stubs (https://github.com/pandas-dev/pandas-stubs)
+   /tmp/mypy_primer/projects/pandas-stubs/tests/test_pandas.py:157:23 - error: Argument of type "list[DataFrame]" cannot be assigned to parameter "objs" of type "Iterable[Series[Unknown]] | Mapping[HashableT1@concat, Series[Unknown]]" in function "concat"
+     "DataFrame" is incompatible with "Series[Unknown]"
+     "DataFrame" is incompatible with "Series[Unknown]" (reportGeneralTypeIssues)
+   /tmp/mypy_primer/projects/pandas-stubs/tests/test_pandas.py:157:62 - error: Argument of type "list[Timedelta]" cannot be assigned to parameter "names" of type "list[HashableT4@concat]" in function "concat"
+     Type "Timedelta" cannot be assigned to type "Hashable"
+       "Timedelta" is incompatible with protocol "Hashable"
+         "__hash__" is an incompatible type
+           Type "None" cannot be assigned to type "(self: Timedelta) -> int" (reportGeneralTypeIssues)
+   /tmp/mypy_primer/projects/pandas-stubs/tests/test_pandas.py:157:13 - error: "assert_type" mismatch: expected "DataFrame" but received "Unknown" (reportGeneralTypeIssues)
+   /tmp/mypy_primer/projects/pandas-stubs/tests/test_pandas.py:1601:13 - error: Dictionary key must be hashable
+     Type "Timestamp" is not hashable (reportGeneralTypeIssues)
+   /tmp/mypy_primer/projects/pandas-stubs/tests/test_pandas.py:1602:13 - error: Dictionary key must be hashable
+     Type "Timedelta" is not hashable (reportGeneralTypeIssues)
- 1243 errors, 0 warnings, 0 informations 
+ 1248 errors, 0 warnings, 0 informations 

Expression (https://github.com/cognitedata/Expression)
+ /tmp/mypy_primer/projects/Expression/expression/collections/map.py
+   /tmp/mypy_primer/projects/Expression/expression/collections/map.py:215:5 - error: "__hash__" overrides symbol of same name in class "Mapping"
+     Type cannot be assigned to type "None" (reportIncompatibleVariableOverride)
+   /tmp/mypy_primer/projects/Expression/expression/collections/map.py:215:5 - error: Instance variable "__hash__" overrides class variable of same name in class "Mapping" (reportIncompatibleVariableOverride)
- 278 errors, 0 warnings, 0 informations 
+ 280 errors, 0 warnings, 0 informations 

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.

Some unhashable types are not detected as unhashable
2 participants