Skip to content

bpo-44635: Convert None to NoneType in the union type constructor #27136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 14, 2021
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
13 changes: 13 additions & 0 deletions Lib/test/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,8 @@ def test_or_types_operator(self):
self.assertEqual(str | int, typing.Union[int, str])
self.assertEqual(int | None, typing.Union[int, None])
self.assertEqual(None | int, typing.Union[int, None])
self.assertEqual(int | type(None), int | None)
self.assertEqual(type(None) | int, None | int)
self.assertEqual(int | str | list, typing.Union[int, str, list])
self.assertEqual(int | (str | list), typing.Union[int, str, list])
self.assertEqual(str | (int | list), typing.Union[int, str, list])
Expand Down Expand Up @@ -699,6 +701,13 @@ def test_or_type_operator_with_TypeVar(self):
assert TV | str == typing.Union[TV, str]
assert str | TV == typing.Union[str, TV]

def test_union_args(self):
self.assertEqual((int | str).__args__, (int, str))
self.assertEqual(((int | str) | list).__args__, (int, str, list))
self.assertEqual((int | (str | list)).__args__, (int, str, list))
self.assertEqual((int | None).__args__, (int, type(None)))
self.assertEqual((int | type(None)).__args__, (int, type(None)))

Copy link
Member

@Fidget-Spinner Fidget-Spinner Jul 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend one more simple test to test equality of two unions.

Suggested change
self.assertEqual(int | type(None), int | None)

def test_union_parameter_chaining(self):
T = typing.TypeVar("T")
S = typing.TypeVar("S")
Expand Down Expand Up @@ -754,7 +763,11 @@ def test_or_type_operator_with_SpecialForm(self):
assert typing.Union[int, bool] | str == typing.Union[int, bool, str]

def test_or_type_repr(self):
assert repr(int | str) == "int | str"
assert repr((int | str) | list) == "int | str | list"
assert repr(int | (str | list)) == "int | str | list"
assert repr(int | None) == "int | None"
assert repr(int | type(None)) == "int | None"
assert repr(int | typing.GenericAlias(list, int)) == "int | list[int]"

def test_or_type_operator_with_genericalias(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Convert ``None`` to ``type(None)`` in the union type constructor.
16 changes: 7 additions & 9 deletions Objects/unionobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,6 @@ union_instancecheck(PyObject *self, PyObject *instance)
}
for (Py_ssize_t iarg = 0; iarg < nargs; iarg++) {
PyObject *arg = PyTuple_GET_ITEM(alias->args, iarg);
if (arg == Py_None) {
arg = (PyObject *)&_PyNone_Type;
}
if (PyType_Check(arg)) {
int res = PyObject_IsInstance(instance, arg);
if (res < 0) {
Expand Down Expand Up @@ -99,9 +96,6 @@ union_subclasscheck(PyObject *self, PyObject *instance)
Py_ssize_t nargs = PyTuple_GET_SIZE(alias->args);
for (Py_ssize_t iarg = 0; iarg < nargs; iarg++) {
PyObject *arg = PyTuple_GET_ITEM(alias->args, iarg);
if (arg == Py_None) {
arg = (PyObject *)&_PyNone_Type;
}
if (PyType_Check(arg)) {
int res = PyObject_IsSubclass(instance, arg);
if (res < 0) {
Expand Down Expand Up @@ -175,9 +169,6 @@ union_richcompare(PyObject *a, PyObject *b, int op)
Py_ssize_t b_arg_length = PyTuple_GET_SIZE(b_args);
for (Py_ssize_t i = 0; i < b_arg_length; i++) {
PyObject* arg = PyTuple_GET_ITEM(b_args, i);
if (arg == (PyObject *)&_PyNone_Type) {
arg = Py_None;
}
if (PySet_Add(b_set, arg) == -1) {
Py_DECREF(b_args);
goto exit;
Expand Down Expand Up @@ -239,6 +230,9 @@ flatten_args(PyObject* args)
pos++;
}
} else {
if (arg == Py_None) {
arg = (PyObject *)&_PyNone_Type;
}
Py_INCREF(arg);
PyTuple_SET_ITEM(flattened_args, pos, arg);
pos++;
Expand Down Expand Up @@ -365,6 +359,10 @@ union_repr_item(_PyUnicodeWriter *writer, PyObject *p)
PyObject *r = NULL;
int err;

if (p == (PyObject *)&_PyNone_Type) {
return _PyUnicodeWriter_WriteASCIIString(writer, "None", 4);
}

if (_PyObject_LookupAttrId(p, &PyId___origin__, &tmp) < 0) {
goto exit;
}
Expand Down