chore: update to modern typing (#1502) · python-zeroconf/python-zeroconf@64138a3

@@ -20,8 +20,10 @@

2020

USA

2121

"""

222223+

from __future__ import annotations

24+2325

from heapq import heapify, heappop, heappush

24-

from typing import Dict, Iterable, List, Optional, Set, Tuple, Union, cast

26+

from typing import Dict, Iterable, Union, cast

25272628

from ._dns import (

2729

DNSAddress,

@@ -66,8 +68,8 @@ class DNSCache:

66686769

def __init__(self) -> None:

6870

self.cache: _DNSRecordCacheType = {}

69-

self._expire_heap: List[Tuple[float, DNSRecord]] = []

70-

self._expirations: Dict[DNSRecord, float] = {}

71+

self._expire_heap: list[tuple[float, DNSRecord]] = []

72+

self._expirations: dict[DNSRecord, float] = {}

7173

self.service_cache: _DNSRecordCacheType = {}

72747375

# Functions prefixed with async_ are NOT threadsafe and must

@@ -135,7 +137,7 @@ def async_remove_records(self, entries: Iterable[DNSRecord]) -> None:

135137

for entry in entries:

136138

self._async_remove(entry)

137139138-

def async_expire(self, now: _float) -> List[DNSRecord]:

140+

def async_expire(self, now: _float) -> list[DNSRecord]:

139141

"""Purge expired entries from the cache.

140142141143

This function must be run in from event loop.

@@ -145,7 +147,7 @@ def async_expire(self, now: _float) -> List[DNSRecord]:

145147

if not (expire_heap_len := len(self._expire_heap)):

146148

return []

147149148-

expired: List[DNSRecord] = []

150+

expired: list[DNSRecord] = []

149151

# Find any expired records and add them to the to-delete list

150152

while self._expire_heap:

151153

when_record = self._expire_heap[0]

@@ -182,7 +184,7 @@ def async_expire(self, now: _float) -> List[DNSRecord]:

182184

self.async_remove_records(expired)

183185

return expired

184186185-

def async_get_unique(self, entry: _UniqueRecordsType) -> Optional[DNSRecord]:

187+

def async_get_unique(self, entry: _UniqueRecordsType) -> DNSRecord | None:

186188

"""Gets a unique entry by key. Will return None if there is no

187189

matching entry.

188190

@@ -194,31 +196,31 @@ def async_get_unique(self, entry: _UniqueRecordsType) -> Optional[DNSRecord]:

194196

return None

195197

return store.get(entry)

196198197-

def async_all_by_details(self, name: _str, type_: _int, class_: _int) -> List[DNSRecord]:

199+

def async_all_by_details(self, name: _str, type_: _int, class_: _int) -> list[DNSRecord]:

198200

"""Gets all matching entries by details.

199201200202

This function is not thread-safe and must be called from

201203

the event loop.

202204

"""

203205

key = name.lower()

204206

records = self.cache.get(key)

205-

matches: List[DNSRecord] = []

207+

matches: list[DNSRecord] = []

206208

if records is None:

207209

return matches

208210

for record in records.values():

209211

if type_ == record.type and class_ == record.class_:

210212

matches.append(record)

211213

return matches

212214213-

def async_entries_with_name(self, name: str) -> List[DNSRecord]:

215+

def async_entries_with_name(self, name: str) -> list[DNSRecord]:

214216

"""Returns a dict of entries whose key matches the name.

215217216218

This function is not threadsafe and must be called from

217219

the event loop.

218220

"""

219221

return self.entries_with_name(name)

220222221-

def async_entries_with_server(self, name: str) -> List[DNSRecord]:

223+

def async_entries_with_server(self, name: str) -> list[DNSRecord]:

222224

"""Returns a dict of entries whose key matches the server.

223225224226

This function is not threadsafe and must be called from

@@ -230,7 +232,7 @@ def async_entries_with_server(self, name: str) -> List[DNSRecord]:

230232

# event loop, however they all make copies so they significantly

231233

# inefficient.

232234233-

def get(self, entry: DNSEntry) -> Optional[DNSRecord]:

235+

def get(self, entry: DNSEntry) -> DNSRecord | None:

234236

"""Gets an entry by key. Will return None if there is no

235237

matching entry."""

236238

if isinstance(entry, _UNIQUE_RECORD_TYPES):

@@ -240,7 +242,7 @@ def get(self, entry: DNSEntry) -> Optional[DNSRecord]:

240242

return cached_entry

241243

return None

242244243-

def get_by_details(self, name: str, type_: _int, class_: _int) -> Optional[DNSRecord]:

245+

def get_by_details(self, name: str, type_: _int, class_: _int) -> DNSRecord | None:

244246

"""Gets the first matching entry by details. Returns None if no entries match.

245247246248

Calling this function is not recommended as it will only

@@ -261,27 +263,27 @@ def get_by_details(self, name: str, type_: _int, class_: _int) -> Optional[DNSRe

261263

return cached_entry

262264

return None

263265264-

def get_all_by_details(self, name: str, type_: _int, class_: _int) -> List[DNSRecord]:

266+

def get_all_by_details(self, name: str, type_: _int, class_: _int) -> list[DNSRecord]:

265267

"""Gets all matching entries by details."""

266268

key = name.lower()

267269

records = self.cache.get(key)

268270

if records is None:

269271

return []

270272

return [entry for entry in list(records.values()) if type_ == entry.type and class_ == entry.class_]

271273272-

def entries_with_server(self, server: str) -> List[DNSRecord]:

274+

def entries_with_server(self, server: str) -> list[DNSRecord]:

273275

"""Returns a list of entries whose server matches the name."""

274276

if entries := self.service_cache.get(server.lower()):

275277

return list(entries.values())

276278

return []

277279278-

def entries_with_name(self, name: str) -> List[DNSRecord]:

280+

def entries_with_name(self, name: str) -> list[DNSRecord]:

279281

"""Returns a list of entries whose key matches the name."""

280282

if entries := self.cache.get(name.lower()):

281283

return list(entries.values())

282284

return []

283285284-

def current_entry_with_name_and_alias(self, name: str, alias: str) -> Optional[DNSRecord]:

286+

def current_entry_with_name_and_alias(self, name: str, alias: str) -> DNSRecord | None:

285287

now = current_time_millis()

286288

for record in reversed(self.entries_with_name(name)):

287289

if (

@@ -292,13 +294,13 @@ def current_entry_with_name_and_alias(self, name: str, alias: str) -> Optional[D

292294

return record

293295

return None

294296295-

def names(self) -> List[str]:

297+

def names(self) -> list[str]:

296298

"""Return a copy of the list of current cache names."""

297299

return list(self.cache)

298300299301

def async_mark_unique_records_older_than_1s_to_expire(

300302

self,

301-

unique_types: Set[Tuple[_str, _int, _int]],

303+

unique_types: set[tuple[_str, _int, _int]],

302304

answers: Iterable[DNSRecord],

303305

now: _float,

304306

) -> None: