Skip to content

Commit 64138a3

Browse files
authored
chore: update to modern typing (#1502)
1 parent 9d383f5 commit 64138a3

67 files changed

Lines changed: 510 additions & 393 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

examples/browser.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
The default is HTTP and HAP; use --find to search for all available services in the network
66
"""
77

8+
from __future__ import annotations
9+
810
import argparse
911
import logging
1012
from time import sleep

examples/registration.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
"""Example of announcing a service (in this case, a fake HTTP server)"""
44

5+
from __future__ import annotations
6+
57
import argparse
68
import logging
79
import socket

examples/resolve_address.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
"""Example of resolving a name to an IP address."""
44

5+
from __future__ import annotations
6+
57
import asyncio
68
import logging
79
import sys

examples/resolver.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
"""Example of resolving a service with a known name"""
44

5+
from __future__ import annotations
6+
57
import logging
68
import sys
79

examples/self_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env python
2+
from __future__ import annotations
23

34
import logging
45
import socket

src/zeroconf/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
USA
2121
"""
2222

23+
from __future__ import annotations
24+
2325
from ._cache import DNSCache # noqa # import needed for backwards compat
2426
from ._core import Zeroconf
2527
from ._dns import ( # noqa # import needed for backwards compat
@@ -57,10 +59,10 @@
5759
)
5860
from ._services.browser import ServiceBrowser
5961
from ._services.info import ( # noqa # import needed for backwards compat
60-
ServiceInfo,
6162
AddressResolver,
6263
AddressResolverIPv4,
6364
AddressResolverIPv6,
65+
ServiceInfo,
6466
instance_name_from_service_info,
6567
)
6668
from ._services.registry import ( # noqa # import needed for backwards compat

src/zeroconf/_cache.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
USA
2121
"""
2222

23+
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
2527

2628
from ._dns import (
2729
DNSAddress,
@@ -66,8 +68,8 @@ class DNSCache:
6668

6769
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 = {}
7274

7375
# 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)
137139

138-
def async_expire(self, now: _float) -> List[DNSRecord]:
140+
def async_expire(self, now: _float) -> list[DNSRecord]:
139141
"""Purge expired entries from the cache.
140142
141143
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 []
147149

148-
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
184186

185-
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)
196198

197-
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.
199201
200202
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
212214

213-
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.
215217
216218
This function is not threadsafe and must be called from
217219
the event loop.
218220
"""
219221
return self.entries_with_name(name)
220222

221-
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.
223225
224226
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.
232234

233-
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
242244

243-
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.
245247
246248
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
263265

264-
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_]
271273

272-
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 []
277279

278-
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 []
283285

284-
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
294296

295-
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)
298300

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

0 commit comments

Comments
 (0)