Skip to content

ipaddress.IPv6Interface.with_prefixlen drops IPv6 scope IDs #158725

Description

@augusto-rehfeldt

Bug report

Bug description:

Documented behaviour: The IPv6Interface documentation inherits the IPv4Interface attribute with_prefixlen: "A string representation of the interface with the mask in prefix notation." The IPv6Address documentation permits a scope zone ID appended with '%'.

Expected: with_prefixlen returns 'fe80::1%eth/0', preserving scope 'eth' and prefix length 0 on reconstruction.

Actual: with_prefixlen returns 'fe80::1/0'; reconstruction retains prefix length 0 but loses the scope ID.

import ipaddress as ip

s = 'fe80::1%eth/0'
address, prefix = s.split('/')
base, zone = address.split('%')
p = int(prefix)
try:
    if not (zone.isascii() and zone.isalpha() and 0 <= p <= 128):
        raise ValueError('input outside documented domain')
    i = ip.IPv6Interface(s)
    expected = (int(ip.IPv6Address(base)), zone, p)
    if (int(i), i.scope_id, i.network.prefixlen) != expected:
        raise ValueError('constructor did not retain the valid input')
except ValueError as e:
    print('REFUTATION REJECTED:', str(e))
else:
    text = i.with_prefixlen
    try:
        j = ip.IPv6Interface(text)
        actual = (int(j), j.scope_id, j.network.prefixlen)
    except ValueError as e:
        actual = str(e)
    if actual != expected:
        print('REFUTATION CONFIRMED:', s, 'actual:', (text, actual),
              'expected:', expected)
    else:
        print('REFUTATION REJECTED:', 'scope and prefix survived roundtrip')

Output on Python 3.14.6 (Windows-11-10.0.26220-SP0), standard library ipaddress:

REFUTATION CONFIRMED: fe80::1%eth/0 actual: ('fe80::1/0', (338288524927261089654018896841347694593, None, 0)) expected: (338288524927261089654018896841347694593, 'eth', 0)

This report was found and written by an automated property-testing tool I run (bugforge). The reproducer above was executed and its output is pasted unedited; no person reviewed the report before it was filed. The search script is in https://github.com/augusto-rehfeldt/bugforge-results/tree/main/ipaddress-20261003-155745-c2

CPython versions tested on:

3.14

Operating systems tested on:

Windows

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions