Use stdlib ip_address method in the network helper when compatible (#102019)

This commit is contained in:
J. Nick Koston 2023-10-15 15:24:49 -10:00 committed by GitHub
parent c60cc11505
commit d0ba42283c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -7,10 +7,12 @@ import re
import yarl
# RFC6890 - IP addresses of loopback interfaces
IPV6_IPV4_LOOPBACK = ip_network("::ffff:127.0.0.0/104")
LOOPBACK_NETWORKS = (
ip_network("127.0.0.0/8"),
ip_network("::1/128"),
ip_network("::ffff:127.0.0.0/104"),
IPV6_IPV4_LOOPBACK,
)
# RFC6890 - Address allocation for Private Internets
@ -34,7 +36,7 @@ LINK_LOCAL_NETWORKS = (
def is_loopback(address: IPv4Address | IPv6Address) -> bool:
"""Check if an address is a loopback address."""
return any(address in network for network in LOOPBACK_NETWORKS)
return address.is_loopback or address in IPV6_IPV4_LOOPBACK
def is_private(address: IPv4Address | IPv6Address) -> bool:
@ -44,7 +46,7 @@ def is_private(address: IPv4Address | IPv6Address) -> bool:
def is_link_local(address: IPv4Address | IPv6Address) -> bool:
"""Check if an address is link-local (local but not necessarily unique)."""
return any(address in network for network in LINK_LOCAL_NETWORKS)
return address.is_link_local
def is_local(address: IPv4Address | IPv6Address) -> bool:
@ -54,7 +56,7 @@ def is_local(address: IPv4Address | IPv6Address) -> bool:
def is_invalid(address: IPv4Address | IPv6Address) -> bool:
"""Check if an address is invalid."""
return bool(address == ip_address("0.0.0.0"))
return address.is_unspecified
def is_ip_address(address: str) -> bool: