From 669a2074ed08fa1963d110c070c6ffedb7e7c3c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Gmach?= Date: Fri, 13 Nov 2020 16:02:03 +0100 Subject: [PATCH 1/4] bpo-38914: Improve pseudo implementation for SimpleNamespace Wheras `SimpleNamespace` is implemented in C, the documentation shows a pseudo implementation in Python for easier understanding. The magic method for the comparison did not take into account that the actual C implementation also compares the types. `__eq__` of the Python pseudo implementation now also compares the types. --- Doc/library/types.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/types.rst b/Doc/library/types.rst index ad40a9fbf32739..4c60bd4343933d 100644 --- a/Doc/library/types.rst +++ b/Doc/library/types.rst @@ -409,7 +409,7 @@ Additional Utility Classes and Functions return "{}({})".format(type(self).__name__, ", ".join(items)) def __eq__(self, other): - return self.__dict__ == other.__dict__ + return self.__dict__ == other.__dict__ and type(self) == type(other) ``SimpleNamespace`` may be useful as a replacement for ``class NS: pass``. However, for a structured record type use :func:`~collections.namedtuple` From 0c03224baa754ee2ffe1fdfc2838f95247d62719 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Gmach?= Date: Fri, 13 Nov 2020 17:06:19 +0100 Subject: [PATCH 2/4] Apply suggested changes modified: library/types.rst --- Doc/library/types.rst | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Doc/library/types.rst b/Doc/library/types.rst index 4c60bd4343933d..02a06c7848aac4 100644 --- a/Doc/library/types.rst +++ b/Doc/library/types.rst @@ -409,7 +409,10 @@ Additional Utility Classes and Functions return "{}({})".format(type(self).__name__, ", ".join(items)) def __eq__(self, other): - return self.__dict__ == other.__dict__ and type(self) == type(other) + return ( + isinstance(self, SimpleNamespace) == isinstance(other, SimpleNamespace) + and self.__dict__ == other.__dict__ + ) ``SimpleNamespace`` may be useful as a replacement for ``class NS: pass``. However, for a structured record type use :func:`~collections.namedtuple` From c07831dbe92d611f3dc7010454885c2de4523a5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Gmach?= Date: Fri, 13 Nov 2020 18:43:50 +0100 Subject: [PATCH 3/4] Apply requested changes modified: library/types.rst --- Doc/library/types.rst | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Doc/library/types.rst b/Doc/library/types.rst index 02a06c7848aac4..e7287f82d524a4 100644 --- a/Doc/library/types.rst +++ b/Doc/library/types.rst @@ -409,10 +409,9 @@ Additional Utility Classes and Functions return "{}({})".format(type(self).__name__, ", ".join(items)) def __eq__(self, other): - return ( - isinstance(self, SimpleNamespace) == isinstance(other, SimpleNamespace) - and self.__dict__ == other.__dict__ - ) + if isinstance(other, SimpleNamespace): + return self.__dict__ == other.__dict__ + return NotImplemented ``SimpleNamespace`` may be useful as a replacement for ``class NS: pass``. However, for a structured record type use :func:`~collections.namedtuple` From bf765fc1959aea9ccd4cce7ab0e5c510ba995f08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Gmach?= Date: Fri, 13 Nov 2020 19:11:48 +0100 Subject: [PATCH 4/4] Apply requested changes --- Doc/library/types.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/types.rst b/Doc/library/types.rst index e7287f82d524a4..6f0dc259303fad 100644 --- a/Doc/library/types.rst +++ b/Doc/library/types.rst @@ -409,7 +409,7 @@ Additional Utility Classes and Functions return "{}({})".format(type(self).__name__, ", ".join(items)) def __eq__(self, other): - if isinstance(other, SimpleNamespace): + if isinstance(self, SimpleNamespace) and isinstance(other, SimpleNamespace): return self.__dict__ == other.__dict__ return NotImplemented