From 913c8c7cacb84a814942f7d5f4385f5de48fda6e Mon Sep 17 00:00:00 2001 From: yihong0618 Date: Mon, 17 Nov 2025 17:08:31 +0800 Subject: [PATCH 1/5] fix: attribute breaks __eq__ behaviour of IPv4Network and IPv6Network Signed-off-by: yihong0618 --- Lib/ipaddress.py | 16 ++++----- Lib/test/test_ipaddress.py | 35 +++++++++++++++++++ ...-11-17-17-06-56.gh-issue-131647.tyafol.rst | 4 +++ 3 files changed, 46 insertions(+), 9 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2025-11-17-17-06-56.gh-issue-131647.tyafol.rst diff --git a/Lib/ipaddress.py b/Lib/ipaddress.py index aa0cf4a0620cd0..dd97c590a766f6 100644 --- a/Lib/ipaddress.py +++ b/Lib/ipaddress.py @@ -567,11 +567,10 @@ def __int__(self): return self._ip def __eq__(self, other): - try: - return (self._ip == other._ip - and self.version == other.version) - except AttributeError: + if not isinstance(other, _BaseAddress): return NotImplemented + return (self._ip == other._ip + and self.version == other.version) def __lt__(self, other): if not isinstance(other, _BaseAddress): @@ -718,12 +717,11 @@ def __lt__(self, other): return False def __eq__(self, other): - try: - return (self.version == other.version and - self.network_address == other.network_address and - int(self.netmask) == int(other.netmask)) - except AttributeError: + if not isinstance(other, _BaseNetwork): return NotImplemented + return (self.version == other.version and + self.network_address == other.network_address and + int(self.netmask) == int(other.netmask)) def __hash__(self): return hash((int(self.network_address), int(self.netmask))) diff --git a/Lib/test/test_ipaddress.py b/Lib/test/test_ipaddress.py index 11721a59972672..ccbad65330af91 100644 --- a/Lib/test/test_ipaddress.py +++ b/Lib/test/test_ipaddress.py @@ -1078,6 +1078,41 @@ def test_incompatible_versions(self): self.assertRaises(TypeError, v6net_scoped.__lt__, v4net) self.assertRaises(TypeError, v6net_scoped.__gt__, v4net) + def test_network_eq_with_version_attribute(self): + class AlwaysTrue: + version = 42 + def __eq__(self, other): + return True + + always_true = AlwaysTrue() + self.assertTrue(ipaddress.IPv4Network('43.48.0.0/12') == always_true) + self.assertTrue(ipaddress.IPv6Network('::eeff:ae3f:d473/128') == always_true) + + def test_address_eq_with_version_attribute(self): + class AlwaysTrue: + def __init__(self, ip_value): + self._ip = ip_value + self.version = 42 + + def __eq__(self, other): + return True + + addr4 = ipaddress.IPv4Address('192.168.1.1') + always_true4 = AlwaysTrue(addr4._ip) + self.assertTrue(addr4 == always_true4) + + addr6 = ipaddress.IPv6Address('::1') + always_true6 = AlwaysTrue(addr6._ip) + self.assertTrue(addr6 == always_true6) + + intf4 = ipaddress.IPv4Interface('192.168.1.1/24') + always_true_intf4 = AlwaysTrue(intf4._ip) + self.assertTrue(intf4 == always_true_intf4) + + intf6 = ipaddress.IPv6Interface('::1/128') + always_true_intf6 = AlwaysTrue(intf6._ip) + self.assertTrue(intf6 == always_true_intf6) + class IpaddrUnitTest(unittest.TestCase): diff --git a/Misc/NEWS.d/next/Library/2025-11-17-17-06-56.gh-issue-131647.tyafol.rst b/Misc/NEWS.d/next/Library/2025-11-17-17-06-56.gh-issue-131647.tyafol.rst new file mode 100644 index 00000000000000..11646aae93692f --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-11-17-17-06-56.gh-issue-131647.tyafol.rst @@ -0,0 +1,4 @@ +Fix :meth:`~ipaddress._BaseAddress.__eq__` and +:meth:`~ipaddress._BaseNetwork.__eq__` to return ``NotImplemented`` instead +of ``False`` when comparing with objects that have matching attribute names +but are not actual address or network objects. From fb9c0da4318fde1bfb7616d96ae5883fd00b9964 Mon Sep 17 00:00:00 2001 From: yihong0618 Date: Mon, 17 Nov 2025 17:25:41 +0800 Subject: [PATCH 2/5] fix: doc ci warnings Signed-off-by: yihong0618 --- .../2025-11-17-17-06-56.gh-issue-131647.tyafol.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Misc/NEWS.d/next/Library/2025-11-17-17-06-56.gh-issue-131647.tyafol.rst b/Misc/NEWS.d/next/Library/2025-11-17-17-06-56.gh-issue-131647.tyafol.rst index 11646aae93692f..67b5dd54743ae2 100644 --- a/Misc/NEWS.d/next/Library/2025-11-17-17-06-56.gh-issue-131647.tyafol.rst +++ b/Misc/NEWS.d/next/Library/2025-11-17-17-06-56.gh-issue-131647.tyafol.rst @@ -1,4 +1,4 @@ -Fix :meth:`~ipaddress._BaseAddress.__eq__` and -:meth:`~ipaddress._BaseNetwork.__eq__` to return ``NotImplemented`` instead -of ``False`` when comparing with objects that have matching attribute names -but are not actual address or network objects. +Fix :mod:`ipaddress` address and network objects' ``__eq__`` methods to +return ``NotImplemented`` instead of ``False`` when comparing with objects +that have matching attribute names but are not actual address or network +objects. From beb5629aebae54e7693e7372d44ccf4fe9f3724e Mon Sep 17 00:00:00 2001 From: yihong Date: Sat, 22 Nov 2025 18:21:18 +0800 Subject: [PATCH 3/5] Update Lib/ipaddress.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- Lib/ipaddress.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Lib/ipaddress.py b/Lib/ipaddress.py index dd97c590a766f6..b4408373e51d2c 100644 --- a/Lib/ipaddress.py +++ b/Lib/ipaddress.py @@ -719,9 +719,9 @@ def __lt__(self, other): def __eq__(self, other): if not isinstance(other, _BaseNetwork): return NotImplemented - return (self.version == other.version and - self.network_address == other.network_address and - int(self.netmask) == int(other.netmask)) + return (self.version == other.version + and self.network_address == other.network_address + and int(self.netmask) == int(other.netmask)) def __hash__(self): return hash((int(self.network_address), int(self.netmask))) From 00efd0fb050b815bf3a1aee2e361711fa800ba3e Mon Sep 17 00:00:00 2001 From: yihong Date: Sat, 22 Nov 2025 18:29:57 +0800 Subject: [PATCH 4/5] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- Lib/test/test_ipaddress.py | 16 ++++++++-------- ...025-11-17-17-06-56.gh-issue-131647.tyafol.rst | 8 ++++---- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/Lib/test/test_ipaddress.py b/Lib/test/test_ipaddress.py index ccbad65330af91..562a3771c453b4 100644 --- a/Lib/test/test_ipaddress.py +++ b/Lib/test/test_ipaddress.py @@ -1078,17 +1078,17 @@ def test_incompatible_versions(self): self.assertRaises(TypeError, v6net_scoped.__lt__, v4net) self.assertRaises(TypeError, v6net_scoped.__gt__, v4net) - def test_network_eq_with_version_attribute(self): + def test_network_eq_with_faux_object(self): class AlwaysTrue: version = 42 def __eq__(self, other): return True always_true = AlwaysTrue() - self.assertTrue(ipaddress.IPv4Network('43.48.0.0/12') == always_true) - self.assertTrue(ipaddress.IPv6Network('::eeff:ae3f:d473/128') == always_true) + self.assertEqual(ipaddress.IPv4Network('43.48.0.0/12'), always_true) + self.assertEqual(ipaddress.IPv6Network('::eeff:ae3f:d473/128'), always_true) - def test_address_eq_with_version_attribute(self): + def test_address_eq_with_faux_object(self): class AlwaysTrue: def __init__(self, ip_value): self._ip = ip_value @@ -1099,19 +1099,19 @@ def __eq__(self, other): addr4 = ipaddress.IPv4Address('192.168.1.1') always_true4 = AlwaysTrue(addr4._ip) - self.assertTrue(addr4 == always_true4) + self.assertEqual(addr4, always_true4) addr6 = ipaddress.IPv6Address('::1') always_true6 = AlwaysTrue(addr6._ip) - self.assertTrue(addr6 == always_true6) + self.assertEqual(addr6, always_true6) intf4 = ipaddress.IPv4Interface('192.168.1.1/24') always_true_intf4 = AlwaysTrue(intf4._ip) - self.assertTrue(intf4 == always_true_intf4) + self.assertEqual(intf4, always_true_intf4) intf6 = ipaddress.IPv6Interface('::1/128') always_true_intf6 = AlwaysTrue(intf6._ip) - self.assertTrue(intf6 == always_true_intf6) + self.assertEqual(intf6, always_true_intf6) class IpaddrUnitTest(unittest.TestCase): diff --git a/Misc/NEWS.d/next/Library/2025-11-17-17-06-56.gh-issue-131647.tyafol.rst b/Misc/NEWS.d/next/Library/2025-11-17-17-06-56.gh-issue-131647.tyafol.rst index 67b5dd54743ae2..7978b70e186c18 100644 --- a/Misc/NEWS.d/next/Library/2025-11-17-17-06-56.gh-issue-131647.tyafol.rst +++ b/Misc/NEWS.d/next/Library/2025-11-17-17-06-56.gh-issue-131647.tyafol.rst @@ -1,4 +1,4 @@ -Fix :mod:`ipaddress` address and network objects' ``__eq__`` methods to -return ``NotImplemented`` instead of ``False`` when comparing with objects -that have matching attribute names but are not actual address or network -objects. +:mod:`ipaddress`: fix equality testing for :class:`~ipaddress.IPv4Address`, +:class:`~ipaddress.IPv6Address`, :class:`~ipaddress.IPv4Network`, and +:class:`~ipaddress.IPv6Network` to avoid comparing instances of incorrect +types. From 5977801ce2712260dc9f691e59130de1e9fe7238 Mon Sep 17 00:00:00 2001 From: yihong0618 Date: Sat, 22 Nov 2025 18:37:58 +0800 Subject: [PATCH 5/5] fix: apply suggestions change test order Signed-off-by: yihong0618 --- Lib/test/test_ipaddress.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/Lib/test/test_ipaddress.py b/Lib/test/test_ipaddress.py index 562a3771c453b4..dcfa66dc9fbbc3 100644 --- a/Lib/test/test_ipaddress.py +++ b/Lib/test/test_ipaddress.py @@ -1078,16 +1078,6 @@ def test_incompatible_versions(self): self.assertRaises(TypeError, v6net_scoped.__lt__, v4net) self.assertRaises(TypeError, v6net_scoped.__gt__, v4net) - def test_network_eq_with_faux_object(self): - class AlwaysTrue: - version = 42 - def __eq__(self, other): - return True - - always_true = AlwaysTrue() - self.assertEqual(ipaddress.IPv4Network('43.48.0.0/12'), always_true) - self.assertEqual(ipaddress.IPv6Network('::eeff:ae3f:d473/128'), always_true) - def test_address_eq_with_faux_object(self): class AlwaysTrue: def __init__(self, ip_value): @@ -1113,6 +1103,16 @@ def __eq__(self, other): always_true_intf6 = AlwaysTrue(intf6._ip) self.assertEqual(intf6, always_true_intf6) + def test_network_eq_with_faux_object(self): + class AlwaysTrue: + version = 42 + def __eq__(self, other): + return True + + always_true = AlwaysTrue() + self.assertEqual(ipaddress.IPv4Network('43.48.0.0/12'), always_true) + self.assertEqual(ipaddress.IPv6Network('::eeff:ae3f:d473/128'), always_true) + class IpaddrUnitTest(unittest.TestCase):