Skip to content

Commit

Permalink
fix: remove useless calls in ServiceInfo (#1248)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco committed Sep 2, 2023
1 parent a7feade commit 4e40fae
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions src/zeroconf/_services/info.py
Expand Up @@ -427,17 +427,17 @@ def _process_record_threadsafe(self, zc: 'Zeroconf', record: DNSRecord, now: flo
return False

record_key = record.key
if record_key == self.server_key and type(record) is DNSAddress:
record_type = type(record)
if record_key == self.server_key and record_type is DNSAddress:
if TYPE_CHECKING:
assert isinstance(record, DNSAddress)
try:
ip_addr = _cached_ip_addresses(record.address)
except ValueError as ex:
log.warning("Encountered invalid address while processing %s: %s", record, ex)
return False

if type(ip_addr) is IPv4Address:
if self._ipv4_addresses:
self._set_ipv4_addresses_from_cache(zc, now)

ipv4_addresses = self._ipv4_addresses
if ip_addr not in ipv4_addresses:
ipv4_addresses.insert(0, ip_addr)
Expand All @@ -448,9 +448,6 @@ def _process_record_threadsafe(self, zc: 'Zeroconf', record: DNSRecord, now: flo

return False

if not self._ipv6_addresses:
self._set_ipv6_addresses_from_cache(zc, now)

ipv6_addresses = self._ipv6_addresses
if ip_addr not in self._ipv6_addresses:
ipv6_addresses.insert(0, ip_addr)
Expand All @@ -464,13 +461,18 @@ def _process_record_threadsafe(self, zc: 'Zeroconf', record: DNSRecord, now: flo
if record_key != self.key:
return False

if record.type == _TYPE_TXT and type(record) is DNSText:
if record_type is DNSText:
if TYPE_CHECKING:
assert isinstance(record, DNSText)
self._set_text(record.text)
return True

if record.type == _TYPE_SRV and type(record) is DNSService:
if record_type is DNSService:
if TYPE_CHECKING:
assert isinstance(record, DNSService)
old_server_key = self.server_key
self.name = record.name
self._name = record.name
self.key = record.key
self.server = record.server
self.server_key = record.server_key
self.port = record.port
Expand Down Expand Up @@ -577,7 +579,11 @@ def _get_address_records_from_cache_by_type(self, zc: 'Zeroconf', _type: int) ->
"""Get the addresses from the cache."""
if self.server_key is None:
return []
return cast("List[DNSAddress]", zc.cache.get_all_by_details(self.server_key, _type, _CLASS_IN))
if TYPE_CHECKING:
records = cast("List[DNSAddress]", zc.cache.get_all_by_details(self.server_key, _type, _CLASS_IN))
else:
records = zc.cache.get_all_by_details(self.server_key, _type, _CLASS_IN)
return records

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

0 comments on commit 4e40fae

Please sign in to comment.