8000 chore: cleanup typing in net utils by bdraco · Pull Request #1521 · python-zeroconf/python-zeroconf · GitHub
[go: up one dir, main page]

Skip to content

chore: cleanup typing in net utils #1521

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 13, 2025
Merged
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
20 changes: 10 additions & 10 deletions src/zeroconf/_utils/net.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,29 +85,30 @@ def get_all_addresses_v6() -> list[tuple[tuple[str, int, int], int]]:
)


def ip6_to_address_and_index(adapters: list[Any], ip: str) -> tuple[tuple[str, int, int], int]:
def ip6_to_address_and_index(adapters: list[ifaddr.Adapter], ip: str) -> tuple[tuple[str, int, int], int]:
if "%" in ip:
ip = ip[: ip.index("%")] # Strip scope_id.
ipaddr = ipaddress.ip_address(ip)
for adapter in adapters:
for adapter_ip in adapter.ips:
# IPv6 addresses are represented as tuples
if isinstance(adapter_ip.ip, tuple) and ipaddress.ip_address(adapter_ip.ip[0]) == ipaddr:
return (
cast(tuple[str, int, int], adapter_ip.ip),
cast(int, adapter.index),
)
if (
adapter.index is not None
and isinstance(adapter_ip.ip, tuple)
and ipaddress.ip_address(adapter_ip.ip[0]) == ipaddr
):
return (adapter_ip.ip, adapter.index)

raise RuntimeError(f"No adapter found for IP address {ip}")


def interface_index_to_ip6_address(adapters: list[Any], index: int) -> tuple[str, int, int]:
def interface_index_to_ip6_address(adapters: list[ifaddr.Adapter], index: int) -> tuple[str, int, int]:
for adapter in adapters:
if adapter.index == index:
for adapter_ip in adapter.ips:
# IPv6 addresses are represented as tuples
if isinstance(adapter_ip.ip, tuple):
return cast(tuple[str, int, int], adapter_ip.ip)
return adapter_ip.ip

raise RuntimeError(f"No adapter found for index {index}")

Expand Down Expand Up @@ -414,8 +415,7 @@ def create_sockets(
return listen_socket, respond_sockets


def get_errno(e: Exception) -> int:
assert isinstance(e, socket.error)
def get_errno(e: OSError) -> int:
return cast(int, e.args[0])


Expand Down
Loading
0