Skip to content

Commit

Permalink
Tune the partial network expansion flag
Browse files Browse the repository at this point in the history
The flag has been introduced in [1].

I think it improves things a little for the flag's name to be more
verbose and to require passing it via keyword arguments (otherwise it's
just True or False which is gonna be quite opaque to the reader).

[1] d2f5d05 ("IPNetwork: Allow partial addresses (again) (#377)")
  • Loading branch information
jstasiak committed May 27, 2024
1 parent c20b9c5 commit 88b9455
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
16 changes: 9 additions & 7 deletions netaddr/ip/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,7 @@ def __bool__(self):
return True


def parse_ip_network(module, addr, flags=0, expand=False):
def parse_ip_network(module, addr, flags=0, *, expand_partial=False):
if isinstance(addr, tuple):
# CIDR integer tuple
if len(addr) != 2:
Expand All @@ -896,7 +896,7 @@ def parse_ip_network(module, addr, flags=0, expand=False):
val1 = addr
val2 = None

if expand:
if expand_partial:
val1 = module.expand_partial_address(val1)

ip = IPAddress(val1, module.version, flags=INET_PTON)
Expand Down Expand Up @@ -972,7 +972,7 @@ class IPNetwork(BaseIP, IPListMixin):

__slots__ = ('_prefixlen',)

def __init__(self, addr, version=None, flags=0, expand=False):
def __init__(self, addr, version=None, flags=0, *, expand_partial=False):
"""
Constructor.
Expand All @@ -990,14 +990,14 @@ def __init__(self, addr, version=None, flags=0, expand=False):
interpretation of the addr value. Currently only supports the
:data:`NOHOST` option.
:param expand: (optional) decides whether partial address is
:param expand_partial: (optional) decides whether partial address is
expanded. Currently this is only effective for IPv4 address.
>>> IPNetwork('1.2.3.4/24')
IPNetwork('1.2.3.4/24')
>>> IPNetwork('1.2.3.4/24', flags=NOHOST)
IPNetwork('1.2.3.0/24')
>>> IPNetwork('10/24', expand=True)
>>> IPNetwork('10/24', expand_partial=True)
IPNetwork('10.0.0.0/24')
"""
super(IPNetwork, self).__init__()
Expand All @@ -1021,7 +1021,7 @@ def __init__(self, addr, version=None, flags=0, expand=False):
module = addr._module
prefixlen = module.width
elif version == 4:
value, prefixlen = parse_ip_network(_ipv4, addr, flags, expand)
value, prefixlen = parse_ip_network(_ipv4, addr, flags, expand_partial=expand_partial)
module = _ipv4
elif version == 6:
value, prefixlen = parse_ip_network(_ipv6, addr, flags)
Expand All @@ -1031,7 +1031,9 @@ def __init__(self, addr, version=None, flags=0, expand=False):
raise ValueError('%r is an invalid IP version!' % version)
try:
module = _ipv4
value, prefixlen = parse_ip_network(module, addr, flags, expand)
value, prefixlen = parse_ip_network(
module, addr, flags, expand_partial=expand_partial
)
except AddrFormatError:
try:
module = _ipv6
Expand Down
8 changes: 4 additions & 4 deletions netaddr/tests/ip/test_ip_v4.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,12 +355,12 @@ def test_ipnetwork_bad_string_constructor():
IPNetwork('foo')


def test_ipnetwork_expand_v4():
def test_ipnetwork_expand_partial_v4():
with pytest.raises(AddrFormatError):
IPNetwork('10/8')
assert IPNetwork('10/8', expand=True) == IPNetwork('10.0.0.0/8')
assert IPNetwork('10.20/16', expand=True) == IPNetwork('10.20.0.0/16')
assert IPNetwork('10.20.30/24', expand=True) == IPNetwork('10.20.30.0/24')
assert IPNetwork('10/8', expand_partial=True) == IPNetwork('10.0.0.0/8')
assert IPNetwork('10.20/16', expand_partial=True) == IPNetwork('10.20.0.0/16')
assert IPNetwork('10.20.30/24', expand_partial=True) == IPNetwork('10.20.30.0/24')


def test_ipaddress_netmask_v4():
Expand Down

0 comments on commit 88b9455

Please sign in to comment.