Skip to content

Commit 4218d75

Browse files
scopjstasiak
authored andcommitted
Turn on and address mypy check_untyped_defs
1 parent 006e614 commit 4218d75

3 files changed

Lines changed: 86 additions & 72 deletions

File tree

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ max-line-length=110
66
[mypy]
77
ignore_missing_imports = true
88
follow_imports = error
9+
check_untyped_defs = true
910
no_implicit_optional = true
1011
warn_incomplete_stub = true
1112
warn_no_return = true

test_zeroconf.py

Lines changed: 53 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import time
1212
import unittest
1313
from threading import Event
14+
from typing import Dict, Optional # noqa # used in type hints
15+
from typing import cast
1416

1517

1618
import zeroconf as r
@@ -25,16 +27,18 @@
2527
)
2628

2729
log = logging.getLogger('zeroconf')
28-
original_logging_level = [None]
30+
original_logging_level = logging.NOTSET
2931

3032

3133
def setup_module():
32-
original_logging_level[0] = log.level
34+
global original_logging_level
35+
original_logging_level = log.level
3336
log.setLevel(logging.DEBUG)
3437

3538

3639
def teardown_module():
37-
log.setLevel(original_logging_level[0])
40+
if original_logging_level != logging.NOTSET:
41+
log.setLevel(original_logging_level)
3842

3943

4044
class TestDunder(unittest.TestCase):
@@ -192,8 +196,9 @@ def test_dns_hinfo(self):
192196
generated.add_additional_answer(
193197
DNSHinfo('irrelevant', r._TYPE_HINFO, 0, 0, 'cpu', 'os'))
194198
parsed = r.DNSIncoming(generated.packet())
195-
self.assertEqual(parsed.answers[0].cpu, u'cpu')
196-
self.assertEqual(parsed.answers[0].os, u'os')
199+
answer = cast(r.DNSHinfo, parsed.answers[0])
200+
self.assertEqual(answer.cpu, u'cpu')
201+
self.assertEqual(answer.os, u'os')
197202

198203
generated = r.DNSOutgoing(0)
199204
generated.add_additional_answer(
@@ -294,19 +299,20 @@ def test_lots_of_names(self):
294299
# we are going to monkey patch the zeroconf send to check packet sizes
295300
old_send = zc.send
296301

297-
# needs to be a list so that we can modify it in our phony send
298-
longest_packet = [0, None]
302+
longest_packet_len = 0
303+
longest_packet = None # type: Optional[r.DNSOutgoing]
299304

300305
def send(out, addr=r._MDNS_ADDR, port=r._MDNS_PORT):
301306
"""Sends an outgoing packet."""
302307
packet = out.packet()
303-
if longest_packet[0] < len(packet):
304-
longest_packet[0] = len(packet)
305-
longest_packet[1] = out
308+
nonlocal longest_packet_len, longest_packet
309+
if longest_packet_len < len(packet):
310+
longest_packet_len = len(packet)
311+
longest_packet = out
306312
old_send(out, addr=addr, port=port)
307313

308314
# monkey patch the zeroconf send
309-
zc.send = send
315+
setattr(zc, "send", send)
310316

311317
# dummy service callback
312318
def on_service_state_change(zeroconf, service_type, state_change, name):
@@ -318,7 +324,7 @@ def on_service_state_change(zeroconf, service_type, state_change, name):
318324
# wait until the browse request packet has maxed out in size
319325
sleep_count = 0
320326
while sleep_count < 100 and \
321-
longest_packet[0] < r._MAX_MSG_ABSOLUTE - 100:
327+
longest_packet_len < r._MAX_MSG_ABSOLUTE - 100:
322328
sleep_count += 1
323329
time.sleep(0.1)
324330

@@ -327,11 +333,11 @@ def on_service_state_change(zeroconf, service_type, state_change, name):
327333

328334
import zeroconf
329335
zeroconf.log.debug('sleep_count %d, sized %d',
330-
sleep_count, longest_packet[0])
336+
sleep_count, longest_packet_len)
331337

332338
# now the browser has sent at least one request, verify the size
333-
assert longest_packet[0] <= r._MAX_MSG_ABSOLUTE
334-
assert longest_packet[0] >= r._MAX_MSG_ABSOLUTE - 100
339+
assert longest_packet_len <= r._MAX_MSG_ABSOLUTE
340+
assert longest_packet_len >= r._MAX_MSG_ABSOLUTE - 100
335341

336342
# mock zeroconf's logger warning() and debug()
337343
from unittest.mock import patch
@@ -342,7 +348,8 @@ def on_service_state_change(zeroconf, service_type, state_change, name):
342348

