GeezerVison is showing github.com — feat: migrate to native types (#1472) · python-zeroconf/python-zeroconf@22a0fb4
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: bench/incoming.py +1-2Lines changed: 1 addition & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 2 | 2 |
| 3 | 3 | import socket | 4 | 4 | import timeit | 5 | | -from typing import List | 6 | 5 |
| 7 | 6 | from zeroconf import ( | 8 | 7 | DNSAddress, | | 15 | 14 | ) | 16 | 15 |
| 17 | 16 |
| 18 | | -def generate_packets() -> List[bytes]: | | 17 | +def generate_packets() -> list[bytes]: | 19 | 18 | out = DNSOutgoing(const._FLAGS_QR_RESPONSE | const._FLAGS_AA) | 20 | 19 | address = socket.inet_pton(socket.AF_INET, "192.168.208.5") | 21 | 20 |
| |
Collapse file Copy file name to clipboardExpand all lines: docs/conf.py +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 7 | 7 | # All configuration values have a default; values that are commented out | 8 | 8 | # serve to show the default. | 9 | 9 |
| 10 | | -from typing import Any, Dict | | 10 | +from typing import Any | 11 | 11 |
| 12 | 12 | import zeroconf | 13 | 13 |
| | 173 | 173 |
| 174 | 174 | # -- Options for LaTeX output -------------------------------------------------- | 175 | 175 |
| 176 | | -latex_elements: Dict[str, Any] = {} | | 176 | +latex_elements: dict[str, Any] = {} | 177 | 177 |
| 178 | 178 | # Grouping the document tree into LaTeX files. List of tuples | 179 | 179 | # (source start file, target name, title, author, documentclass [howto/manual]). | |
Collapse file Copy file name to clipboardExpand all lines: examples/async_apple_scanner.py +5-3Lines changed: 5 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 2 | 2 |
| 3 | 3 | """Scan for apple devices.""" | 4 | 4 |
| | 5 | +from __future__ import annotations | | 6 | + | 5 | 7 | import argparse | 6 | 8 | import asyncio | 7 | 9 | import logging | 8 | | -from typing import Any, Optional, cast | | 10 | +from typing import Any, cast | 9 | 11 |
| 10 | 12 | from zeroconf import DNSQuestionType, IPVersion, ServiceStateChange, Zeroconf | 11 | 13 | from zeroconf.asyncio import AsyncServiceBrowser, AsyncServiceInfo, AsyncZeroconf | @@ -76,8 +78,8 @@ async def _async_show_service_info(zeroconf: Zeroconf, service_type: str, name: | 76 | 78 | class AsyncAppleScanner: | 77 | 79 | def __init__(self, args: Any) -> None: | 78 | 80 | 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 | 81 | 83 |
| 82 | 84 | async def async_run(self) -> None: | 83 | 85 | self.aiozc = AsyncZeroconf(ip_version=ip_version) | |
Collapse file Copy file name to clipboardExpand all lines: examples/async_browser.py +5-3Lines changed: 5 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 5 | 5 | The default is HTTP and HAP; use --find to search for all available services in the network | 6 | 6 | """ | 7 | 7 |
| | 8 | +from __future__ import annotations | | 9 | + | 8 | 10 | import argparse | 9 | 11 | import asyncio | 10 | 12 | import logging | 11 | | -from typing import Any, Optional, cast | | 13 | +from typing import Any, cast | 12 | 14 |
| 13 | 15 | from zeroconf import IPVersion, ServiceStateChange, Zeroconf | 14 | 16 | from zeroconf.asyncio import ( | @@ -56,8 +58,8 @@ async def async_display_service_info(zeroconf: Zeroconf, service_type: str, name | 56 | 58 | class AsyncRunner: | 57 | 59 | def __init__(self, args: Any) -> None: | 58 | 60 | 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 | 61 | 63 |
| 62 | 64 | async def async_run(self) -> None: | 63 | 65 | self.aiozc = AsyncZeroconf(ip_version=ip_version) | |
Collapse file Copy file name to clipboardExpand all lines: examples/async_registration.py +5-4Lines changed: 5 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 2 | 2 |
| 3 | 3 | """Example of announcing 250 services (in this case, a fake HTTP server).""" | 4 | 4 |
| | 5 | +from __future__ import annotations | | 6 | + | 5 | 7 | import argparse | 6 | 8 | import asyncio | 7 | 9 | import logging | 8 | 10 | import socket | 9 | | -from typing import List, Optional | 10 | 11 |
| 11 | 12 | from zeroconf import IPVersion | 12 | 13 | from zeroconf.asyncio import AsyncServiceInfo, AsyncZeroconf | | 15 | 16 | class AsyncRunner: | 16 | 17 | def __init__(self, ip_version: IPVersion) -> None: | 17 | 18 | self.ip_version = ip_version | 18 | | - self.aiozc: Optional[AsyncZeroconf] = None | | 19 | + self.aiozc: AsyncZeroconf | None = None | 19 | 20 |
| 20 | | - async def register_services(self, infos: List[AsyncServiceInfo]) -> None: | | 21 | + async def register_services(self, infos: list[AsyncServiceInfo]) -> None: | 21 | 22 | self.aiozc = AsyncZeroconf(ip_version=self.ip_version) | 22 | 23 | tasks = [self.aiozc.async_register_service(info) for info in infos] | 23 | 24 | background_tasks = await asyncio.gather(*tasks) | @@ -26,7 +27,7 @@ async def register_services(self, infos: List[AsyncServiceInfo]) -> None: | 26 | 27 | while True: | 27 | 28 | await asyncio.sleep(1) | 28 | 29 |
| 29 | | - async def unregister_services(self, infos: List[AsyncServiceInfo]) -> None: | | 30 | + async def unregister_services(self, infos: list[AsyncServiceInfo]) -> None: | 30 | 31 | assert self.aiozc is not None | 31 | 32 | tasks = [self.aiozc.async_unregister_service(info) for info in infos] | 32 | 33 | background_tasks = await asyncio.gather(*tasks) | |
Collapse file Copy file name to clipboardExpand all lines: examples/async_service_info_request.py +6-4Lines changed: 6 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 7 | 7 |
| 8 | 8 | """ | 9 | 9 |
| | 10 | +from __future__ import annotations | | 11 | + | 10 | 12 | import argparse | 11 | 13 | import asyncio | 12 | 14 | import logging | 13 | | -from typing import Any, List, Optional, cast | | 15 | +from typing import Any, cast | 14 | 16 |
| 15 | 17 | from zeroconf import IPVersion, ServiceBrowser, ServiceStateChange, Zeroconf | 16 | 18 | from zeroconf.asyncio import AsyncServiceInfo, AsyncZeroconf | @@ -22,7 +24,7 @@ async def async_watch_services(aiozc: AsyncZeroconf) -> None: | 22 | 24 | zeroconf = aiozc.zeroconf | 23 | 25 | while True: | 24 | 26 | await asyncio.sleep(5) | 25 | | - infos: List[AsyncServiceInfo] = [] | | 27 | + infos: list[AsyncServiceInfo] = [] | 26 | 28 | for name in zeroconf.cache.names(): | 27 | 29 | if not name.endswith(HAP_TYPE): | 28 | 30 | continue | @@ -50,8 +52,8 @@ async def async_watch_services(aiozc: AsyncZeroconf) -> None: | 50 | 52 | class AsyncRunner: | 51 | 53 | def __init__(self, args: Any) -> None: | 52 | 54 | 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 | 55 | 57 |
| 56 | 58 | async def async_run(self) -> None: | 57 | 59 | self.aiozc = AsyncZeroconf(ip_version=ip_version) | |
Collapse file Copy file name to clipboardExpand all lines: tests/__init__.py +5-4Lines changed: 5 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 20 | 20 | USA | 21 | 21 | """ | 22 | 22 |
| | 23 | +from __future__ import annotations | | 24 | + | 23 | 25 | import asyncio | 24 | 26 | import socket | 25 | 27 | import time | 26 | 28 | from functools import cache | 27 | | -from typing import List, Optional, Set | 28 | 29 | from unittest import mock | 29 | 30 |
| 30 | 31 | import ifaddr | | 36 | 37 |
| 37 | 38 |
| 38 | 39 | 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: | 40 | 41 | return False | 41 | 42 |
| 42 | 43 |
| 43 | | -def _inject_responses(zc: Zeroconf, msgs: List[DNSIncoming]) -> None: | | 44 | +def _inject_responses(zc: Zeroconf, msgs: list[DNSIncoming]) -> None: | 44 | 45 | """Inject a DNSIncoming response.""" | 45 | 46 | assert zc.loop is not None | 46 | 47 |
| @@ -90,7 +91,7 @@ def _clear_cache(zc: Zeroconf) -> None: | 90 | 91 | zc.question_history.clear() | 91 | 92 |
| 92 | 93 |
| 93 | | -def time_changed_millis(millis: Optional[float] = None) -> None: | | 94 | +def time_changed_millis(millis: float | None = None) -> None: | 94 | 95 | """Call all scheduled events for a time.""" | 95 | 96 | loop = asyncio.get_running_loop() | 96 | 97 | loop_time = loop.time() | |
Collapse file Copy file name to clipboardExpand all lines: tests/benchmarks/test_incoming.py +1-2Lines changed: 1 addition & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | """Benchmark for DNSIncoming.""" | 2 | 2 |
| 3 | 3 | import socket | 4 | | -from typing import List | 5 | 4 |
| 6 | 5 | from pytest_codspeed import BenchmarkFixture | 7 | 6 |
| | 16 | 15 | ) | 17 | 16 |
| 18 | 17 |
| 19 | | -def generate_packets() -> List[bytes]: | | 18 | +def generate_packets() -> list[bytes]: | 20 | 19 | out = DNSOutgoing(const._FLAGS_QR_RESPONSE | const._FLAGS_AA) | 21 | 20 | address = socket.inet_pton(socket.AF_INET, "192.168.208.5") | 22 | 21 |
| |
Collapse file Copy file name to clipboardExpand all lines: tests/services/test_browser.py +6-5Lines changed: 6 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 6 | 6 | import socket | 7 | 7 | import time | 8 | 8 | import unittest | | 9 | +from collections.abc import Iterable | 9 | 10 | from threading import Event | 10 | | -from typing import Iterable, List, Set, cast | | 11 | +from typing import cast | 11 | 12 | from unittest.mock import patch | 12 | 13 |
| 13 | 14 | import pytest | @@ -580,7 +581,7 @@ def on_service_state_change(zeroconf, service_type, state_change, name): | 580 | 581 | old_send = zeroconf_browser.async_send | 581 | 582 |
| 582 | 583 | expected_ttl = const._DNS_OTHER_TTL | 583 | | - questions: List[List[DNSQuestion]] = [] | | 584 | + questions: list[list[DNSQuestion]] = [] | 584 | 585 |
| 585 | 586 | def send(out, addr=const._MDNS_ADDR, port=const._MDNS_PORT, v6_flow_scope=()): | 586 | 587 | """Sends an outgoing packet.""" | @@ -1151,7 +1152,7 @@ async def test_generate_service_query_suppress_duplicate_questions(): | 1151 | 1152 | 10000, | 1152 | 1153 | f"known-to-other.{name}", | 1153 | 1154 | ) | 1154 | | - other_known_answers: Set[r.DNSRecord] = {answer} | | 1155 | + other_known_answers: set[r.DNSRecord] = {answer} | 1155 | 1156 | zc.question_history.add_question_at_time(question, now, other_known_answers) | 1156 | 1157 | assert zc.question_history.suppresses(question, now, other_known_answers) | 1157 | 1158 |
| @@ -1196,7 +1197,7 @@ async def test_query_scheduler(): | 1196 | 1197 | aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) | 1197 | 1198 | await aiozc.zeroconf.async_wait_for_start() | 1198 | 1199 | zc = aiozc.zeroconf | 1199 | | - sends: List[r.DNSIncoming] = [] | | 1200 | + sends: list[r.DNSIncoming] = [] | 1200 | 1201 |
| 1201 | 1202 | def send(out, addr=const._MDNS_ADDR, port=const._MDNS_PORT, v6_flow_scope=()): | 1202 | 1203 | """Sends an outgoing packet.""" | @@ -1289,7 +1290,7 @@ async def test_query_scheduler_rescue_records(): | 1289 | 1290 | aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) | 1290 | 1291 | await aiozc.zeroconf.async_wait_for_start() | 1291 | 1292 | zc = aiozc.zeroconf | 1292 | | - sends: List[r.DNSIncoming] = [] | | 1293 | + sends: list[r.DNSIncoming] = [] | 1293 | 1294 |
| 1294 | 1295 | def send(out, addr=const._MDNS_ADDR, port=const._MDNS_PORT, v6_flow_scope=()): | 1295 | 1296 | """Sends an outgoing packet.""" | |
Collapse file Copy file name to clipboardExpand all lines: tests/services/test_info.py +7-5Lines changed: 7 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | """Unit tests for zeroconf._services.info.""" | 2 | 2 |
| | 3 | +from __future__ import annotations | | 4 | + | 3 | 5 | import asyncio | 4 | 6 | import logging | 5 | 7 | import os | 6 | 8 | import socket | 7 | 9 | import threading | 8 | 10 | import unittest | | 11 | +from collections.abc import Iterable | 9 | 12 | from ipaddress import ip_address | 10 | 13 | from threading import Event | 11 | | -from typing import Iterable, List, Optional | 12 | 14 | from unittest.mock import patch | 13 | 15 |
| 14 | 16 | import pytest | @@ -264,7 +266,7 @@ def test_get_info_partial(self): | 264 | 266 | send_event = Event() | 265 | 267 | service_info_event = Event() | 266 | 268 |
| 267 | | - last_sent: Optional[r.DNSOutgoing] = None | | 269 | + last_sent: r.DNSOutgoing | None = None | 268 | 270 |
| 269 | 271 | def send(out, addr=const._MDNS_ADDR, port=const._MDNS_PORT, v6_flow_scope=()): | 270 | 272 | """Sends an outgoing packet.""" | @@ -407,7 +409,7 @@ def test_get_info_suppressed_by_question_history(self): | 407 | 409 | send_event = Event() | 408 | 410 | service_info_event = Event() | 409 | 411 |
| 410 | | - last_sent: Optional[r.DNSOutgoing] = None | | 412 | + last_sent: r.DNSOutgoing | None = None | 411 | 413 |
| 412 | 414 | def send(out, addr=const._MDNS_ADDR, port=const._MDNS_PORT, v6_flow_scope=()): | 413 | 415 | """Sends an outgoing packet.""" | @@ -534,7 +536,7 @@ def test_get_info_single(self): | 534 | 536 | send_event = Event() | 535 | 537 | service_info_event = Event() | 536 | 538 |
| 537 | | - last_sent = None # type: Optional[r.DNSOutgoing] | | 539 | + last_sent: r.DNSOutgoing | None = None | 538 | 540 |
| 539 | 541 | def send(out, addr=const._MDNS_ADDR, port=const._MDNS_PORT, v6_flow_scope=()): | 540 | 542 | """Sends an outgoing packet.""" | @@ -879,7 +881,7 @@ def test_filter_address_by_type_from_service_info(): | 879 | 881 | ipv6 = socket.inet_pton(socket.AF_INET6, "2001:db8::1") | 880 | 882 | info = ServiceInfo(type_, registration_name, 80, 0, 0, desc, "ash-2.local.", addresses=[ipv4, ipv6]) | 881 | 883 |
| 882 | | - def dns_addresses_to_addresses(dns_address: List[DNSAddress]) -> List[bytes]: | | 884 | + def dns_addresses_to_addresses(dns_address: list[DNSAddress]) -> list[bytes]: | 883 | 885 | return [address.address for address in dns_address] | 884 | 886 |
| 885 | 887 | assert dns_addresses_to_addresses(info.dns_addresses()) == [ipv4, ipv6] | |
|