Skip to content

Commit

Permalink
[PR #9344/ea991a91 backport][3.11] Remove unused ip address helpers (#…
Browse files Browse the repository at this point in the history
…9349)

Co-authored-by: J. Nick Koston <nick@koston.org>
  • Loading branch information
patchback[bot] and bdraco authored Sep 29, 2024
1 parent 35b4c90 commit 1ca7244
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 63 deletions.
1 change: 1 addition & 0 deletions CHANGES/9344.breaking.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Removed the ``is_ipv6_address`` and ``is_ip4_address`` helpers are they are no longer used -- by :user:`bdraco`.
40 changes: 3 additions & 37 deletions aiohttp/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,51 +469,17 @@ def __set__(self, inst: _TSelf[_T], value: _T) -> None:
pass


def is_ipv4_address(host: Optional[Union[str, bytes]]) -> bool:
"""Check if host looks like an IPv4 address.
This function does not validate that the format is correct, only that
the host is a str or bytes, and its all numeric.
def is_ip_address(host: Optional[str]) -> bool:
"""Check if host looks like an IP Address.
This check is only meant as a heuristic to ensure that
a host is not a domain name.
"""
if not host:
return False
# For a host to be an ipv4 address, it must be all numeric.
if isinstance(host, str):
return host.replace(".", "").isdigit()
if isinstance(host, (bytes, bytearray, memoryview)):
return host.decode("ascii").replace(".", "").isdigit()
raise TypeError(f"{host} [{type(host)}] is not a str or bytes")


def is_ipv6_address(host: Optional[Union[str, bytes]]) -> bool:
"""Check if host looks like an IPv6 address.
This function does not validate that the format is correct, only that
the host contains a colon and that it is a str or bytes.
This check is only meant as a heuristic to ensure that
a host is not a domain name.
"""
if not host:
return False
# The host must contain a colon to be an IPv6 address.
if isinstance(host, str):
return ":" in host
if isinstance(host, (bytes, bytearray, memoryview)):
return b":" in host
raise TypeError(f"{host} [{type(host)}] is not a str or bytes")


def is_ip_address(host: Optional[Union[str, bytes, bytearray, memoryview]]) -> bool:
"""Check if host looks like an IP Address.
This check is only meant as a heuristic to ensure that
a host is not a domain name.
"""
return is_ipv4_address(host) or is_ipv6_address(host)
return ":" in host or host.replace(".", "").isdigit()


_cached_current_datetime: Optional[int] = None
Expand Down
26 changes: 0 additions & 26 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,25 +268,13 @@ def test_is_ip_address() -> None:
assert not helpers.is_ip_address("www.example.com")


def test_is_ip_address_bytes() -> None:
assert helpers.is_ip_address(b"127.0.0.1")
assert helpers.is_ip_address(b"::1")
assert helpers.is_ip_address(b"FE80:0000:0000:0000:0202:B3FF:FE1E:8329")

# Hostnames
assert not helpers.is_ip_address(b"localhost")
assert not helpers.is_ip_address(b"www.example.com")


def test_ipv4_addresses() -> None:
ip_addresses = [
"0.0.0.0",
"127.0.0.1",
"255.255.255.255",
]
for address in ip_addresses:
assert helpers.is_ipv4_address(address)
assert not helpers.is_ipv6_address(address)
assert helpers.is_ip_address(address)


Expand All @@ -302,8 +290,6 @@ def test_ipv6_addresses() -> None:
"1::1",
]
for address in ip_addresses:
assert not helpers.is_ipv4_address(address)
assert helpers.is_ipv6_address(address)
assert helpers.is_ip_address(address)


Expand All @@ -325,18 +311,6 @@ def test_is_ip_address_invalid_type() -> None:
with pytest.raises(TypeError):
helpers.is_ip_address(object())

with pytest.raises(TypeError):
helpers.is_ipv4_address(123) # type: ignore[arg-type]

with pytest.raises(TypeError):
helpers.is_ipv4_address(object()) # type: ignore[arg-type]

with pytest.raises(TypeError):
helpers.is_ipv6_address(123) # type: ignore[arg-type]

with pytest.raises(TypeError):
helpers.is_ipv6_address(object()) # type: ignore[arg-type]


# ----------------------------------- TimeoutHandle -------------------

Expand Down

0 comments on commit 1ca7244

Please sign in to comment.