343349
# now that we have a long packet in our possession, let's verify the
344350
# exception handling.
345-
out = longest_packet[1]
351+
out = longest_packet
352+
assert out is not None
346353
out.data.append(b'\0' * 1000)
347354

348355
# mock the zeroconf logger and check for the correct logging backoff
@@ -582,71 +589,69 @@ def test_ttl(self):
582589
# we are going to monkey patch the zeroconf send to check packet sizes
583590
old_send = zc.send
584591

585-
# needs to be a list so that we can modify it in our phony send
586-
nbr_answers = [0, None]
587-
nbr_additionals = [0, None]
588-
nbr_authorities = [0, None]
592+
nbr_answers = nbr_additionals = nbr_authorities = 0
589593

590594
def send(out, addr=r._MDNS_ADDR, port=r._MDNS_PORT):
591595
"""Sends an outgoing packet."""
596+
nonlocal nbr_answers, nbr_additionals, nbr_authorities
592597
for answer, time_ in out.answers:
593-
nbr_answers[0] += 1
598+
nbr_answers += 1
594599
assert answer.ttl == expected_ttl
595600
for answer in out.additionals:
596-
nbr_additionals[0] += 1
601+
nbr_additionals += 1
597602
assert answer.ttl == expected_ttl
598603
for answer in out.authorities:
599-
nbr_authorities[0] += 1
604+
nbr_authorities += 1
600605
assert answer.ttl == expected_ttl
601606
old_send(out, addr=addr, port=port)
602607

603608
# monkey patch the zeroconf send
604-
zc.send = send
609+
setattr(zc, "send", send)
605610

606611
# register service with default TTL
607612
expected_ttl = r._DNS_TTL
608613
zc.register_service(info)
609-
assert nbr_answers[0] == 12 and nbr_additionals[0] == 0 and nbr_authorities[0] == 3
610-
nbr_answers[0] = nbr_additionals[0] = nbr_authorities[0] = 0
614+
assert nbr_answers == 12 and nbr_additionals == 0 and nbr_authorities == 3
615+
nbr_answers = nbr_additionals = nbr_authorities = 0
611616

612617
# query
613618
query = r.DNSOutgoing(r._FLAGS_QR_QUERY | r._FLAGS_AA)
614619
query.add_question(r.DNSQuestion(info.type, r._TYPE_PTR, r._CLASS_IN))
615620
query.add_question(r.DNSQuestion(info.name, r._TYPE_SRV, r._CLASS_IN))
616621
query.add_question(r.DNSQuestion(info.name, r._TYPE_TXT, r._CLASS_IN))
617622
query.add_question(r.DNSQuestion(info.server, r._TYPE_A, r._CLASS_IN))
618-
zc.handle_query(query, r._MDNS_ADDR, r._MDNS_PORT)
619-
assert nbr_answers[0] == 4 and nbr_additionals[0] == 1 and nbr_authorities[0] == 0
620-
nbr_answers[0] = nbr_additionals[0] = nbr_authorities[0] = 0
623+
zc.handle_query(r.DNSIncoming(query.packet()), r._MDNS_ADDR, r._MDNS_PORT)
624+
assert nbr_answers == 4 and nbr_additionals == 1 and nbr_authorities == 0
625+
nbr_answers = nbr_additionals = nbr_authorities = 0
621626

622627
# unregister
623628
expected_ttl = 0
624629
zc.unregister_service(info)
625-
assert nbr_answers[0] == 12 and nbr_additionals[0] == 0 and nbr_authorities[0] == 0
626-
nbr_answers[0] = nbr_additionals[0] = nbr_authorities[0] = 0
630+
assert nbr_answers == 12 and nbr_additionals == 0 and nbr_authorities == 0
631+
nbr_answers = nbr_additionals = nbr_authorities = 0
627632

628633
# register service with custom TTL
629634
expected_ttl = r._DNS_TTL * 2
630635
assert expected_ttl != r._DNS_TTL
631636
zc.register_service(info, ttl=expected_ttl)
632-
assert nbr_answers[0] == 12 and nbr_additionals[0] == 0 and nbr_authorities[0] == 3
633-
nbr_answers[0] = nbr_additionals[0] = nbr_authorities[0] = 0
637+
assert nbr_answers == 12 and nbr_additionals == 0 and nbr_authorities == 3
638+
nbr_answers = nbr_additionals = nbr_authorities = 0
634639

