chore: update to modern typing (#1502) · python-zeroconf/python-zeroconf@64138a3
@@ -20,8 +20,10 @@
2020USA
2121"""
222223+from __future__ import annotations
24+2325from 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
25272628from ._dns import (
2729DNSAddress,
@@ -66,8 +68,8 @@ class DNSCache:
66686769def __init__(self) -> None:
6870self.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] = {}
7173self.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:
135137for entry in entries:
136138self._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]:
145147if not (expire_heap_len := len(self._expire_heap)):
146148return []
147149148-expired: List[DNSRecord] = []
150+expired: list[DNSRecord] = []
149151# Find any expired records and add them to the to-delete list
150152while self._expire_heap:
151153when_record = self._expire_heap[0]
@@ -182,7 +184,7 @@ def async_expire(self, now: _float) -> List[DNSRecord]:
182184self.async_remove_records(expired)
183185return 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]:
194196return None
195197return 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 """
203205key = name.lower()
204206records = self.cache.get(key)
205-matches: List[DNSRecord] = []
207+matches: list[DNSRecord] = []
206208if records is None:
207209return matches
208210for record in records.values():
209211if type_ == record.type and class_ == record.class_:
210212matches.append(record)
211213return 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 """
219221return 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."""
236238if isinstance(entry, _UNIQUE_RECORD_TYPES):
@@ -240,7 +242,7 @@ def get(self, entry: DNSEntry) -> Optional[DNSRecord]:
240242return cached_entry
241243return 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
261263return cached_entry
262264return 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."""
266268key = name.lower()
267269records = self.cache.get(key)
268270if records is None:
269271return []
270272return [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."""
274276if entries := self.service_cache.get(server.lower()):
275277return list(entries.values())
276278return []
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."""
280282if entries := self.cache.get(name.lower()):
281283return list(entries.values())
282284return []
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:
285287now = current_time_millis()
286288for record in reversed(self.entries_with_name(name)):
287289if (
@@ -292,13 +294,13 @@ def current_entry_with_name_and_alias(self, name: str, alias: str) -> Optional[D
292294return record
293295return None
294296295-def names(self) -> List[str]:
297+def names(self) -> list[str]:
296298"""Return a copy of the list of current cache names."""
297299return list(self.cache)
298300299301def async_mark_unique_records_older_than_1s_to_expire(
300302self,
301-unique_types: Set[Tuple[_str, _int, _int]],
303+unique_types: set[tuple[_str, _int, _int]],
302304answers: Iterable[DNSRecord],
303305now: _float,
304306 ) -> None: