Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions bench/incoming.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import socket
import timeit
from typing import List

from zeroconf import (
DNSAddress,
Expand All @@ -15,7 +14,7 @@
)


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

Expand Down
4 changes: 2 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# All configuration values have a default; values that are commented out
# serve to show the default.

from typing import Any, Dict
from typing import Any

import zeroconf

Expand Down Expand Up @@ -173,7 +173,7 @@

# -- Options for LaTeX output --------------------------------------------------

latex_elements: Dict[str, Any] = {}
latex_elements: dict[str, Any] = {}

# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title, author, documentclass [howto/manual]).
Expand Down
8 changes: 5 additions & 3 deletions examples/async_apple_scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

"""Scan for apple devices."""

from __future__ import annotations

import argparse
import asyncio
import logging
from typing import Any, Optional, cast
from typing import Any, cast

from zeroconf import DNSQuestionType, IPVersion, ServiceStateChange, Zeroconf
from zeroconf.asyncio import AsyncServiceBrowser, AsyncServiceInfo, AsyncZeroconf
Expand Down Expand Up @@ -76,8 +78,8 @@ async def _async_show_service_info(zeroconf: Zeroconf, service_type: str, name:
class AsyncAppleScanner:
def __init__(self, args: Any) -> None:
self.args = args
self.aiobrowser: Optional[AsyncServiceBrowser] = None
self.aiozc: Optional[AsyncZeroconf] = None
self.aiobrowser: AsyncServiceBrowser | None = None
self.aiozc: AsyncZeroconf | None = None

async def async_run(self) -> None:
self.aiozc = AsyncZeroconf(ip_version=ip_version)
Expand Down
8 changes: 5 additions & 3 deletions examples/async_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
The default is HTTP and HAP; use --find to search for all available services in the network
"""

from __future__ import annotations

import argparse
import asyncio
import logging
from typing import Any, Optional, cast
from typing import Any, cast

from zeroconf import IPVersion, ServiceStateChange, Zeroconf
from zeroconf.asyncio import (
Expand Down Expand Up @@ -56,8 +58,8 @@ async def async_display_service_info(zeroconf: Zeroconf, service_type: str, name
class AsyncRunner:
def __init__(self, args: Any) -> None:
self.args = args
self.aiobrowser: Optional[AsyncServiceBrowser] = None
self.aiozc: Optional[AsyncZeroconf] = None
self.aiobrowser: AsyncServiceBrowser | None = None
self.aiozc: AsyncZeroconf | None = None

async def async_run(self) -> None:
self.aiozc = AsyncZeroconf(ip_version=ip_version)
Expand Down
9 changes: 5 additions & 4 deletions examples/async_registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

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

from __future__ import annotations

import argparse
import asyncio
import logging
import socket
from typing import List, Optional

from zeroconf import IPVersion
from zeroconf.asyncio import AsyncServiceInfo, AsyncZeroconf
Expand All @@ -15,9 +16,9 @@
class AsyncRunner:
def __init__(self, ip_version: IPVersion) -> None:
self.ip_version = ip_version
self.aiozc: Optional[AsyncZeroconf] = None
self.aiozc: AsyncZeroconf | None = None

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

async def unregister_services(self, infos: List[AsyncServiceInfo]) -> None:
async def unregister_services(self, infos: list[AsyncServiceInfo]) -> None:
assert self.aiozc is not None
tasks = [self.aiozc.async_unregister_service(info) for info in infos]
background_tasks = await asyncio.gather(*tasks)
Expand Down
10 changes: 6 additions & 4 deletions examples/async_service_info_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@

"""

from __future__ import annotations

import argparse
import asyncio
import logging
from typing import Any, List, Optional, cast
from typing import Any, cast

from zeroconf import IPVersion, ServiceBrowser, ServiceStateChange, Zeroconf
from zeroconf.asyncio import AsyncServiceInfo, AsyncZeroconf
Expand All @@ -22,7 +24,7 @@ async def async_watch_services(aiozc: AsyncZeroconf) -> None:
zeroconf = aiozc.zeroconf
while True:
await asyncio.sleep(5)
infos: List[AsyncServiceInfo] = []
infos: list[AsyncServiceInfo] = []
for name in zeroconf.cache.names():
if not name.endswith(HAP_TYPE):
continue
Expand Down Expand Up @@ -50,8 +52,8 @@ async def async_watch_services(aiozc: AsyncZeroconf) -> None:
class AsyncRunner:
def __init__(self, args: Any) -> None:
self.args = args
self.threaded_browser: Optional[ServiceBrowser] = None
self.aiozc: Optional[AsyncZeroconf] = None
self.threaded_browser: ServiceBrowser | None = None
self.aiozc: AsyncZeroconf | None = None

async def async_run(self) -> None:
self.aiozc = AsyncZeroconf(ip_version=ip_version)
Expand Down
9 changes: 5 additions & 4 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@
USA
"""

from __future__ import annotations

import asyncio
import socket
import time
from functools import cache
from typing import List, Optional, Set
from unittest import mock

import ifaddr
Expand All @@ -36,11 +37,11 @@


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


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

Expand Down Expand Up @@ -90,7 +91,7 @@ def _clear_cache(zc: Zeroconf) -> None:
zc.question_history.clear()


def time_changed_millis(millis: Optional[float] = None) -> None:
def time_changed_millis(millis: float | None = None) -> None:
"""Call all scheduled events for a time."""
loop = asyncio.get_running_loop()
loop_time = loop.time()
Expand Down
3 changes: 1 addition & 2 deletions tests/benchmarks/test_incoming.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Benchmark for DNSIncoming."""

import socket
from typing import List

from pytest_codspeed import BenchmarkFixture

Expand All @@ -16,7 +15,7 @@
)


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

