feat: small speed up to writing outgoing dns records (#1258) · python-zeroconf/python-zeroconf@1ed6bd2
@@ -22,7 +22,7 @@
22222323import enum
2424import logging
25-from typing import Dict, List, Optional, Sequence, Tuple, Union
25+from typing import TYPE_CHECKING, Dict, List, Optional, Sequence, Tuple, Union
26262727from .._cache import DNSCache
2828from .._dns import DNSPointer, DNSQuestion, DNSRecord
@@ -176,7 +176,7 @@ def add_additional_answer(self, record: DNSRecord) -> None:
176176self.additionals.append(record)
177177178178def add_question_or_one_cache(
179-self, cache: DNSCache, now: float, name: str, type_: int, class_: int
179+self, cache: DNSCache, now: float_, name: str_, type_: int_, class_: int_
180180 ) -> None:
181181"""Add a question if it is not already cached."""
182182cached_entry = cache.get_by_details(name, type_, class_)
@@ -186,7 +186,7 @@ def add_question_or_one_cache(
186186self.add_answer_at_time(cached_entry, now)
187187188188def add_question_or_all_cache(
189-self, cache: DNSCache, now: float, name: str, type_: int, class_: int
189+self, cache: DNSCache, now: float_, name: str_, type_: int_, class_: int_
190190 ) -> None:
191191"""Add a question if it is not already cached.
192192 This is currently only used for IPv6 addresses.
@@ -223,7 +223,8 @@ def _write_int(self, value: Union[float, int]) -> None:
223223224224def write_string(self, value: bytes) -> None:
225225"""Writes a string to the packet"""
226-assert isinstance(value, bytes)
226+if TYPE_CHECKING:
227+assert isinstance(value, bytes)
227228self.data.append(value)
228229self.size += len(value)
229230@@ -237,7 +238,8 @@ def _write_utf(self, s: str) -> None:
237238self.write_string(utfstr)
238239239240def write_character_string(self, value: bytes) -> None:
240-assert isinstance(value, bytes)
241+if TYPE_CHECKING:
242+assert isinstance(value, bytes)
241243length = len(value)
242244if length > 256:
243245raise NamePartTooLongException