Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
8c8b1dd
Unify UnionType and Union
JelleZijlstra Jun 8, 2023
aba63eb
test_typing succeeds
JelleZijlstra Jun 8, 2023
f2f23a0
Merge remote-tracking branch 'upstream/main' into unifyunion
JelleZijlstra Jun 8, 2023
e75282f
blurb
JelleZijlstra Jun 8, 2023
540b04b
Fix test_types
JelleZijlstra Jun 8, 2023
ae8fa62
Documentation
JelleZijlstra Jun 8, 2023
7569c48
stray f
JelleZijlstra Jun 8, 2023
8bcb930
Fix tests
JelleZijlstra Jun 8, 2023
291953e
No more typing._make_union
JelleZijlstra Jun 8, 2023
f7ca8d4
Add tp_new
JelleZijlstra Jun 9, 2023
0f1ab18
fix a refleak
JelleZijlstra Jun 9, 2023
eb47a0b
Fix test
JelleZijlstra Jun 9, 2023
eaa4e79
Update Lib/test/test_typing.py
JelleZijlstra Jun 9, 2023
4a0235f
Merge remote-tracking branch 'upstream/main' into unifyunion
JelleZijlstra Jun 13, 2023
c71e8c3
Add back _UnionGenericAlias
JelleZijlstra Jun 13, 2023
0475415
Merge remote-tracking branch 'upstream/main' into unifyunion
JelleZijlstra Jun 16, 2023
bb50899
Remove unnecessary NewRef
JelleZijlstra Jun 16, 2023
c564672
Make typing.Union the canonical name
JelleZijlstra Jun 16, 2023
9f58421
docs
JelleZijlstra Jun 16, 2023
8884a94
fix test_pydoc
JelleZijlstra Jun 16, 2023
bcc2e6a
Update Objects/unionobject.c
JelleZijlstra Jun 16, 2023
c4b217b
Update Doc/library/typing.rst
JelleZijlstra Jun 17, 2023
5eb2a0c
Update Doc/library/stdtypes.rst
JelleZijlstra Jun 17, 2023
64ac293
Update Doc/library/stdtypes.rst
JelleZijlstra Jun 17, 2023
0ba8551
Merge remote-tracking branch 'upstream/main' into unifyunion
JelleZijlstra Jun 17, 2023
aacf2b0
Add __mro_entries__
JelleZijlstra Jun 17, 2023
f46c0c6
Improve docs, expose it in _typing
JelleZijlstra Jun 17, 2023
8c04441
Update Doc/library/functools.rst
JelleZijlstra Jun 20, 2023
04df4d0
Merge branch 'main' into unifyunion
JelleZijlstra Jun 20, 2023
c363de7
Merge remote-tracking branch 'upstream/main' into unifyunion
JelleZijlstra Oct 11, 2023
98b634a
Post-merge cleanup
JelleZijlstra Oct 11, 2023
f2f961b
No need for tp_new
JelleZijlstra Oct 12, 2023
932f8e5
Merge branch 'main' into unifyunion
JelleZijlstra Oct 28, 2023
c3edd87
Merge remote-tracking branch 'upstream/main' into unifyunion
JelleZijlstra Feb 18, 2024
9ae102d
Add back doctest
JelleZijlstra Feb 18, 2024
e0924c6
Merge branch 'main' into unifyunion
JelleZijlstra Feb 19, 2024
7086879
Merge branch 'main' into unifyunion
JelleZijlstra Feb 28, 2024
b0e057f
Merge branch 'main' into unifyunion
JelleZijlstra Mar 12, 2024
55d0c97
Merge branch 'main' into unifyunion
JelleZijlstra Apr 26, 2024
8a80fe0
Fix two issues
JelleZijlstra Apr 26, 2024
f910e88
in progress: hashable unions
JelleZijlstra Apr 26, 2024
cae36c7
Merge remote-tracking branch 'upstream/main' into unifyunion
JelleZijlstra Sep 28, 2024
65da3f1
fixup
JelleZijlstra Sep 28, 2024
f500f5f
Make union support unhashable objects
JelleZijlstra Sep 28, 2024
5f9e599
simplify, extend docs
JelleZijlstra Sep 28, 2024
9927b38
fix more tests
JelleZijlstra Sep 28, 2024
eea6eca
another
JelleZijlstra Sep 28, 2024
0785951
change hash
JelleZijlstra Sep 29, 2024
1a39ded
Merge remote-tracking branch 'upstream/main' into unifyunion
JelleZijlstra Sep 29, 2024
eddbfde
Merge branch 'main' into unifyunion
JelleZijlstra Nov 5, 2024
3ec9271
Merge branch 'main' into unifyunion
JelleZijlstra Mar 3, 2025
cab69f0
tweak docs
JelleZijlstra Mar 3, 2025
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
Prev Previous commit
Next Next commit
Add tp_new
  • Loading branch information
