Skip to content

Commit e429d66

Browse files
authored
chore: add typing, enable additional mypy checks (#1514)
1 parent c9aa899 commit e429d66

File tree

5 files changed

+24
-18
lines changed

5 files changed

+24
-18
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ repos:
5757
rev: v1.15.0
5858
hooks:
5959
- id: mypy
60-
additional_dependencies: []
60+
additional_dependencies: [ifaddr]
6161
- repo: https://github.com/MarcoGorelli/cython-lint
6262
rev: v0.16.6
6363
hooks:

pyproject.toml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,15 +162,21 @@ profile = "black"
162162
known_first_party = ["zeroconf", "tests"]
163163

164164
[tool.mypy]
165+
warn_unused_configs = true
165166
check_untyped_defs = true
166167
disallow_any_generics = false # turn this on when we drop 3.7/3.8 support
167168
disallow_incomplete_defs = true
168169
disallow_untyped_defs = true
170+
warn_incomplete_stub = true
169171
mypy_path = "src/"
170-
no_implicit_optional = true
171172
show_error_codes = true
173+
warn_redundant_casts = false # Activate for cleanup.
174+
warn_return_any = true
172175
warn_unreachable = true
173-
warn_unused_ignores = false
176+
warn_unused_ignores = false # Does not always work properly, activate for cleanup.
177+
extra_checks = true
178+
strict_equality = true
179+
strict_bytes = true # Will be true by default with mypy v2 release.
174180
exclude = [
175181
'docs/*',
176182
'bench/*',

src/zeroconf/_dns.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def _fast_init_entry(self, name: str, type_: _int, class_: _int) -> None:
7979
self.class_ = class_ & _CLASS_MASK
8080
self.unique = (class_ & _CLASS_UNIQUE) != 0
8181

82-
def _dns_entry_matches(self, other) -> bool: # type: ignore[no-untyped-def]
82+
def _dns_entry_matches(self, other: DNSEntry) -> bool:
8383
return self.key == other.key and self.type == other.type and self.class_ == other.class_
8484

8585
def __eq__(self, other: Any) -> bool:
@@ -135,7 +135,7 @@ def __eq__(self, other: Any) -> bool:
135135
@property
136136
def max_size(self) -> int:
137137
"""Maximum size of the question in the packet."""
138-
return len(self.name.encode("utf-8")) + _LEN_BYTE + _LEN_SHORT + _LEN_SHORT # type # class
138+
return len(self.name.encode("utf-8")) + _LEN_BYTE + _LEN_SHORT + _LEN_SHORT
139139

140140
@property
141141
def unicast(self) -> bool:
@@ -199,7 +199,7 @@ def suppressed_by(self, msg: DNSIncoming) -> bool:
199199
return True
200200
return False
201201

202-
def _suppressed_by_answer(self, other) -> bool: # type: ignore[no-untyped-def]
202+
def _suppressed_by_answer(self, other: DNSRecord) -> bool:
203203
"""Returns true if another record has same name, type and class,
204204
and if its TTL is at least half of this record's."""
205205
return self == other and other.ttl > (self.ttl / 2)
@@ -285,7 +285,7 @@ def __eq__(self, other: Any) -> bool:
285285
"""Tests equality on address"""
286286
return isinstance(other, DNSAddress) and self._eq(other)
287287

288-
def _eq(self, other) -> bool: # type: ignore[no-untyped-def]
288+
def _eq(self, other: DNSAddress) -> bool:
289289
return (
290290
self.address == other.address
291291
and self.scope_id == other.scope_id
@@ -344,7 +344,7 @@ def __eq__(self, other: Any) -> bool:
344344
"""Tests equality on cpu and os."""
345345
return isinstance(other, DNSHinfo) and self._eq(other)
346346

347-
def _eq(self, other) -> bool: # type: ignore[no-untyped-def]
347+
def _eq(self, other: DNSHinfo) -> bool:
348348
"""Tests equality on cpu and os."""
349349
return self.cpu == other.cpu and self.os == other.os and self._dns_entry_matches(other)
350350

@@ -399,7 +399,7 @@ def __eq__(self, other: Any) -> bool:
399399
"""Tests equality on alias."""
400400
return isinstance(other, DNSPointer) and self._eq(other)
401401

402-
def _eq(self, other) -> bool: # type: ignore[no-untyped-def]
402+
def _eq(self, other: DNSPointer) -> bool:
403403
"""Tests equality on alias."""
404404
return self.alias_key == other.alias_key and self._dns_entry_matches(other)
405405

@@ -447,7 +447,7 @@ def __eq__(self, other: Any) -> bool:
447447
"""Tests equality on text."""
448448
return isinstance(other, DNSText) and self._eq(other)
449449

450-
def _eq(self, other) -> bool: # type: ignore[no-untyped-def]
450+
def _eq(self, other: DNSText) -> bool:
451451
"""Tests equality on text."""
452452
return self.text == other.text and self._dns_entry_matches(other)
453453

@@ -510,7 +510,7 @@ def __eq__(self, other: Any) -> bool:
510510
"""Tests equality on priority, weight, port and server"""
511511
return isinstance(other, DNSService) and self._eq(other)
512512

513-
def _eq(self, other) -> bool: # type: ignore[no-untyped-def]
513+
def _eq(self, other: DNSService) -> bool:
514514
"""Tests equality on priority, weight, port and server."""
515515
return (
516516
self.priority == other.priority
@@ -585,7 +585,7 @@ def __eq__(self, other: Any) -> bool:
585585
"""Tests equality on next_name and rdtypes."""
586586
return isinstance(other, DNSNsec) and self._eq(other)
587587

588-
def _eq(self, other) -> bool: # type: ignore[no-untyped-def]
588+
def _eq(self, other: DNSNsec) -> bool:
589589
"""Tests equality on next_name and rdtypes."""
590590
return (
591591
self.next_name == other.next_name

src/zeroconf/_listener.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,14 @@ def _process_datagram_at_time(
131131
if len(addrs) == 2:
132132
v6_flow_scope: tuple[()] | tuple[int, int] = ()
133133
# https://github.com/python/mypy/issues/1178
134-
addr, port = addrs # type: ignore
134+
addr, port = addrs
135135
addr_port = addrs
136136
if TYPE_CHECKING:
137137
addr_port = cast(tuple[str, int], addr_port)
138138
scope = None
139139
else:
140140
# https://github.com/python/mypy/issues/1178
141-
addr, port, flow, scope = addrs # type: ignore
141+
addr, port, flow, scope = addrs
142142
if debug: # pragma: no branch
143143
log.debug("IPv6 scope_id %d associated to the receiving interface", scope)
144144
v6_flow_scope = (flow, scope)

src/zeroconf/_utils/net.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,14 @@ def _encode_address(address: str) -> bytes:
7474

7575

7676
def get_all_addresses() -> list[str]:
77-
return list({addr.ip for iface in ifaddr.get_adapters() for addr in iface.ips if addr.is_IPv4})
77+
return list({addr.ip for iface in ifaddr.get_adapters() for addr in iface.ips if addr.is_IPv4}) # type: ignore[misc]
7878

7979

8080
def get_all_addresses_v6() -> list[tuple[tuple[str, int, int], int]]:
8181
# IPv6 multicast uses positive indexes for interfaces
8282
# TODO: What about multi-address interfaces?
8383
return list(
84-
{(addr.ip, iface.index) for iface in ifaddr.get_adapters() for addr in iface.ips if addr.is_IPv6}
84+
{(addr.ip, iface.index) for iface in ifaddr.get_adapters() for addr in iface.ips if addr.is_IPv6} # type: ignore[misc]
8585
)
8686

8787

@@ -127,9 +127,9 @@ def ip6_addresses_to_indexes(
127127

128128
for iface in interfaces:
129129
if isinstance(iface, int):
130-
result.append((interface_index_to_ip6_address(adapters, iface), iface))
130+
result.append((interface_index_to_ip6_address(adapters, iface), iface)) # type: ignore[arg-type]
131131
elif isinstance(iface, str) and ipaddress.ip_address(iface).version == 6:
132-
result.append(ip6_to_address_and_index(adapters, iface))
132+
result.append(ip6_to_address_and_index(adapters, iface)) # type: ignore[arg-type]
133133

134134
return result
135135

0 commit comments

Comments
 (0)