feat: small speed up to writing outgoing dns records (#1258) · python-zeroconf/python-zeroconf@1ed6bd2

@@ -22,7 +22,7 @@

22222323

import enum

2424

import logging

25-

from typing import Dict, List, Optional, Sequence, Tuple, Union

25+

from typing import TYPE_CHECKING, Dict, List, Optional, Sequence, Tuple, Union

26262727

from .._cache import DNSCache

2828

from .._dns import DNSPointer, DNSQuestion, DNSRecord

@@ -176,7 +176,7 @@ def add_additional_answer(self, record: DNSRecord) -> None:

176176

self.additionals.append(record)

177177178178

def 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."""

182182

cached_entry = cache.get_by_details(name, type_, class_)

@@ -186,7 +186,7 @@ def add_question_or_one_cache(

186186

self.add_answer_at_time(cached_entry, now)

187187188188

def 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:

223223224224

def 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)

227228

self.data.append(value)

228229

self.size += len(value)

229230

@@ -237,7 +238,8 @@ def _write_utf(self, s: str) -> None:

237238

self.write_string(utfstr)

238239239240

def write_character_string(self, value: bytes) -> None:

240-

assert isinstance(value, bytes)

241+

if TYPE_CHECKING:

242+

assert isinstance(value, bytes)

241243

length = len(value)

242244

if length > 256:

243245

raise NamePartTooLongException