JelleZijlstra committed Jun 9, 2023
commit f7ca8d443253dba858cafe5d53ac04c91c61d036
2 changes: 1 addition & 1 deletion Include/internal/pycore_unionobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ extern PyObject *_Py_union_type_or(PyObject *, PyObject *);
extern PyObject *_Py_subs_parameters(PyObject *, PyObject *, PyObject *, PyObject *);
extern PyObject *_Py_make_parameters(PyObject *);
extern PyObject *_Py_union_args(PyObject *self);
extern PyObject *_Py_union_class_getitem(PyObject *cls, PyObject *args);
extern PyObject *_Py_union_from_tuple(PyObject *args);

#ifdef __cplusplus
}
Expand Down
8 changes: 8 additions & 0 deletions Lib/test/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -1025,6 +1025,14 @@ def test_or_type_operator_reference_cycle(self):
self.assertLessEqual(sys.gettotalrefcount() - before, leeway,
msg='Check for union reference leak.')

def test_instantiation(self):
with self.assertRaises(TypeError):
types.UnionType()
self.assertIs(int, types.UnionType(int))
self.assertIs(int, types.UnionType(int, int))
self.assertEqual(int | str, types.UnionType(int, str))
self.assertEqual(int | typing.ForwardRef("str"), types.UnionType(int, "str"))


class MappingProxyTests(unittest.TestCase):
mappingproxy = types.MappingProxyType
Expand Down
4 changes: 2 additions & 2 deletions Objects/typevarobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "Python.h"
#include "pycore_object.h" // _PyObject_GC_TRACK/UNTRACK
#include "pycore_typevarobject.h"
#include "pycore_unionobject.h" // _Py_union_type_or, _Py_union_class_getitem
#include "pycore_unionobject.h" // _Py_union_type_or, _Py_union_from_tuple
#include "structmember.h"

/*[clinic input]
Expand Down Expand Up @@ -103,7 +103,7 @@ make_union(PyObject *self, PyObject *other)
if (args == NULL) {
return NULL;
}
return _Py_union_class_getitem((PyObject *)&_PyUnion_Type, args);
return _Py_union_from_tuple(args);
}

static PyObject *
Expand Down
20 changes: 18 additions & 2 deletions Objects/unionobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,15 @@ _Py_union_args(PyObject *self)
return ((unionobject *) self)->args;
}

static PyObject *
union_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
if (!_PyArg_NoKeywords("UnionType", kwds)) {
return NULL;
}
return _Py_union_from_tuple(args);
}

static PyObject *
call_typing_func_object(const char *name, PyObject **args, size_t nargs)
Comment thread
carljm marked this conversation as resolved.
{
Expand Down Expand Up @@ -461,7 +470,7 @@ add_object_to_union_args(PyObject *args_list, PyObject *args_set, PyObject *obj)
}

PyObject *
_Py_union_class_getitem(PyObject *cls, PyObject *args)
_Py_union_from_tuple(PyObject *args)
{
PyObject *args_list = PyList_New(0);
if (args_list == NULL) {
Expand Down Expand Up @@ -510,8 +519,14 @@ _Py_union_class_getitem(PyObject *cls, PyObject *args)
return make_union(args_tuple);
}

static PyObject *
union_class_getitem(PyObject *cls, PyObject *args)
{
return _Py_union_from_tuple(args);
}

static PyMethodDef union_methods[] = {
{"__class_getitem__", _Py_union_class_getitem, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
{"__class_getitem__", union_class_getitem, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
{0}
};

Expand All @@ -531,6 +546,7 @@ PyTypeObject _PyUnion_Type = {
.tp_getattro = union_getattro,
.tp_members = union_members,
.tp_methods = union_methods,
.tp_new = union_new,
.tp_richcompare = union_richcompare,
.tp_as_mapping = &union_as_mapping,
.tp_as_number = &union_as_number,
Expand Down