ip interface passthrough fixed

This commit is contained in:
Cian Hughes
2023-11-08 12:51:46 +00:00
parent 29f292edaa
commit a524e7c719
2 changed files with 68 additions and 36 deletions

View File

@@ -4,37 +4,73 @@ from ipaddress import IPv4Address, IPv6Address, ip_address
class IPAddress:
def __init__(self, *args, **kwargs) -> None:
self.obj: IPv4Address | IPv6Address = ip_address(*args, **kwargs)
to_passthrough = (
"compressed",
"exploded",
"is_global",
"is_link_local",
"is_loopback",
"is_multicast",
"is_private",
"is_reserved",
"is_unspecified",
"max_prefixlen",
"packed",
"reverse_pointer",
"version",
)
for attrname in to_passthrough:
self._passthrough(attrname)
# to_passthrough = (
# "compressed",
# "exploded",
# "is_global",
# "is_link_local",
# "is_loopback",
# "is_multicast",
# "is_private",
# "is_reserved",
# "is_unspecified",
# "max_prefixlen",
# "packed",
# "reverse_pointer",
# "version",
# )
def _passthrough(self, attrname: str) -> None:
"""Passes through an attribute from the underlying IPv4Address or IPv6Address object
Args:
attrname (str): The name of the attribute to pass through
Raises:
AttributeError: If the attribute is a method
"""
attr = getattr(self.obj, attrname)
if callable(attr):
raise AttributeError(f"Passthrough is unavailable for methods ({attrname})")
setattr(self, attrname, attr)
@property
def compressed(self) -> str:
return self.obj.compressed
@property
def exploded(self) -> str:
return self.obj.exploded
@property
def is_global(self) -> bool:
return self.obj.is_global
@property
def is_link_local(self) -> bool:
return self.obj.is_link_local
@property
def is_loopback(self) -> bool:
return self.obj.is_loopback
@property
def is_multicast(self) -> bool:
return self.obj.is_multicast
@property
def is_private(self) -> bool:
return self.obj.is_private
@property
def is_reserved(self) -> bool:
return self.obj.is_reserved
@property
def is_unspecified(self) -> bool:
return self.obj.is_unspecified
@property
def max_prefixlen(self) -> int:
return self.obj.max_prefixlen
@property
def packed(self) -> bytes:
return self.obj.packed
@property
def reverse_pointer(self) -> str:
return self.obj.reverse_pointer
@property
def version(self) -> int:
return self.obj.version
def __str__(self) -> str:
return str(self.obj)

View File

@@ -70,11 +70,9 @@ class TestIPInterface:
@given(st.ip_addresses(v=4))
def test_ipv4_attr_passthrough(self, ip: IPv4Address):
# Should be able to access all attributes of ipaddress.IPv4Address directly,
# not just a copy of the attribute object at instantiation
ip_addr = ip_interface.IPAddress(str(ip))
for attr in self.TEST_ATTRS:
assert getattr(ip_addr, attr) is getattr(ip_addr.obj, attr)
assert getattr(ip_addr, attr) == getattr(ip_addr.obj, attr)
@given(st.ip_addresses(v=4))
def test_ipv4_bool(self, ip: IPv4Address):
@@ -89,11 +87,9 @@ class TestIPInterface:
@given(st.ip_addresses(v=6))
def test_ipv6_attr_passthrough(self, ip: IPv6Address):
# Should be able to access all attributes of ipaddress.IPv4Address directly,
# not just a copy of the attribute object at instantiation
ip_addr = ip_interface.IPAddress(str(ip))
for attr in self.TEST_ATTRS:
assert getattr(ip_addr, attr) is getattr(ip_addr.obj, attr)
assert getattr(ip_addr, attr) == getattr(ip_addr.obj, attr)
@given(st.ip_addresses(v=6))
def test_ipv6_bool(self, ip: IPv6Address):