Skip to content

IPv4Interface.is_loopback ignores the network prefix, unlike IPv6Interface #151785

@aisk

Description

@aisk

Bug report

Bug description:

IPv4Interface does not override is_loopback, so it inherits IPv4Address.is_loopback and only checks the host address, ignoring the network. IPv6Interface, on the other hand, checks both the address and the network:

>>> import ipaddress
>>> ipaddress.IPv4Interface('127.0.0.1/4').is_loopback
True
>>> ipaddress.IPv4Interface('127.0.0.1/4').network.is_loopback
False
>>> ipaddress.IPv6Interface('::1/64').is_loopback
False

127.0.0.1/4 has a network of 112.0.0.0/4, which is not within the loopback range 127.0.0.0/8, yet is_loopback returns True.

This is the same class of inconsistency already fixed for is_unspecified in gh-115766: an interface property should consider both the address and its network. IPv6Interface.is_loopback already does this:

@property
def is_loopback(self):
    return super().is_loopback and self.network.is_loopback

IPv4Interface should get the same override for consistency.

Note this is a behavior change (127.0.0.1/4 etc. would change from True to False).

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error
    No fields configured for issues without a type.

    Projects

    Status
    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions