Skip to content

Commit 22a0fb4

Browse files
Rotzbuabdracopre-commit-ci[bot]
authored
feat: migrate to native types (#1472)
Co-authored-by: J. Nick Koston <nick@koston.org> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent dde26c6 commit 22a0fb4

22 files changed

Lines changed: 66 additions & 61 deletions

bench/incoming.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import socket
44
import timeit
5-
from typing import List
65

76
from zeroconf import (
87
DNSAddress,
@@ -15,7 +14,7 @@
1514
)
1615

1716

18-
def generate_packets() -> List[bytes]:
17+
def generate_packets() -> list[bytes]:
1918
out = DNSOutgoing(const._FLAGS_QR_RESPONSE | const._FLAGS_AA)
2019
address = socket.inet_pton(socket.AF_INET, "192.168.208.5")
2120

docs/conf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# All configuration values have a default; values that are commented out
88
# serve to show the default.
99

10-
from typing import Any, Dict
10+
from typing import Any
1111

1212
import zeroconf
1313

@@ -173,7 +173,7 @@
173173

174174
# -- Options for LaTeX output --------------------------------------------------
175175

176-
latex_elements: Dict[str, Any] = {}
176+
latex_elements: dict[str, Any] = {}
177177

178178
# Grouping the document tree into LaTeX files. List of tuples
179179
# (source start file, target name, title, author, documentclass [howto/manual]).

examples/async_apple_scanner.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
"""Scan for apple devices."""
44

5+
from __future__ import annotations
6+
57
import argparse
68
import asyncio
79
import logging
8-
from typing import Any, Optional, cast
10+
from typing import Any, cast
911

1012
from zeroconf import DNSQuestionType, IPVersion, ServiceStateChange, Zeroconf
1113
from zeroconf.asyncio import AsyncServiceBrowser, AsyncServiceInfo, AsyncZeroconf
@@ -76,8 +78,8 @@ async def _async_show_service_info(zeroconf: Zeroconf, service_type: str, name:
7678
class AsyncAppleScanner:
7779
def __init__(self, args: Any) -> None:
7880
self.args = args
79-
self.aiobrowser: Optional[AsyncServiceBrowser] = None
80-
self.aiozc: Optional[AsyncZeroconf] = None
81+
self.aiobrowser: AsyncServiceBrowser | None = None
82+
self.aiozc: AsyncZeroconf | None = None
8183

8284
async def async_run(self) -> None:
8385
self.aiozc = AsyncZeroconf(ip_version=ip_version)

examples/async_browser.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
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 asyncio
1012
import logging
11-
from typing import Any, Optional, cast
13+
from typing import Any, cast
1214

1315
from zeroconf import IPVersion, ServiceStateChange, Zeroconf
1416
from zeroconf.asyncio import (
@@ -56,8 +58,8 @@ async def async_display_service_info(zeroconf: Zeroconf, service_type: str, name
5658
class AsyncRunner:
5759
def __init__(self, args: Any) -> None:
5860
self.args = args
59-
self.aiobrowser: Optional[AsyncServiceBrowser] = None
60-
self.aiozc: Optional[AsyncZeroconf] = None
61+
self.aiobrowser: AsyncServiceBrowser | None = None
62+
self.aiozc: AsyncZeroconf | None = None
6163

6264
async def async_run(self) -> None:
6365
self.aiozc = AsyncZeroconf(ip_version=ip_version)

examples/async_registration.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

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

5+
from __future__ import annotations
6+
57
import argparse
68
import asyncio
79
import logging
810
import socket
9-
from typing import List, Optional
1011

1112
from zeroconf import IPVersion
1213
from zeroconf.asyncio import AsyncServiceInfo, AsyncZeroconf
@@ -15,9 +16,9 @@
1516
class AsyncRunner:
1617
def __init__(self, ip_version: IPVersion) -> None:
1718
self.ip_version = ip_version
18-
self.aiozc: Optional[AsyncZeroconf] = None
19+
self.aiozc: AsyncZeroconf | None = None
1920

20-
async def register_services(self, infos: List[AsyncServiceInfo]) -> None:
21+
async def register_services(self, infos: list[AsyncServiceInfo]) -> None:
2122
self.aiozc = AsyncZeroconf(ip_version=self.ip_version)
2223
tasks = [self.aiozc.async_register_service(info) for info in infos]
2324
background_tasks = await asyncio.gather(*tasks)
@@ -26,7 +27,7 @@ async def register_services(self, infos: List[AsyncServiceInfo]) -> None:
2627
while True:
2728
await asyncio.sleep(1)
2829

29-
async def unregister_services(self, infos: List[AsyncServiceInfo]) -> None:
30+
async def unregister_services(self, infos: list[AsyncServiceInfo]) -> None:
3031
assert self.aiozc is not None
3132
tasks = [self.aiozc.async_unregister_service(info) for info in infos]
3233
background_tasks = await asyncio.gather(*tasks)

examples/async_service_info_request.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
88
"""
99

10+
from __future__ import annotations
11+
1012
import argparse
1113
import asyncio
1214
import logging
13-
from typing import Any, List, Optional, cast
15+
from typing import Any, cast
1416

1517
from zeroconf import IPVersion, ServiceBrowser, ServiceStateChange, Zeroconf
1618
from zeroconf.asyncio import AsyncServiceInfo, AsyncZeroconf
@@ -22,7 +24,7 @@ async def async_watch_services(aiozc: AsyncZeroconf) -> None:
2224
zeroconf = aiozc.zeroconf
2325
while True:
2426
await asyncio.sleep(5)
25-
infos: List[AsyncServiceInfo] = []
27+
infos: list[AsyncServiceInfo] = []
2628
for name in zeroconf.cache.names():
2729
if not name.endswith(HAP_TYPE):
2830
continue
@@ -50,8 +52,8 @@ async def async_watch_services(aiozc: AsyncZeroconf) -> None:
5052
class AsyncRunner:
5153
def __init__(self, args: Any) -> None:
5254
self.args = args
53-
self.threaded_browser: Optional[ServiceBrowser] = None
54-
self.aiozc: Optional[AsyncZeroconf] = None
55+
self.threaded_browser: ServiceBrowser | None = None
56+
self.aiozc: AsyncZeroconf | None = None
5557

5658
async def async_run(self) -> None:
5759
self.aiozc = AsyncZeroconf(ip_version=ip_version)

tests/__init__.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@
2020
USA
2121
"""
2222

23+
from __future__ import annotations
24+
2325
import asyncio
2426
import socket
2527
import time
2628
from functools import cache
27-
from typing import List, Optional, Set
2829
from unittest import mock
2930

3031
import ifaddr
@@ -36,11 +37,11 @@
3637

3738

3839
class QuestionHistoryWithoutSuppression(QuestionHistory):
39-
def suppresses(self, question: DNSQuestion, now: float, known_answers: Set[DNSRecord]) -> bool:
40+
def suppresses(self, question: DNSQuestion, now: float, known_answers: set[DNSRecord]) -> bool:
4041
return False
4142

4243

43-
def _inject_responses(zc: Zeroconf, msgs: List[DNSIncoming]) -> None:
44+
def _inject_responses(zc: Zeroconf, msgs: list[DNSIncoming]) -> None:
4445
"""Inject a DNSIncoming response."""
4546
assert zc.loop is not None
4647

@@ -90,7 +91,7 @@ def _clear_cache(zc: Zeroconf) -> None:
9091
zc.question_history.clear()
9192

9293

93-
def time_changed_millis(millis: Optional[float] = None) -> None:
94+
def time_changed_millis(millis: float | None = None) -> None:
9495
"""Call all scheduled events for a time."""
9596
loop = asyncio.get_running_loop()
9697
loop_time = loop.time()

tests/benchmarks/test_incoming.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""Benchmark for DNSIncoming."""
22

33
import socket
4-
from typing import List
54

65
from pytest_codspeed import BenchmarkFixture
76

@@ -16,7 +15,7 @@
1615
)
1716

1817

19-
def generate_packets() -> List[bytes]:
18+
def generate_packets() -> list[bytes]:
2019
out = DNSOutgoing(const._FLAGS_QR_RESPONSE | const._FLAGS_AA)
2120
address = socket.inet_pton(socket.AF_INET, "192.168.208.5")
2221

tests/services/test_browser.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
import socket
77
import time
88
import unittest
9+
from collections.abc import Iterable
910
from threading import Event
10-
from typing import Iterable, List, Set, cast
11+
from typing import cast
1112
from unittest.mock import patch
1213

1314
import pytest
@@ -580,7 +581,7 @@ def on_service_state_change(zeroconf, service_type, state_change, name):
580581
old_send = zeroconf_browser.async_send
581582

582583
expected_ttl = const._DNS_OTHER_TTL
583-
questions: List[List[DNSQuestion]] = []
584+
questions: list[list[DNSQuestion]] = []
584585

585586
def send(out, addr=const._MDNS_ADDR, port=const._MDNS_PORT, v6_flow_scope=()):
586587
"""Sends an outgoing packet."""
@@ -1151,7 +1152,7 @@ async def test_generate_service_query_suppress_duplicate_questions():
11511152
10000,
11521153
f"known-to-other.{name}",
11531154
)
1154-
other_known_answers: Set[r.DNSRecord] = {answer}
1155+
other_known_answers: set[r.DNSRecord] = {answer}
11551156
zc.question_history.add_question_at_time(question, now, other_known_answers)
11561157
assert zc.question_history.suppresses(question, now, other_known_answers)
11571158

@@ -1196,7 +1197,7 @@ async def test_query_scheduler():
11961197
aiozc = AsyncZeroconf(interfaces=["127.0.0.1"])
11971198
await aiozc.zeroconf.async_wait_for_start()
11981199
zc = aiozc.zeroconf
1199-
sends: List[r.DNSIncoming] = []
1200+
sends: list[r.DNSIncoming] = []
12001201

12011202
def send(out, addr=const._MDNS_ADDR, port=const._MDNS_PORT, v6_flow_scope=()):
12021203
"""Sends an outgoing packet."""
@@ -1289,7 +1290,7 @@ async def test_query_scheduler_rescue_records():
12891290
aiozc = AsyncZeroconf(interfaces=["127.0.0.1"])
12901291
await aiozc.zeroconf.async_wait_for_start()
12911292
zc = aiozc.zeroconf
1292-
sends: List[r.DNSIncoming] = []
1293+
sends: list[r.DNSIncoming] = []
12931294

12941295
def send(out, addr=const._MDNS_ADDR, port=const._MDNS_PORT, v6_flow_scope=()):
12951296
"""Sends an outgoing packet."""

tests/services/test_info.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
"""Unit tests for zeroconf._services.info."""
22

3+
from __future__ import annotations
4+
35
import asyncio
46
import logging
57
import os
68
import socket
79
import threading
810
import unittest
11+
from collections.abc import Iterable
912
from ipaddress import ip_address
1013
from threading import Event
11-
from typing import Iterable, List, Optional
1214
from unittest.mock import patch
1315

1416
import pytest
@@ -264,7 +266,7 @@ def test_get_info_partial(self):
264266
send_event = Event()
265267
service_info_event = Event()
266268

267-
last_sent: Optional[r.DNSOutgoing] = None
269+
last_sent: r.DNSOutgoing | None = None
268270

269271
def send(out, addr=const._MDNS_ADDR, port=const._MDNS_PORT, v6_flow_scope=()):
270272
"""Sends an outgoing packet."""
@@ -407,7 +409,7 @@ def test_get_info_suppressed_by_question_history(self):
407409
send_event = Event()
408410
service_info_event = Event()
409411

410-
last_sent: Optional[r.DNSOutgoing] = None
412+
last_sent: r.DNSOutgoing | None = None
411413

412414
def send(out, addr=const._MDNS_ADDR, port=const._MDNS_PORT, v6_flow_scope=()):
413415
"""Sends an outgoing packet."""
@@ -534,7 +536,7 @@ def test_get_info_single(self):
534536
send_event = Event()
535537
service_info_event = Event()
536538

537-
last_sent = None # type: Optional[r.DNSOutgoing]
539+
last_sent: r.DNSOutgoing | None = None
538540

539541
def send(out, addr=const._MDNS_ADDR, port=const._MDNS_PORT, v6_flow_scope=()):
540542
"""Sends an outgoing packet."""
@@ -879,7 +881,7 @@ def test_filter_address_by_type_from_service_info():
879881
ipv6 = socket.inet_pton(socket.AF_INET6, "2001:db8::1")
880882
info = ServiceInfo(type_, registration_name, 80, 0, 0, desc, "ash-2.local.", addresses=[ipv4, ipv6])
881883

882-
def dns_addresses_to_addresses(dns_address: List[DNSAddress]) -> List[bytes]:
884+
def dns_addresses_to_addresses(dns_address: list[DNSAddress]) -> list[bytes]:
883885
return [address.address for address in dns_address]
884886

885887
assert dns_addresses_to_addresses(info.dns_addresses()) == [ipv4, ipv6]

0 commit comments

Comments
 (0)