Expand Down
11 changes: 6 additions & 5 deletions tests/services/test_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
import socket
import time
import unittest
from collections.abc import Iterable
from threading import Event
from typing import Iterable, List, Set, cast
from typing import cast
from unittest.mock import patch

import pytest
Expand Down Expand Up @@ -580,7 +581,7 @@ def on_service_state_change(zeroconf, service_type, state_change, name):
old_send = zeroconf_browser.async_send

expected_ttl = const._DNS_OTHER_TTL
questions: List[List[DNSQuestion]] = []
questions: list[list[DNSQuestion]] = []

def send(out, addr=const._MDNS_ADDR, port=const._MDNS_PORT, v6_flow_scope=()):
"""Sends an outgoing packet."""
Expand Down Expand Up @@ -1151,7 +1152,7 @@ async def test_generate_service_query_suppress_duplicate_questions():
10000,
f"known-to-other.{name}",
)
other_known_answers: Set[r.DNSRecord] = {answer}
other_known_answers: set[r.DNSRecord] = {answer}
zc.question_history.add_question_at_time(question, now, other_known_answers)
assert zc.question_history.suppresses(question, now, other_known_answers)

Expand Down Expand Up @@ -1196,7 +1197,7 @@ async def test_query_scheduler():
aiozc = AsyncZeroconf(interfaces=["127.0.0.1"])
await aiozc.zeroconf.async_wait_for_start()
zc = aiozc.zeroconf
sends: List[r.DNSIncoming] = []
sends: list[r.DNSIncoming] = []

def send(out, addr=const._MDNS_ADDR, port=const._MDNS_PORT, v6_flow_scope=()):
"""Sends an outgoing packet."""
Expand Down Expand Up @@ -1289,7 +1290,7 @@ async def test_query_scheduler_rescue_records():
aiozc = AsyncZeroconf(interfaces=["127.0.0.1"])
await aiozc.zeroconf.async_wait_for_start()
zc = aiozc.zeroconf
sends: List[r.DNSIncoming] = []
sends: list[r.DNSIncoming] = []

def send(out, addr=const._MDNS_ADDR, port=const._MDNS_PORT, v6_flow_scope=()):
"""Sends an outgoing packet."""
Expand Down
12 changes: 7 additions & 5 deletions tests/services/test_info.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
"""Unit tests for zeroconf._services.info."""