635640
# query
636641
query = r.DNSOutgoing(r._FLAGS_QR_QUERY | r._FLAGS_AA)
637642
query.add_question(r.DNSQuestion(info.type, r._TYPE_PTR, r._CLASS_IN))
638643
query.add_question(r.DNSQuestion(info.name, r._TYPE_SRV, r._CLASS_IN))
639644
query.add_question(r.DNSQuestion(info.name, r._TYPE_TXT, r._CLASS_IN))
640645
query.add_question(r.DNSQuestion(info.server, r._TYPE_A, r._CLASS_IN))
641-
zc.handle_query(query, r._MDNS_ADDR, r._MDNS_PORT)
642-
assert nbr_answers[0] == 4 and nbr_additionals[0] == 1 and nbr_authorities[0] == 0
643-
nbr_answers[0] = nbr_additionals[0] = nbr_authorities[0] = 0
646+
zc.handle_query(r.DNSIncoming(query.packet()), r._MDNS_ADDR, r._MDNS_PORT)
647+
assert nbr_answers == 4 and nbr_additionals == 1 and nbr_authorities == 0
648+
nbr_answers = nbr_additionals = nbr_authorities = 0
644649

645650
# unregister
646651
expected_ttl = 0
647652
zc.unregister_service(info)
648-
assert nbr_answers[0] == 12 and nbr_additionals[0] == 0 and nbr_authorities[0] == 0
649-
nbr_answers[0] = nbr_additionals[0] = nbr_authorities[0] = 0
653+
assert nbr_answers == 12 and nbr_additionals == 0 and nbr_authorities == 0
654+
nbr_answers = nbr_additionals = nbr_authorities = 0
650655

651656

652657
class TestDNSCache(unittest.TestCase):
@@ -730,7 +735,7 @@ def test_integration_with_listener_class(self):
730735
name = "xxxyyyæøå"
731736
registration_name = "%s.%s" % (name, type_)
732737

733-
class MyListener:
738+
class MyListener(r.ServiceListener):
734739
def add_service(self, zeroconf, type, name):
735740
zeroconf.get_service_info(type, name)
736741
service_added.set()
@@ -752,7 +757,7 @@ def remove_service(self, zeroconf, type, name):
752757
)
753758

754759
zeroconf_registrar = Zeroconf(interfaces=['127.0.0.1'])
755-
desc = {'path': '/~paulsm/'}
760+
desc = {'path': '/~paulsm/'} # type: r.ServicePropertiesType
756761
desc.update(properties)
757762
info_service = ServiceInfo(
758763
subtype, registration_name,
@@ -773,7 +778,7 @@ def remove_service(self, zeroconf, type, name):
773778

774779
# get service info without answer cache
775780
info = zeroconf_browser.get_service_info(type_, registration_name)
776-
781+
assert info is not None
777782
assert info.properties[b'prop_none'] is False
778783
assert info.properties[b'prop_string'] == properties['prop_string']
779784
assert info.properties[b'prop_float'] is False
@@ -782,6 +787,7 @@ def remove_service(self, zeroconf, type, name):
782787
assert info.properties[b'prop_false'] is False
783788

784789
info = zeroconf_browser.get_service_info(subtype, registration_name)
790+
assert info is not None
785791
assert info.properties[b'prop_none'] is False
786792

787793
zeroconf_registrar.unregister_service(info_service)
@@ -814,31 +820,30 @@ def on_service_state_change(zeroconf, service_type, state_change, name):
814820
# we are going to monkey patch the zeroconf send to check packet sizes
815821
old_send = zeroconf_browser.send
816822

817-
time_offset = 0
823+
time_offset = 0.0
818824

819825
def current_time_millis():
820826
"""Current system time in milliseconds"""
821827
return time.time() * 1000 + time_offset * 1000
822828

823829
expected_ttl = r._DNS_TTL
824830

825-
# needs to be a list so that we can modify it in our phony send
826-
nbr_queries = [0, None]
831+
nbr_queries = 0
827832

828833
def send(out, addr=r._MDNS_ADDR, port=r._MDNS_PORT):
829834
"""Sends an outgoing packet."""
830835
pout = r.DNSIncoming(out.packet())
831-
836+
nonlocal nbr_queries
832837
for answer in pout.answers:
833-
nbr_queries[0] += 1
838+
nbr_queries += 1
834839
if not answer.ttl > expected_ttl / 2:
835840
unexpected_ttl.set()
836841

837842
got_query.set()
838843
old_send(out, addr=addr, port=port)
839844

840845
# monkey patch the zeroconf send
841-
zeroconf_browser.send = send
846+
setattr(zeroconf_browser, "send", send)
842847

843848
# monkey patch the zeroconf current_time_millis
844849
r.current_time_millis = current_time_millis
@@ -861,7 +866,7 @@ def send(out, addr=r._MDNS_ADDR, port=r._MDNS_PORT):
861866
assert service_added.is_set()
862867

863868
sleep_count = 0
864-
while nbr_queries[0] < 50:
869+
while nbr_queries < 50:
865870
time_offset += expected_ttl / 4
866871
zeroconf_browser.notify_all()
867872
sleep_count += 1

0 commit comments

Comments
 (0)