Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Remove types.Union serialezation
  • Loading branch information
uriyyo committed Jul 7, 2021
commit d699211892947f6b769681a94790bf984d913bf3
11 changes: 0 additions & 11 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3001,17 +3001,6 @@ def barfoo3(x: BA2): ...
BA2
)

BA3 = typing.Annotated[int | float, "const"]
def barfoo4(x: BA3): ...
self.assertEqual(
get_type_hints(barfoo4, globals(), locals()),
{"x": int | float}
)
self.assertEqual(
get_type_hints(barfoo4, globals(), locals(), include_extras=True),
{"x": typing.Annotated[int | float, "const"]}
)

def test_get_type_hints_annotated_refs(self):

Const = Annotated[T, "Const"]
Expand Down
2 changes: 1 addition & 1 deletion Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ def _eval_type(t, globalns, localns, recursive_guard=frozenset()):
if isinstance(t, GenericAlias):
return GenericAlias(t.__origin__, ev_args)
if isinstance(t, types.Union):
return types.Union(ev_args)
return functools.reduce(operator.or_, ev_args)
else:
return t.copy_with(ev_args)
return t
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
Add ability to serialize ``types.Union``. Fix ``typing`` module issues with
``types.Union`` parameters and arguments resolving. Patch provided by Yurii
Karabas.
Fix ``typing`` module issues with ``types.Union`` parameters and arguments
resolving. Patch provided by Yurii Karabas.
47 changes: 1 addition & 46 deletions Objects/unionobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -428,13 +428,6 @@ union_repr(PyObject *self)
return NULL;
}

static PyObject *
union_reduce(PyObject *self, PyObject *Py_UNUSED(ignored))
{
unionobject *alias = (unionobject *)self;
return Py_BuildValue("O(O)", Py_TYPE(alias), alias->args);
}

static PyMemberDef union_members[] = {
{"__args__", T_OBJECT, offsetof(unionobject, args), READONLY},
{0}
Expand All @@ -443,7 +436,6 @@ static PyMemberDef union_members[] = {
static PyMethodDef union_methods[] = {
{"__instancecheck__", union_instancecheck, METH_O},
{"__subclasscheck__", union_subclasscheck, METH_O},
{"__reduce__", union_reduce, METH_NOARGS},
{0}};


Expand Down Expand Up @@ -497,42 +489,6 @@ static PyNumberMethods union_as_number = {
.nb_or = _Py_union_type_or, // Add __or__ function
};

static PyObject *
union_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
if (!_PyArg_NoKeywords("Union", kwds)) {
return NULL;
}
if (!_PyArg_CheckPositional("Union", PyTuple_GET_SIZE(args), 1, 1)) {
return NULL;
}

return _Py_Union(PyTuple_GET_ITEM(args, 0));
}

static const char* const cls_attrs[] = {
"__name__",
"__qualname__",
"__module__",
NULL,
};

static PyObject *
union_getattro(PyObject *self, PyObject *name)
{
unionobject *alias = (unionobject *)self;

for (const char * const *p = cls_attrs; ; p++) {
if (*p == NULL) {
break;
}
if (_PyUnicode_EqualToASCIIString(name, *p)) {
return PyObject_GetAttr((PyObject *) Py_TYPE(alias), name);
}
}
return PyObject_GenericGetAttr(self, name);
}

PyTypeObject _Py_UnionType = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
.tp_name = "types.Union",
Expand All @@ -546,15 +502,14 @@ PyTypeObject _Py_UnionType = {
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
.tp_traverse = union_traverse,
.tp_hash = union_hash,
.tp_getattro = union_getattro,
.tp_getattro = PyObject_GenericGetAttr,
.tp_members = union_members,
.tp_methods = union_methods,
.tp_richcompare = union_richcompare,
.tp_as_mapping = &union_as_mapping,
.tp_as_number = &union_as_number,
.tp_repr = union_repr,
.tp_getset = union_properties,
.tp_new = union_new,
};

PyObject *
Expand Down