Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
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 4 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 10 Objects/unionobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,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
Morty Proxy This is a proxified and sanitized view of the page, visit original site.