from __future__ import annotations

import asyncio
import logging
import os
import socket
import threading
import unittest
from collections.abc import Iterable
from ipaddress import ip_address
from threading import Event
from typing import Iterable, List, Optional
from unittest.mock import patch

import pytest
Expand Down Expand Up @@ -264,7 +266,7 @@ def test_get_info_partial(self):
send_event = Event()
service_info_event = Event()

last_sent: Optional[r.DNSOutgoing] = None
last_sent: r.DNSOutgoing | None = None

def send(out, addr=const._MDNS_ADDR, port=const._MDNS_PORT, v6_flow_scope=()):
"""Sends an outgoing packet."""
Expand Down Expand Up @@ -407,7 +409,7 @@ def test_get_info_suppressed_by_question_history(self):
send_event = Event()
service_info_event = Event()

last_sent: Optional[r.DNSOutgoing] = None
last_sent: r.DNSOutgoing | None = None

def send(out, addr=const._MDNS_ADDR, port=const._MDNS_PORT, v6_flow_scope=()):
"""Sends an outgoing packet."""
Expand Down Expand Up @@ -534,7 +536,7 @@ def test_get_info_single(self):
send_event = Event()
service_info_event = Event()

last_sent = None # type: Optional[r.DNSOutgoing]
last_sent: r.DNSOutgoing | None = None

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

def dns_addresses_to_addresses(dns_address: List[DNSAddress]) -> List[bytes]:
def dns_addresses_to_addresses(dns_address: list[DNSAddress]) -> list[bytes]:
return [address.address for address in dns_address]

assert dns_addresses_to_addresses(info.dns_addresses()) == [ipv4, ipv6]
Expand Down
1 change: 0 additions & 1 deletion tests/test_cache.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Unit tests for zeroconf._cache."""

import logging
import unittest
import unittest.mock
from heapq import heapify, heappop

Expand Down
10 changes: 6 additions & 4 deletions tests/test_core.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Unit tests for zeroconf._core"""

from __future__ import annotations

import asyncio
import logging
import os
Expand All @@ -9,7 +11,7 @@
import time
import unittest
import unittest.mock
from typing import Tuple, Union, cast
from typing import cast
from unittest.mock import AsyncMock, Mock, patch

import pytest
Expand Down Expand Up @@ -38,13 +40,13 @@ def teardown_module():


def threadsafe_query(
zc: "Zeroconf",
protocol: "AsyncListener",
zc: Zeroconf,
protocol: AsyncListener,
msg: DNSIncoming,
addr: str,
port: int,
transport: _WrappedTransport,
v6_flow_scope: Union[Tuple[()], Tuple[int, int]],
v6_flow_scope: tuple[()] | tuple[int, int],
) -> None:
async def make_query():
protocol.handle_query_or_defer(msg, addr, port, transport, v6_flow_scope)
Expand Down
1 change: 0 additions & 1 deletion tests/test_dns.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import logging
import os
import socket
import unittest
import unittest.mock

import pytest
Expand Down
3 changes: 1 addition & 2 deletions tests/test_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import asyncio
import itertools
import logging
from typing import Set
from unittest.mock import patch

import pytest
Expand Down Expand Up @@ -41,7 +40,7 @@ async def test_reaper():
zeroconf.cache.async_add_records([record_with_10s_ttl, record_with_1s_ttl])
question = r.DNSQuestion("_hap._tcp._local.", const._TYPE_PTR, const._CLASS_IN)
now = r.current_time_millis()
other_known_answers: Set[r.DNSRecord] = {
other_known_answers: set[r.DNSRecord] = {
r.DNSPointer(
"_hap._tcp.local.",
const._TYPE_PTR,
Expand Down
1 change: 0 additions & 1 deletion tests/test_exceptions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Unit tests for zeroconf._exceptions"""

import logging
import unittest
import unittest.mock

import zeroconf as r
Expand Down
Loading