Skip to content
Open
3 changes: 2 additions & 1 deletion Lib/ipaddress.py
Original file line number Diff line number Diff line change
Expand Up @@ -2239,7 +2239,8 @@ def __hash__(self):

@property
def ip(self):
return IPv6Address(self._ip)
addr_str = self._string_from_ip_int(self._ip) + (f'%{self._scope_id}' if self._scope_id else '')
return IPv6Address(addr_str)

@property
def with_prefixlen(self):
Expand Down
5 changes: 5 additions & 0 deletions Lib/test/test_ipaddress.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,11 @@ def test_copy(self):
self.assertEqual(addr, copy.copy(addr))
self.assertEqual(addr, copy.deepcopy(addr))

def test_ipv6_interface_scope_id(self):
addr = ipaddress.IPv6Address("fe80::%10")
addr_with_prefix = ipaddress.IPv6Interface((addr, 64))
self.assertEqual(addr_with_prefix.ip, ipaddress.IPv6Address("fe80::%10"))


class NetmaskTestMixin_v4(CommonTestMixin_v4):
"""Input validation on interfaces and networks is very similar"""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
This pull request addresses the issue described in `issue #129538 <https://github.com/python/cpython/issues/129538>`_, where the ``IPv6Interface.ip`` loses the scope ID information when accessing the IP address.

The problem occurs when an IPv6 address with a scope ID is converted into an ``IPv6Interface`` and then accessed via the ``ip`` property, resulting in the loss of the scope ID information. This behavior is unexpected and not documented.

Minimal Example Demonstrating the Issue::

addr = ipaddress.IPv6Address("fe80::%10")
addr_with_prefix = ipaddress.IPv6Interface((addr, 64))
print(addr_with_prefix.ip) # Expected: IPv6Address('fe80::%10'), Actual: IPv6Address('fe80::')

Changes Made:
1. Fix in ``IPv6Interface`` Class:
- Updated the ``IPv6Interface`` class to ensure the scope ID is preserved when converting the IP address.
- Modified the ``ip`` property to include the scope ID in the address string.

2. Added Test Case:
- Added a test case to verify that the scope ID is preserved when converting the IP address in the ``IPv6Interface`` class.

Tests and Documentation:
- Updated the tests in ``Lib/test/test_ipaddress.py`` to include the new test case.
- No changes to documentation as the behavior is expected and intuitive.
Loading