Skip to content

Commit e0f8a0e

Browse files
committed
feat: add more cython typing to DNSRecord
1 parent d713a45 commit e0f8a0e

2 files changed

Lines changed: 25 additions & 12 deletions

File tree

src/zeroconf/_dns.pxd

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ cdef object _LEN_INT
99
cdef object _NAME_COMPRESSION_MIN_SIZE
1010
cdef object _BASE_MAX_SIZE
1111

12-
cdef object _EXPIRE_FULL_TIME_MS
13-
cdef object _EXPIRE_STALE_TIME_MS
14-
cdef object _RECENT_TIME_MS
12+
cdef cython.float _EXPIRE_FULL_TIME_MS
13+
cdef cython.float _EXPIRE_STALE_TIME_MS
14+
cdef cython.float _RECENT_TIME_MS
1515

1616
cdef object _CLASS_UNIQUE
1717
cdef object _CLASS_MASK
1818

1919
cdef object current_time_millis
20+
cdef object millis_to_seconds
2021

2122
cdef class DNSEntry:
2223

@@ -34,11 +35,16 @@ cdef class DNSQuestion(DNSEntry):
3435

3536
cdef class DNSRecord(DNSEntry):
3637

37-
cdef public object ttl
38-
cdef public object created
38+
cdef public cython.float ttl
39+
cdef public cython.float created
3940

4041
cdef _suppressed_by_answer(self, DNSRecord answer)
4142

43+
cpdef get_expiration_time(self, cython.float percent)
44+
45+
cpdef reset_ttl(self, DNSRecord other)
46+
47+
cpdef set_created_ttl(self, cython.float created, cython.float ttl)
4248

4349
cdef class DNSAddress(DNSRecord):
4450

src/zeroconf/_dns.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@
4141
_RECENT_TIME_MS = 250
4242

4343

44+
int_ = int
45+
float_ = float
46+
int_or_float_ = Union[int, float]
47+
4448
if TYPE_CHECKING:
4549
from ._protocol.incoming import DNSIncoming
4650
from ._protocol.outgoing import DNSOutgoing
@@ -159,7 +163,7 @@ class DNSRecord(DNSEntry):
159163

160164
# TODO: Switch to just int ttl
161165
def __init__(
162-
self, name: str, type_: int, class_: int, ttl: Union[float, int], created: Optional[float] = None
166+
self, name: str, type_: int, class_: int, ttl: int_or_float_, created: Optional[float] = None
163167
) -> None:
164168
super().__init__(name, type_, class_)
165169
self.ttl = ttl
@@ -172,20 +176,23 @@ def __eq__(self, other: Any) -> bool: # pylint: disable=no-self-use
172176
def suppressed_by(self, msg: 'DNSIncoming') -> bool:
173177
"""Returns true if any answer in a message can suffice for the
174178
information held in this record."""
175-
return any(self._suppressed_by_answer(record) for record in msg.answers)
179+
for record in msg.answers:
180+
if self._suppressed_by_answer(record):
181+
return True
182+
return False
176183

177184
def _suppressed_by_answer(self, other) -> bool: # type: ignore[no-untyped-def]
178185
"""Returns true if another record has same name, type and class,
179186
and if its TTL is at least half of this record's."""
180-
return self == other and other.ttl > (self.ttl / 2)
187+
return bool(self == other and other.ttl > (self.ttl / 2))
181188

182-
def get_expiration_time(self, percent: int) -> float:
189+
def get_expiration_time(self, percent: int_) -> float:
183190
"""Returns the time at which this record will have expired
184191
by a certain percentage."""
185192
return self.created + (percent * self.ttl * 10)
186193

187194
# TODO: Switch to just int here
188-
def get_remaining_ttl(self, now: float) -> Union[int, float]:
195+
def get_remaining_ttl(self, now: float) -> int_or_float_:
189196
"""Returns the remaining TTL in seconds."""
190197
return max(0, millis_to_seconds((self.created + (_EXPIRE_FULL_TIME_MS * self.ttl)) - now))
191198

@@ -206,7 +213,7 @@ def reset_ttl(self, other: 'DNSRecord') -> None:
206213
another record."""
207214
self.set_created_ttl(other.created, other.ttl)
208215

209-
def set_created_ttl(self, created: float, ttl: Union[float, int]) -> None:
216+
def set_created_ttl(self, created: float_, ttl: int_or_float_) -> None:
210217
"""Set the created and ttl of a record."""
211218
self.created = created
212219
self.ttl = ttl
@@ -403,7 +410,7 @@ def __init__(
403410
name: str,
404411
type_: int,
405412
class_: int,
406-
ttl: Union[float, int],
413+
ttl: int_or_float_,
407414
priority: int,
408415
weight: int,
409416
port: int,

0 commit comments

Comments
 (0)