Skip to content

Commit 468e00c

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

17 files changed

Lines changed: 79 additions & 64 deletions

File tree

.pre-commit-config.yaml

Lines 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

Lines 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

Lines 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

Lines 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

Lines 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

Lines 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

Lines 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

Lines 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

Lines 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

Lines 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

0 commit comments

Comments
 (0)