Skip to content

Commit 88b9455

Browse files
committed
Tune the partial network expansion flag
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)")
1 parent c20b9c5 commit 88b9455

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

netaddr/ip/__init__.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -876,7 +876,7 @@ def __bool__(self):
876876
return True
877877

878878

879-
def parse_ip_network(module, addr, flags=0, expand=False):
879+
def parse_ip_network(module, addr, flags=0, *, expand_partial=False):
880880
if isinstance(addr, tuple):
881881
# CIDR integer tuple
882882
if len(addr) != 2:
@@ -896,7 +896,7 @@ def parse_ip_network(module, addr, flags=0, expand=False):
896896
val1 = addr
897897
val2 = None
898898

899-
if expand:
899+
if expand_partial:
900900
val1 = module.expand_partial_address(val1)
901901

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

973973
__slots__ = ('_prefixlen',)
974974

975-
def __init__(self, addr, version=None, flags=0, expand=False):
975+
def __init__(self, addr, version=None, flags=0, *, expand_partial=False):
976976
"""
977977
Constructor.
978978
@@ -990,14 +990,14 @@ def __init__(self, addr, version=None, flags=0, expand=False):
990990
interpretation of the addr value. Currently only supports the
991991
:data:`NOHOST` option.
992992
993-
:param expand: (optional) decides whether partial address is
993+
:param expand_partial: (optional) decides whether partial address is
994994
expanded. Currently this is only effective for IPv4 address.
995995
996996
>>> IPNetwork('1.2.3.4/24')
997997
IPNetwork('1.2.3.4/24')
998998
>>> IPNetwork('1.2.3.4/24', flags=NOHOST)
999999
IPNetwork('1.2.3.0/24')
1000-
>>> IPNetwork('10/24', expand=True)
1000+
>>> IPNetwork('10/24', expand_partial=True)
10011001
IPNetwork('10.0.0.0/24')
10021002
"""
10031003
super(IPNetwork, self).__init__()
@@ -1021,7 +1021,7 @@ def __init__(self, addr, version=None, flags=0, expand=False):
10211021
module = addr._module
10221022
prefixlen = module.width
10231023
elif version == 4:
1024-
value, prefixlen = parse_ip_network(_ipv4, addr, flags, expand)
1024+
value, prefixlen = parse_ip_network(_ipv4, addr, flags, expand_partial=expand_partial)
10251025
module = _ipv4
10261026
elif version == 6:
10271027
value, prefixlen = parse_ip_network(_ipv6, addr, flags)
@@ -1031,7 +1031,9 @@ def __init__(self, addr, version=None, flags=0, expand=False):
10311031
raise ValueError('%r is an invalid IP version!' % version)
10321032
try:
10331033
module = _ipv4
1034-
value, prefixlen = parse_ip_network(module, addr, flags, expand)
1034+
value, prefixlen = parse_ip_network(
1035+
module, addr, flags, expand_partial=expand_partial
1036+
)
10351037
except AddrFormatError:
10361038
try:
10371039
module = _ipv6

netaddr/tests/ip/test_ip_v4.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -355,12 +355,12 @@ def test_ipnetwork_bad_string_constructor():
355355
IPNetwork('foo')
356356

357357

358-
def test_ipnetwork_expand_v4():
358+
def test_ipnetwork_expand_partial_v4():
359359
with pytest.raises(AddrFormatError):
360360
IPNetwork('10/8')
361-
assert IPNetwork('10/8', expand=True) == IPNetwork('10.0.0.0/8')
362-
assert IPNetwork('10.20/16', expand=True) == IPNetwork('10.20.0.0/16')
363-
assert IPNetwork('10.20.30/24', expand=True) == IPNetwork('10.20.30.0/24')
361+
assert IPNetwork('10/8', expand_partial=True) == IPNetwork('10.0.0.0/8')
362+
assert IPNetwork('10.20/16', expand_partial=True) == IPNetwork('10.20.0.0/16')
363+
assert IPNetwork('10.20.30/24', expand_partial=True) == IPNetwork('10.20.30.0/24')
364364

365365

366366
def test_ipaddress_netmask_v4():

0 commit comments

Comments
 (0)