Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Lib/test/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,10 @@ def test_or_types_operator(self):
x.__args__ = [str, int]
(int | str ) == x

def test_hash(self):
self.assertEqual(hash(int | str), hash(str | int))
self.assertEqual(hash(int | str), hash(typing.Union[int, str]))

def test_instancecheck(self):
x = int | str
self.assertIsInstance(1, x)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix the hash of the union type: it no longer depends on the order of
arguments.
10 changes: 6 additions & 4 deletions Objects/unionobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ static Py_hash_t
union_hash(PyObject *self)
{
unionobject *alias = (unionobject *)self;
Py_hash_t h1 = PyObject_Hash(alias->args);
if (h1 == -1) {
return -1;
PyObject *args = PyFrozenSet_New(alias->args);
if (args == NULL) {
return (Py_hash_t)-1;
}
return h1;
Py_hash_t hash = PyObject_Hash(args);
Py_DECREF(args);
return hash;
}

static int
Expand Down