Skip to content

Commit 83b54ba

Browse files
committed
fix: add some missing type checking guards
1 parent 109bbe1 commit 83b54ba

4 files changed

Lines changed: 23 additions & 13 deletions

File tree

src/zeroconf/_cache.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"""
2222

2323
import itertools
24+
from collections.abc import Sequence
2425
from typing import Dict, Iterable, List, Optional, Set, Tuple, Union, cast
2526

2627
from ._dns import (
@@ -208,7 +209,7 @@ def get_by_details(self, name: str, type_: int, class_: int) -> Optional[DNSReco
208209
return cached_entry
209210
return None
210211

211-
def get_all_by_details(self, name: str, type_: int, class_: int) -> List[DNSRecord]:
212+
def get_all_by_details(self, name: str, type_: int, class_: int) -> Sequence[DNSRecord]:
212213
"""Gets all matching entries by details."""
213214
key = name.lower()
214215
return [

src/zeroconf/_services/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ def update_service(self, zc: 'Zeroconf', type_: str, name: str) -> None:
4646

4747

4848
class Signal:
49-
5049
__slots__ = ('_handlers',)
5150

5251
def __init__(self) -> None:
@@ -62,7 +61,6 @@ def registration_interface(self) -> 'SignalRegistrationInterface':
6261

6362

6463
class SignalRegistrationInterface:
65-
6664
__slots__ = ('_handlers',)
6765

6866
def __init__(self, handlers: List[Callable[..., None]]) -> None:

src/zeroconf/_services/browser.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,9 @@ def generate_service_query(
166166
if not qu_question and zc.question_history.suppresses(question, now, known_answers):
167167
log.debug("Asking %s was suppressed by the question history", question)
168168
continue
169-
questions_with_known_answers[question] = cast(Set[DNSPointer], known_answers)
169+
if TYPE_CHECKING:
170+
known_answers = cast(Set[DNSPointer], known_answers) # type: ignore[assignment]
171+
questions_with_known_answers[question] = known_answers # type: ignore[assignment]
170172
if not qu_question:
171173
zc.question_history.add_question_at_time(question, now, known_answers)
172174

src/zeroconf/_services/info.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import asyncio
2424
import ipaddress
2525
import random
26+
from collections.abc import Sequence
2627
from functools import lru_cache
2728
from typing import TYPE_CHECKING, Dict, List, Optional, Set, Union, cast
2829

@@ -366,7 +367,7 @@ def get_name(self) -> str:
366367

367368
def _get_ip_addresses_from_cache_lifo(
368369
self, zc: 'Zeroconf', now: float, type: int
369-
) -> List[Union[ipaddress.IPv4Address, ipaddress.IPv6Address]]:
370+
) -> Sequence[Union[ipaddress.IPv4Address, ipaddress.IPv6Address]]:
370371
"""Set IPv6 addresses from the cache."""
371372
address_list: List[Union[ipaddress.IPv4Address, ipaddress.IPv6Address]] = []
372373
for record in self._get_address_records_from_cache_by_type(zc, type):
@@ -383,15 +384,17 @@ def _get_ip_addresses_from_cache_lifo(
383384

384385
def _set_ipv6_addresses_from_cache(self, zc: 'Zeroconf', now: float) -> None:
385386
"""Set IPv6 addresses from the cache."""
386-
self._ipv6_addresses = cast(
387-
"List[ipaddress.IPv6Address]", self._get_ip_addresses_from_cache_lifo(zc, now, _TYPE_AAAA)
388-
)
387+
ipv6_addresses = self._get_ip_addresses_from_cache_lifo(zc, now, _TYPE_AAAA)
388+
if TYPE_CHECKING:
389+
ipv6_addresses = cast("List[ipaddress.IPv6Address]", ipv6_addresses)
390+
self._ipv6_addresses = ipv6_addresses
389391

390392
def _set_ipv4_addresses_from_cache(self, zc: 'Zeroconf', now: float) -> None:
391393
"""Set IPv4 addresses from the cache."""
392-
self._ipv4_addresses = cast(
393-
"List[ipaddress.IPv4Address]", self._get_ip_addresses_from_cache_lifo(zc, now, _TYPE_A)
394-
)
394+
ipv4_addresses = self._get_ip_addresses_from_cache_lifo(zc, now, _TYPE_A)
395+
if TYPE_CHECKING:
396+
ipv4_addresses = cast("List[ipaddress.IPv4Address]", ipv4_addresses)
397+
self._ipv4_addresses = ipv4_addresses
395398

396399
def update_record(self, zc: 'Zeroconf', now: float, record: Optional[DNSRecord]) -> None:
397400
"""Updates service information from a DNS record.
@@ -520,14 +523,17 @@ def dns_pointer(self, override_ttl: Optional[int] = None, created: Optional[floa
520523

521524
def dns_service(self, override_ttl: Optional[int] = None, created: Optional[float] = None) -> DNSService:
522525
"""Return DNSService from ServiceInfo."""
526+
port = self.port
527+
if TYPE_CHECKING:
528+
port = cast(int, port)
523529
return DNSService(
524530
self.name,
525531
_TYPE_SRV,
526532
_CLASS_IN | _CLASS_UNIQUE,
527533
override_ttl if override_ttl is not None else self.host_ttl,
528534
self.priority,
529535
self.weight,
530-
cast(int, self.port),
536+
port,
531537
self.server or self.name,
532538
created,
533539
)
@@ -576,7 +582,10 @@ def _get_address_records_from_cache_by_type(self, zc: 'Zeroconf', _type: int) ->
576582
"""Get the addresses from the cache."""
577583
if self.server_key is None:
578584
return []
579-
return cast("List[DNSAddress]", zc.cache.get_all_by_details(self.server_key, _type, _CLASS_IN))
585+
result = zc.cache.get_all_by_details(self.server_key, _type, _CLASS_IN)
586+
if TYPE_CHECKING:
587+
result = cast("List[DNSAddress]", result)
588+
return result
580589

581590
def set_server_if_missing(self) -> None:
582591
"""Set the server if it is missing.

0 commit comments

Comments
 (0)