Skip to content

gh-153769: Clarify the TypeError from ipaddress subnet_of()/supernet_of() - #154147

Closed
fedonman wants to merge 2 commits into
python:mainfrom
fedonman:fix-gh-153769-ipaddress-subnet-of-typeerror
Closed

gh-153769: Clarify the TypeError from ipaddress subnet_of()/supernet_of()#154147
fedonman wants to merge 2 commits into
python:mainfrom
fedonman:fix-gh-153769-ipaddress-subnet-of-typeerror

Conversation

@fedonman

@fedonman fedonman commented Jul 19, 2026

Copy link
Copy Markdown
Contributor

subnet_of() and supernet_of() reported a non-network argument as "Unable to test subnet
containment between ::/0 and ::1", with an AttributeError about network_address shown
above it. The message now says what is actually wrong, and the internal error is no longer
displayed:

TypeError: Unable to test subnet containment between ::/0 and ::1: both must be network objects

The exception type does not change, and the handler stays. An isinstance(_BaseNetwork)
check would also reject a network-like object that is not a subclass of it, which works
today:

class DuckNetwork:
    version = 4
    network_address = ipaddress.IPv4Address('10.0.0.0')
    broadcast_address = ipaddress.IPv4Address('10.255.255.255')

ipaddress.IPv4Network('10.1.0.0/16').subnet_of(DuckNetwork())   # True on 3.14 and on main

Nothing documents other as having to be a network instance, so that stays working here.

test_ipaddress passes, 216 tests.

…ernet_of()

Passing something that is not a network object, such as an address, to
IPv4Network.subnet_of() or supernet_of() made _is_subnet_of() reach for
the network_address attribute, fail with an AttributeError, and turn that
into a TypeError with a message that read like a real containment result
and hid the actual cause.

Check that both arguments are network objects up front and raise a clear
TypeError explaining what is required, so nothing masks an internal
AttributeError anymore.
@python-cla-bot

python-cla-bot Bot commented Jul 19, 2026

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

An isinstance() check on _BaseNetwork rejects a network-like object that is
not a subclass, which works today. Keeping the handler and suppressing the
context keeps that working and still gives the caller one clear error.
@picnixz picnixz closed this Aug 5, 2026
@picnixz

picnixz commented Aug 5, 2026

Copy link
Copy Markdown
Member

As I said, this is a premature PR. Avoid using LLMs just for getting quick contributions, especially when the issue is still under discussion.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants