Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 468e00c

Browse filesBrowse files
authored
chore: update to modern typing (#1511)
1 parent 3dda6d5 commit 468e00c
Copy full SHA for 468e00c

17 files changed

+79
-64
lines changed

‎.pre-commit-config.yaml

Copy file name to clipboardExpand all lines: .pre-commit-config.yaml
+4-4Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ ci:
99

1010
repos:
1111
- repo: https://github.com/commitizen-tools/commitizen
12-
rev: v4.1.1
12+
rev: v4.2.1
1313
hooks:
1414
- id: commitizen
1515
stages: [commit-msg]
@@ -38,9 +38,9 @@ repos:
3838
rev: v3.19.1
3939
hooks:
4040
- id: pyupgrade
41-
args: [--py38-plus]
41+
args: [--py39-plus]
4242
- repo: https://github.com/astral-sh/ruff-pre-commit
43-
rev: v0.9.4
43+
rev: v0.9.5
4444
hooks:
4545
- id: ruff
4646
args: [--fix, --exit-non-zero-on-fix]
@@ -54,7 +54,7 @@ repos:
5454
hooks:
5555
- id: flake8
5656
- repo: https://github.com/pre-commit/mirrors-mypy
57-
rev: v1.14.1
57+
rev: v1.15.0
5858
hooks:
5959
- id: mypy
6060
additional_dependencies: []

‎pyproject.toml

Copy file name to clipboardExpand all lines: pyproject.toml
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ sphinx = "^7.4.7 || ^8.1.3"
8686
sphinx-rtd-theme = "^3.0.2"
8787

8888
[tool.ruff]
89-
target-version = "py38"
89+
target-version = "py39"
9090
line-length = 110
9191

9292
[tool.ruff.lint]

‎src/zeroconf/_cache.py

Copy file name to clipboardExpand all lines: src/zeroconf/_cache.py
+3-2Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@
2222

2323
from __future__ import annotations
2424

25+
from collections.abc import Iterable
2526
from heapq import heapify, heappop, heappush
26-
from typing import Dict, Iterable, Union, cast
27+
from typing import Union, cast
2728

2829
from ._dns import (
2930
DNSAddress,
@@ -40,7 +41,7 @@
4041

4142
_UNIQUE_RECORD_TYPES = (DNSAddress, DNSHinfo, DNSPointer, DNSText, DNSService)
4243
_UniqueRecordsType = Union[DNSAddress, DNSHinfo, DNSPointer, DNSText, DNSService]
43-
_DNSRecordCacheType = Dict[str, Dict[DNSRecord, DNSRecord]]
44+
_DNSRecordCacheType = dict[str, dict[DNSRecord, DNSRecord]]
4445
_DNSRecord = DNSRecord
4546
_str = str
4647
_float = float

‎src/zeroconf/_core.py

Copy file name to clipboardExpand all lines: src/zeroconf/_core.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
import logging
2727
import sys
2828
import threading
29+
from collections.abc import Awaitable
2930
from types import TracebackType
30-
from typing import Awaitable
3131

3232
from ._cache import DNSCache
3333
from ._dns import DNSQuestion, DNSQuestionType

‎src/zeroconf/_handlers/answers.py

Copy file name to clipboardExpand all lines: src/zeroconf/_handlers/answers.py
+1-2Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,12 @@
2323
from __future__ import annotations
2424

2525
from operator import attrgetter
26-
from typing import Dict, Set
2726

2827
from .._dns import DNSQuestion, DNSRecord
2928
from .._protocol.outgoing import DNSOutgoing
3029
from ..const import _FLAGS_AA, _FLAGS_QR_RESPONSE
3130

32-
_AnswerWithAdditionalsType = Dict[DNSRecord, Set[DNSRecord]]
31+
_AnswerWithAdditionalsType = dict[DNSRecord, set[DNSRecord]]
3332

3433
int_ = int
3534

‎src/zeroconf/_listener.py

Copy file name to clipboardExpand all lines: src/zeroconf/_listener.py
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import logging
2727
import random
2828
from functools import partial
29-
from typing import TYPE_CHECKING, Tuple, cast
29+
from typing import TYPE_CHECKING, cast
3030

3131
from ._logger import QuietLogger, log
3232
from ._protocol.incoming import DNSIncoming
@@ -134,7 +134,7 @@ def _process_datagram_at_time(
134134
addr, port = addrs # type: ignore
135135
addr_port = addrs
136136
if TYPE_CHECKING:
137-
addr_port = cast(Tuple[str, int], addr_port)
137+
addr_port = cast(tuple[str, int], addr_port)
138138
scope = None
139139
else:
140140
# https://github.com/python/mypy/issues/1178

‎src/zeroconf/_protocol/outgoing.py

Copy file name to clipboardExpand all lines: src/zeroconf/_protocol/outgoing.py
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@
2424

2525
import enum
2626
import logging
27+
from collections.abc import Sequence
2728
from struct import Struct
28-
from typing import TYPE_CHECKING, Sequence
29+
from typing import TYPE_CHECKING
2930

3031
from .._dns import DNSPointer, DNSQuestion, DNSRecord
3132
from .._exceptions import NamePartTooLongException

‎src/zeroconf/_services/browser.py

Copy file name to clipboardExpand all lines: src/zeroconf/_services/browser.py
+5-8Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,13 @@
2929
import threading
3030
import time
3131
import warnings
32+
from collections.abc import Iterable
3233
from functools import partial
3334
from types import TracebackType # used in type hints
3435
from typing import (
3536
TYPE_CHECKING,
3637
Any,
3738
Callable,
38-
Dict,
39-
Iterable,
40-
List,
41-
Set,
4239
cast,
4340
)
4441

@@ -96,7 +93,7 @@
9693
bool_ = bool
9794
str_ = str
9895

99-
_QuestionWithKnownAnswers = Dict[DNSQuestion, Set[DNSPointer]]
96+
_QuestionWithKnownAnswers = dict[DNSQuestion, set[DNSPointer]]
10097

10198
heappop = heapq.heappop
10299
heappush = heapq.heappush
@@ -282,7 +279,7 @@ def generate_service_query(
282279
log.debug("Asking %s was suppressed by the question history", question)
283280
continue
284281
if TYPE_CHECKING:
285-
pointer_known_answers = cast(Set[DNSPointer], known_answers)
282+
pointer_known_answers = cast(set[DNSPointer], known_answers)
286283
else:
287284
pointer_known_answers = known_answers
288285
questions_with_known_answers[question] = pointer_known_answers
@@ -618,10 +615,10 @@ def __init__(
618615
self._query_sender_task: asyncio.Task | None = None
619616

620617
if hasattr(handlers, "add_service"):
621-
listener = cast("ServiceListener", handlers)
618+
listener = cast(ServiceListener, handlers)
622619
handlers = None
623620

624-
handlers = cast(List[Callable[..., None]], handlers or [])
621+
handlers = cast(list[Callable[..., None]], handlers or [])
625622

626623
if listener:
627624
handlers.append(_service_state_changed_from_listener(listener))

‎src/zeroconf/_services/info.py

Copy file name to clipboardExpand all lines: src/zeroconf/_services/info.py
+5-5Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
import asyncio
2626
import random
27-
from typing import TYPE_CHECKING, Dict, List, Optional, cast
27+
from typing import TYPE_CHECKING, cast
2828

2929
from .._cache import DNSCache
3030
from .._dns import (
@@ -395,7 +395,7 @@ def _set_properties(self, properties: dict[str | bytes, str | bytes | None]) ->
395395
# as-is, without decoding them, otherwise calling
396396
# self.properties will lazy decode them, which is expensive.
397397
if TYPE_CHECKING:
398-
self._properties = cast("Dict[bytes, Optional[bytes]]", properties)
398+
self._properties = cast(dict[bytes, bytes | None], properties)
399399
else:
400400
self._properties = properties
401401
self.text = result
@@ -462,7 +462,7 @@ def _set_ipv6_addresses_from_cache(self, zc: Zeroconf, now: float_) -> None:
462462
"""Set IPv6 addresses from the cache."""
463463
if TYPE_CHECKING:
464464
self._ipv6_addresses = cast(
465-
"List[ZeroconfIPv6Address]",
465+
list[ZeroconfIPv6Address],
466466
self._get_ip_addresses_from_cache_lifo(zc, now, _TYPE_AAAA),
467467
)
468468
else:
@@ -472,7 +472,7 @@ def _set_ipv4_addresses_from_cache(self, zc: Zeroconf, now: float_) -> None:
472472
"""Set IPv4 addresses from the cache."""
473473
if TYPE_CHECKING:
474474
self._ipv4_addresses = cast(
475-
"List[ZeroconfIPv4Address]",
475+
list[ZeroconfIPv4Address],
476476
self._get_ip_addresses_from_cache_lifo(zc, now, _TYPE_A),
477477
)
478478
else:
@@ -724,7 +724,7 @@ def _get_address_records_from_cache_by_type(self, zc: Zeroconf, _type: int_) ->
724724
cache = zc.cache
725725
if TYPE_CHECKING:
726726
records = cast(
727-
"List[DNSAddress]",
727+
list[DNSAddress],
728728
cache.get_all_by_details(self.server_key, _type, _CLASS_IN),
729729
)
730730
else:

‎src/zeroconf/_utils/asyncio.py

Copy file name to clipboardExpand all lines: src/zeroconf/_utils/asyncio.py
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
import concurrent.futures
2727
import contextlib
2828
import sys
29-
from typing import Any, Awaitable, Coroutine
29+
from collections.abc import Awaitable, Coroutine
30+
from typing import Any
3031

3132
from .._exceptions import EventLoopBlocked
3233
from ..const import _LOADED_SYSTEM_TIMEOUT

‎src/zeroconf/_utils/net.py

Copy file name to clipboardExpand all lines: src/zeroconf/_utils/net.py
+6-5Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
import socket
2929
import struct
3030
import sys
31-
from typing import Any, Sequence, Tuple, Union, cast
31+
from collections.abc import Sequence
32+
from typing import Any, Union, cast
3233

3334
import ifaddr
3435

@@ -42,7 +43,7 @@ class InterfaceChoice(enum.Enum):
4243
All = 2
4344

4445

45-
InterfacesType = Union[Sequence[Union[str, int, Tuple[Tuple[str, int, int], int]]], InterfaceChoice]
46+
InterfacesType = Union[Sequence[Union[str, int, tuple[tuple[str, int, int], int]]], InterfaceChoice]
4647

4748

4849
@enum.unique
@@ -93,7 +94,7 @@ def ip6_to_address_and_index(adapters: list[Any], ip: str) -> tuple[tuple[str, i
9394
# IPv6 addresses are represented as tuples
9495
if isinstance(adapter_ip.ip, tuple) and ipaddress.ip_address(adapter_ip.ip[0]) == ipaddr:
9596
return (
96-
cast(Tuple[str, int, int], adapter_ip.ip),
97+
cast(tuple[str, int, int], adapter_ip.ip),
9798
cast(int, adapter.index),
9899
)
99100

@@ -106,7 +107,7 @@ def interface_index_to_ip6_address(adapters: list[Any], index: int) -> tuple[str
106107
for adapter_ip in adapter.ips:
107108
# IPv6 addresses are represented as tuples
108109
if isinstance(adapter_ip.ip, tuple):
109-
return cast(Tuple[str, int, int], adapter_ip.ip)
110+
return cast(tuple[str, int, int], adapter_ip.ip)
110111

111112
raise RuntimeError(f"No adapter found for index {index}")
112113

@@ -340,7 +341,7 @@ def new_respond_socket(
340341
respond_socket = new_socket(
341342
ip_version=(IPVersion.V6Only if is_v6 else IPVersion.V4Only),
342343
apple_p2p=apple_p2p,
343-
bind_addr=cast(Tuple[Tuple[str, int, int], int], interface)[0] if is_v6 else (cast(str, interface),),
344+
bind_addr=cast(tuple[tuple[str, int, int], int], interface)[0] if is_v6 else (cast(str, interface),),
344345
)
345346
if not respond_socket:
346347
return None

‎src/zeroconf/asyncio.py

Copy file name to clipboardExpand all lines: src/zeroconf/asyncio.py
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@
2424

2525
import asyncio
2626
import contextlib
27+
from collections.abc import Awaitable
2728
from types import TracebackType # used in type hints
28-
from typing import Awaitable, Callable
29+
from typing import Callable
2930

3031
from ._core import Zeroconf
3132
from ._dns import DNSQuestionType

‎tests/conftest.py

Copy file name to clipboardExpand all lines: tests/conftest.py
+5-3Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ def verify_threads_ended():
2323
@pytest.fixture
2424
def run_isolated():
2525
"""Change the mDNS port to run the test in isolation."""
26-
with patch.object(query_handler, "_MDNS_PORT", 5454), patch.object(
27-
_core, "_MDNS_PORT", 5454
28-
), patch.object(const, "_MDNS_PORT", 5454):
26+
with (
27+
patch.object(query_handler, "_MDNS_PORT", 5454),
28+
patch.object(_core, "_MDNS_PORT", 5454),
29+
patch.object(const, "_MDNS_PORT", 5454),
30+
):
2931
yield
3032

3133

‎tests/test_init.py

Copy file name to clipboardExpand all lines: tests/test_init.py
+4-3Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,10 @@ def test_large_packet_exception_log_handling(self):
8989
# instantiate a zeroconf instance
9090
zc = Zeroconf(interfaces=["127.0.0.1"])
9191

92-
with patch("zeroconf._logger.log.warning") as mocked_log_warn, patch(
93-
"zeroconf._logger.log.debug"
94-
) as mocked_log_debug:
92+
with (
93+
patch("zeroconf._logger.log.warning") as mocked_log_warn,
94+
patch("zeroconf._logger.log.debug") as mocked_log_debug,
95+
):
9596
# now that we have a long packet in our possession, let's verify the
9697
# exception handling.
9798
out = r.DNSOutgoing(const._FLAGS_QR_RESPONSE | const._FLAGS_AA)

‎tests/test_listener.py

Copy file name to clipboardExpand all lines: tests/test_listener.py
+3-2Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,9 @@ def test_guard_against_oversized_packets():
5959

6060
try:
6161
# We are patching to generate an oversized packet
62-
with patch.object(outgoing, "_MAX_MSG_ABSOLUTE", 100000), patch.object(
63-
outgoing, "_MAX_MSG_TYPICAL", 100000
62+
with (
63+
patch.object(outgoing, "_MAX_MSG_ABSOLUTE", 100000),
64+
patch.object(outgoing, "_MAX_MSG_TYPICAL", 100000),
6465
):
6566
over_sized_packet = generated.packets()[0]
6667
assert len(over_sized_packet) > const._MAX_MSG_ABSOLUTE

‎tests/test_logger.py

Copy file name to clipboardExpand all lines: tests/test_logger.py
+24-18Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,19 @@ def test_log_warning_once():
2727
"""Test we only log with warning level once."""
2828
QuietLogger._seen_logs = {}
2929
quiet_logger = QuietLogger()
30-
with patch("zeroconf._logger.log.warning") as mock_log_warning, patch(
31-
"zeroconf._logger.log.debug"
32-
) as mock_log_debug:
30+
with (
31+
patch("zeroconf._logger.log.warning") as mock_log_warning,
32+
patch("zeroconf._logger.log.debug") as mock_log_debug,
33+
):
3334
quiet_logger.log_warning_once("the warning")
3435

3536
assert mock_log_warning.mock_calls
3637
assert not mock_log_debug.mock_calls
3738

38-
with patch("zeroconf._logger.log.warning") as mock_log_warning, patch(
39-
"zeroconf._logger.log.debug"
40-
) as mock_log_debug:
39+
with (
40+
patch("zeroconf._logger.log.warning") as mock_log_warning,
41+
patch("zeroconf._logger.log.debug") as mock_log_debug,
42+
):
4143
quiet_logger.log_warning_once("the warning")
4244

4345
assert not mock_log_warning.mock_calls
@@ -48,17 +50,19 @@ def test_log_exception_warning():
4850
"""Test we only log with warning level once."""
4951
QuietLogger._seen_logs = {}
5052
quiet_logger = QuietLogger()
51-
with patch("zeroconf._logger.log.warning") as mock_log_warning, patch(
52-
"zeroconf._logger.log.debug"
53-
) as mock_log_debug:
53+
with (
54+
patch("zeroconf._logger.log.warning") as mock_log_warning,
55+
patch("zeroconf._logger.log.debug") as mock_log_debug,
56+
):
5457
quiet_logger.log_exception_warning("the exception warning")
5558

5659
assert mock_log_warning.mock_calls
5760
assert not mock_log_debug.mock_calls
5861

59-
with patch("zeroconf._logger.log.warning") as mock_log_warning, patch(
60-
"zeroconf._logger.log.debug"
61-
) as mock_log_debug:
62+
with (
63+
patch("zeroconf._logger.log.warning") as mock_log_warning,
64+
patch("zeroconf._logger.log.debug") as mock_log_debug,
65+
):
6266
quiet_logger.log_exception_warning("the exception warning")
6367

6468
assert not mock_log_warning.mock_calls
@@ -85,17 +89,19 @@ def test_log_exception_once():
8589
QuietLogger._seen_logs = {}
8690
quiet_logger = QuietLogger()
8791
exc = Exception()
88-
with patch("zeroconf._logger.log.warning") as mock_log_warning, patch(
89-
"zeroconf._logger.log.debug"
90-
) as mock_log_debug:
92+
with (
93+
patch("zeroconf._logger.log.warning") as mock_log_warning,
94+
patch("zeroconf._logger.log.debug") as mock_log_debug,
95+
):
9196
quiet_logger.log_exception_once(exc, "the exceptional exception warning")
9297

9398
assert mock_log_warning.mock_calls
9499
assert not mock_log_debug.mock_calls
95100

96-
with patch("zeroconf._logger.log.warning") as mock_log_warning, patch(
97-
"zeroconf._logger.log.debug"
98-
) as mock_log_debug:
101+
with (
102+
patch("zeroconf._logger.log.warning") as mock_log_warning,
103+
patch("zeroconf._logger.log.debug") as mock_log_debug,
104+
):
99105
quiet_logger.log_exception_once(exc, "the exceptional exception warning")
100106

101107
assert not mock_log_warning.mock_calls

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.