Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/zeroconf/_services/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def __init__(
host_ttl: int = _DNS_HOST_TTL,
other_ttl: int = _DNS_OTHER_TTL,
*,
addresses: list[bytes] | None = None,
addresses: Sequence[bytes | str] | None = None,
parsed_addresses: list[str] | None = None,
interface_index: int | None = None,
) -> None:
Expand Down Expand Up @@ -292,7 +292,7 @@ def addresses(self) -> list[bytes]:
return self.addresses_by_version(IPVersion.V4Only)

@addresses.setter
def addresses(self, value: list[bytes]) -> None:
def addresses(self, value: Sequence[bytes | str]) -> None:
"""Replace the addresses list.

This replaces all currently stored addresses, both IPv4 and IPv6.
Expand Down
2 changes: 1 addition & 1 deletion src/zeroconf/_utils/ipaddress.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def get_ip_address_object_from_record(


def ip_bytes_and_scope_to_address(
address: bytes_, scope: int_
address: bytes_ | str, scope: int_
) -> ZeroconfIPv4Address | ZeroconfIPv6Address | None:
"""Convert the bytes and scope to an IP address object."""
base_address = cached_ip_addresses_wrapper(address)
Expand Down
27 changes: 27 additions & 0 deletions tests/services/test_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -3071,3 +3071,30 @@ def test_denied_flag_is_ignored_when_address_is_held(zc_loopback: r.Zeroconf) ->
assert info.addresses == [socket.inet_aton("127.0.0.1")]
# the held A record keeps the request querying instead of fast failing
assert info._is_denied is False


@pytest.mark.parametrize(
"addresses",
[
["10.0.1.2", "2001:db8::1"],
[socket.inet_aton("10.0.1.2"), socket.inet_pton(socket.AF_INET6, "2001:db8::1")],
["10.0.1.2", socket.inet_pton(socket.AF_INET6, "2001:db8::1")],
],
)
def test_addresses_setter_accepts_str_and_bytes(addresses):
"""The addresses setter accepts str and bytes addresses."""
type_ = "_http._tcp.local."
info = ServiceInfo(type_, f"xxxyyy.{type_}", 80, server="ash-2.local.")
info.addresses = addresses
assert info.parsed_addresses() == ["10.0.1.2", "2001:db8::1"]

info = ServiceInfo(type_, f"xxxyyy.{type_}", 80, server="ash-2.local.", addresses=addresses)
assert info.parsed_addresses() == ["10.0.1.2", "2001:db8::1"]


def test_addresses_setter_rejects_invalid_address():
"""The addresses setter raises TypeError for invalid addresses."""
type_ = "_http._tcp.local."
info = ServiceInfo(type_, f"xxxyyy.{type_}", 80, server="ash-2.local.")
with pytest.raises(TypeError, match="Addresses must either be"):
info.addresses = ["not an address"]
Loading