bpo-44676: Add ability to serialise types.Union#27244
Conversation
| s = pickle.dumps(alias) | ||
| loaded = pickle.loads(s) | ||
| self.assertEqual(alias.__args__, loaded.__args__) | ||
| self.assertEqual(alias.__parameters__, loaded.__parameters__) |
There was a problem hiding this comment.
Those are good checks 👍🏻
I'd also put a plain self.assertEqual(alias, loaded). At the moment it's redundant with comparing __args__ but this might not be true later, so this will keep future-proof.
There was a problem hiding this comment.
Thanks, I added self.assertEqual(alias, loaded) line to test.
Lib/test/test_types.py
Outdated
| ): | ||
| types.Union.from_args(1) | ||
|
|
||
| alias = types.Union.from_args((int, str, T)) |
There was a problem hiding this comment.
At first I was a little weirded out by the single argument that is a tuple but looking at actual usage in typing.py this is the right choice 👍🏻
Using literals like in this test will be a rare occurence because then you can use the regular syntax for the union.
There was a problem hiding this comment.
Agree, I used from_args only to test behaviour of this classmethod.
| } | ||
|
|
||
| static int | ||
| is_args_unionable(PyObject *args) |
There was a problem hiding this comment.
Note to self: this is extracted from L450 below.
|
@Fidget-Spinner I also cover case when |
# Conflicts: # Lib/test/test_types.py # Objects/unionobject.c
bpo-44676: Add ability to serialize types.Union (pythonGH-27244)
| return NULL; | ||
| } | ||
|
|
||
| return Py_BuildValue("O(O)", from_args, alias->args); |
There was a problem hiding this comment.
@pablogsal and @uriyyo the reference leak is here, we need to decref from_args. I am submitting a PR now.
There was a problem hiding this comment.
@Fidget-Spinner thanks for solving this issue
|
@uriyyo thanks again for fixing union pickling. I'm doing a manual backport without the reference leak now to keep 3.10 in sync. |
(cherry picked from commit fe13f0b) Co-authored-by: Yurii Karabas <1998uriyyo@gmail.com>
|
Nevermind it seems that Pablo got to it first :). |
|
@Fidget-Spinner I just realise that |
|
https://bugs.python.org/issue44676