Skip to content

Commit 0578731

Browse files
authored
Upgrade syntax to python 3.6 (#907)
1 parent bc9e9cf commit 0578731

23 files changed

Lines changed: 136 additions & 161 deletions

examples/async_apple_scanner.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
def async_on_service_state_change(
3737
zeroconf: Zeroconf, service_type: str, name: str, state_change: ServiceStateChange
3838
) -> None:
39-
print("Service %s of type %s state changed: %s" % (name, service_type, state_change))
39+
print(f"Service {name} of type {service_type} state changed: {state_change}")
4040
if state_change is not ServiceStateChange.Added:
4141
return
4242
base_name = name[: -len(service_type) - 1]
@@ -55,11 +55,11 @@ async def _async_show_service_info(zeroconf: Zeroconf, service_type: str, name:
5555
print(" Name: %s" % name)
5656
print(" Addresses: %s" % ", ".join(addresses))
5757
print(" Weight: %d, priority: %d" % (info.weight, info.priority))
58-
print(" Server: %s" % (info.server,))
58+
print(f" Server: {info.server}")
5959
if info.properties:
6060
print(" Properties are:")
6161
for key, value in info.properties.items():
62-
print(" %s: %s" % (key, value))
62+
print(f" {key}: {value}")
6363
else:
6464
print(" No properties")
6565
else:

examples/async_browser.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
def async_on_service_state_change(
1818
zeroconf: Zeroconf, service_type: str, name: str, state_change: ServiceStateChange
1919
) -> None:
20-
print("Service %s of type %s state changed: %s" % (name, service_type, state_change))
20+
print(f"Service {name} of type {service_type} state changed: {state_change}")
2121
if state_change is not ServiceStateChange.Added:
2222
return
2323
asyncio.ensure_future(async_display_service_info(zeroconf, service_type, name))
@@ -32,11 +32,11 @@ async def async_display_service_info(zeroconf: Zeroconf, service_type: str, name
3232
print(" Name: %s" % name)
3333
print(" Addresses: %s" % ", ".join(addresses))
3434
print(" Weight: %d, priority: %d" % (info.weight, info.priority))
35-
print(" Server: %s" % (info.server,))
35+
print(f" Server: {info.server}")
3636
if info.properties:
3737
print(" Properties are:")
3838
for key, value in info.properties.items():
39-
print(" %s: %s" % (key, value))
39+
print(f" {key}: {value}")
4040
else:
4141
print(" No properties")
4242
else:

examples/async_service_info_request.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ async def async_watch_services(aiozc: AsyncZeroconf) -> None:
3636
addresses = ["%s:%d" % (addr, cast(int, info.port)) for addr in info.parsed_addresses()]
3737
print(" Addresses: %s" % ", ".join(addresses))
3838
print(" Weight: %d, priority: %d" % (info.weight, info.priority))
39-
print(" Server: %s" % (info.server,))
39+
print(f" Server: {info.server}")
4040
if info.properties:
4141
print(" Properties are:")
4242
for key, value in info.properties.items():
43-
print(" %s: %s" % (key, value))
43+
print(f" {key}: {value}")
4444
else:
4545
print(" No properties")
4646
else:

examples/browser.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
def on_service_state_change(
1717
zeroconf: Zeroconf, service_type: str, name: str, state_change: ServiceStateChange
1818
) -> None:
19-
print("Service %s of type %s state changed: %s" % (name, service_type, state_change))
19+
print(f"Service {name} of type {service_type} state changed: {state_change}")
2020

2121
if state_change is ServiceStateChange.Added:
2222
info = zeroconf.get_service_info(service_type, name)
@@ -26,11 +26,11 @@ def on_service_state_change(
2626
addresses = ["%s:%d" % (addr, cast(int, info.port)) for addr in info.parsed_scoped_addresses()]
2727
print(" Addresses: %s" % ", ".join(addresses))
2828
print(" Weight: %d, priority: %d" % (info.weight, info.priority))
29-
print(" Server: %s" % (info.server,))
29+
print(f" Server: {info.server}")
3030
if info.properties:
3131
print(" Properties are:")
3232
for key, value in info.properties.items():
33-
print(" %s: %s" % (key, value))
33+
print(f" {key}: {value}")
3434
else:
3535
print(" No properties")
3636
else:

examples/self_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
# Test a few module features, including service registration, service
1616
# query (for Zoe), and service unregistration.
17-
print("Multicast DNS Service Discovery for Python, version %s" % (__version__,))
17+
print(f"Multicast DNS Service Discovery for Python, version {__version__}")
1818
r = Zeroconf()
1919
print("1. Testing registration of a service...")
2020
desc = {'version': '0.10', 'a': 'test value', 'b': 'another value'}
@@ -40,7 +40,7 @@
4040
queried_info = r.get_service_info("_http._tcp.local.", "My Service Name._http._tcp.local.")
4141
assert queried_info
4242
assert set(queried_info.parsed_addresses()) == expected
43-
print(" Getting self: %s" % (queried_info,))
43+
print(f" Getting self: {queried_info}")
4444
print(" Query done.")
4545
print("4. Testing unregister of service information...")
4646
r.unregister_service(info)

tests/conftest.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#!/usr/bin/env python
2-
# -*- coding: utf-8 -*-
32

43

54
""" conftest for zeroconf tests. """

tests/test_asyncio.py

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#!/usr/bin/env python
2-
# -*- coding: utf-8 -*-
32

43

54
"""Unit tests for aio.py."""
@@ -57,11 +56,9 @@ def verify_threads_ended():
5756
yield
5857
threads_after = frozenset(threading.enumerate())
5958
non_executor_threads = frozenset(
60-
[
61-
thread
62-
for thread in threads_after
63-
if "asyncio" not in thread.name and "ThreadPoolExecutor" not in thread.name
64-
]
59+
thread
60+
for thread in threads_after
61+
if "asyncio" not in thread.name and "ThreadPoolExecutor" not in thread.name
6562
)
6663
threads = non_executor_threads - threads_before
6764
assert not threads
@@ -119,7 +116,7 @@ async def test_async_service_registration() -> None:
119116
aiozc = AsyncZeroconf(interfaces=['127.0.0.1'])
120117
type_ = "_test1-srvc-type._tcp.local."
121118
name = "xxxyyy"
122-
registration_name = "%s.%s" % (name, type_)
119+
registration_name = f"{name}.{type_}"
123120

124121
calls = []
125122

@@ -179,7 +176,7 @@ async def test_async_service_registration_name_conflict() -> None:
179176
aiozc = AsyncZeroconf(interfaces=['127.0.0.1'])
180177
type_ = "_test-srvc2-type._tcp.local."
181178
name = "xxxyyy"
182-
registration_name = "%s.%s" % (name, type_)
179+
registration_name = f"{name}.{type_}"
183180

184181
desc = {'path': '/~paulsm/'}
185182
info = ServiceInfo(
@@ -227,7 +224,7 @@ async def test_async_service_registration_name_does_not_match_type() -> None:
227224
aiozc = AsyncZeroconf(interfaces=['127.0.0.1'])
228225
type_ = "_test-srvc3-type._tcp.local."
229226
name = "xxxyyy"
230-
registration_name = "%s.%s" % (name, type_)
227+
registration_name = f"{name}.{type_}"
231228

232229
desc = {'path': '/~paulsm/'}
233230
info = ServiceInfo(
@@ -254,7 +251,7 @@ async def test_async_tasks() -> None:
254251
aiozc = AsyncZeroconf(interfaces=['127.0.0.1'])
255252
type_ = "_test-srvc4-type._tcp.local."
256253
name = "xxxyyy"
257-
registration_name = "%s.%s" % (name, type_)
254+
registration_name = f"{name}.{type_}"
258255

259256
calls = []
260257

@@ -320,7 +317,7 @@ async def test_async_wait_unblocks_on_update() -> None:
320317
aiozc = AsyncZeroconf(interfaces=['127.0.0.1'])
321318
type_ = "_test-srvc4-type._tcp.local."
322319
name = "xxxyyy"
323-
registration_name = "%s.%s" % (name, type_)
320+
registration_name = f"{name}.{type_}"
324321

325322
desc = {'path': '/~paulsm/'}
326323
info = ServiceInfo(
@@ -356,8 +353,8 @@ async def test_service_info_async_request() -> None:
356353
type_ = "_test1-srvc-type._tcp.local."
357354
name = "xxxyyy"
358355
name2 = "abc"
359-
registration_name = "%s.%s" % (name, type_)
360-
registration_name2 = "%s.%s" % (name2, type_)
356+
registration_name = f"{name}.{type_}"
357+
registration_name2 = f"{name2}.{type_}"
361358

362359
# Start a tasks BEFORE the registration that will keep trying
363360
# and see the registration a bit later
@@ -454,7 +451,7 @@ async def test_async_service_browser() -> None:
454451
aiozc = AsyncZeroconf(interfaces=['127.0.0.1'])
455452
type_ = "_test9-srvc-type._tcp.local."
456453
name = "xxxyyy"
457-
registration_name = "%s.%s" % (name, type_)
454+
registration_name = f"{name}.{type_}"
458455

459456
calls = []
460457

@@ -513,7 +510,7 @@ async def test_async_context_manager() -> None:
513510
"""Test using an async context manager."""
514511
type_ = "_test10-sr-type._tcp.local."
515512
name = "xxxyyy"
516-
registration_name = "%s.%s" % (name, type_)
513+
registration_name = f"{name}.{type_}"
517514

518515
async with AsyncZeroconf(interfaces=['127.0.0.1']) as aiozc:
519516
info = ServiceInfo(
@@ -539,8 +536,8 @@ async def test_async_unregister_all_services() -> None:
539536
type_ = "_test1-srvc-type._tcp.local."
540537
name = "xxxyyy"
541538
name2 = "abc"
542-
registration_name = "%s.%s" % (name, type_)
543-
registration_name2 = "%s.%s" % (name2, type_)
539+
registration_name = f"{name}.{type_}"
540+
registration_name2 = f"{name2}.{type_}"
544541

545542
desc = {'path': '/~paulsm/'}
546543
info = ServiceInfo(
@@ -594,7 +591,7 @@ async def test_async_unregister_all_services() -> None:
594591
async def test_async_zeroconf_service_types():
595592
type_ = "_test-srvc-type._tcp.local."
596593
name = "xxxyyy"
597-
registration_name = "%s.%s" % (name, type_)
594+
registration_name = f"{name}.{type_}"
598595

599596
zeroconf_registrar = AsyncZeroconf(interfaces=['127.0.0.1'])
600597
desc = {'path': '/~paulsm/'}
@@ -808,7 +805,7 @@ async def test_info_asking_default_is_asking_qm_questions_after_the_first_qu():
808805
zeroconf_info = aiozc.zeroconf
809806

810807
name = "xxxyyy"
811-
registration_name = "%s.%s" % (name, type_)
808+
registration_name = f"{name}.{type_}"
812809

813810
desc = {'path': '/~paulsm/'}
814811
info = ServiceInfo(

tests/test_cache.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#!/usr/bin/env python
2-
# -*- coding: utf-8 -*-
32

43

54
""" Unit tests for zeroconf._cache. """
@@ -98,7 +97,7 @@ def test_async_all_by_details(self):
9897
record2 = r.DNSAddress('a', const._TYPE_A, const._CLASS_IN, 1, b'b')
9998
cache = r.DNSCache()
10099
cache.async_add_records([record1, record2])
101-
assert set(cache.async_all_by_details('a', const._TYPE_A, const._CLASS_IN)) == set([record1, record2])
100+
assert set(cache.async_all_by_details('a', const._TYPE_A, const._CLASS_IN)) == {record1, record2}
102101

103102
def test_async_entries_with_server(self):
104103
record1 = r.DNSService(
@@ -109,8 +108,8 @@ def test_async_entries_with_server(self):
109108
)
110109
cache = r.DNSCache()
111110
cache.async_add_records([record1, record2])
112-
assert set(cache.async_entries_with_server('ab')) == set([record1, record2])
113-
assert set(cache.async_entries_with_server('AB')) == set([record1, record2])
111+
assert set(cache.async_entries_with_server('ab')) == {record1, record2}
112+
assert set(cache.async_entries_with_server('AB')) == {record1, record2}
114113

115114
def test_async_entries_with_name(self):
116115
record1 = r.DNSService(
@@ -121,8 +120,8 @@ def test_async_entries_with_name(self):
121120
)
122121
cache = r.DNSCache()
123122
cache.async_add_records([record1, record2])
124-
assert set(cache.async_entries_with_name('irrelevant')) == set([record1, record2])
125-
assert set(cache.async_entries_with_name('Irrelevant')) == set([record1, record2])
123+
assert set(cache.async_entries_with_name('irrelevant')) == {record1, record2}
124+
assert set(cache.async_entries_with_name('Irrelevant')) == {record1, record2}
126125

127126

128127
# These functions have been seen in other projects so
@@ -152,7 +151,7 @@ def test_get_all_by_details(self):
152151
record2 = r.DNSAddress('a', const._TYPE_A, const._CLASS_IN, 1, b'b')
153152
cache = r.DNSCache()
154153
cache.async_add_records([record1, record2])
155-
assert set(cache.get_all_by_details('a', const._TYPE_A, const._CLASS_IN)) == set([record1, record2])
154+
assert set(cache.get_all_by_details('a', const._TYPE_A, const._CLASS_IN)) == {record1, record2}
156155

157156
def test_entries_with_server(self):
158157
record1 = r.DNSService(
@@ -163,8 +162,8 @@ def test_entries_with_server(self):
163162
)
164163
cache = r.DNSCache()
165164
cache.async_add_records([record1, record2])
166-
assert set(cache.entries_with_server('ab')) == set([record1, record2])
167-
assert set(cache.entries_with_server('AB')) == set([record1, record2])
165+
assert set(cache.entries_with_server('ab')) == {record1, record2}
166+
assert set(cache.entries_with_server('AB')) == {record1, record2}
168167

169168
def test_entries_with_name(self):
170169
record1 = r.DNSService(
@@ -175,8 +174,8 @@ def test_entries_with_name(self):
175174
)
176175
cache = r.DNSCache()
177176
cache.async_add_records([record1, record2])
178-
assert set(cache.entries_with_name('irrelevant')) == set([record1, record2])
179-
assert set(cache.entries_with_name('Irrelevant')) == set([record1, record2])
177+
assert set(cache.entries_with_name('irrelevant')) == {record1, record2}
178+
assert set(cache.entries_with_name('Irrelevant')) == {record1, record2}
180179

181180
def test_current_entry_with_name_and_alias(self):
182181
record1 = r.DNSPointer(

tests/test_core.py

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#!/usr/bin/env python
2-
# -*- coding: utf-8 -*-
32

43

54
""" Unit tests for zeroconf._core """
@@ -55,28 +54,26 @@ async def test_reaper():
5554
aiozc = AsyncZeroconf(interfaces=['127.0.0.1'])
5655
zeroconf = aiozc.zeroconf
5756
cache = zeroconf.cache
58-
original_entries = list(itertools.chain(*[cache.entries_with_name(name) for name in cache.names()]))
57+
original_entries = list(itertools.chain(*(cache.entries_with_name(name) for name in cache.names())))
5958
record_with_10s_ttl = r.DNSAddress('a', const._TYPE_SOA, const._CLASS_IN, 10, b'a')
6059
record_with_1s_ttl = r.DNSAddress('a', const._TYPE_SOA, const._CLASS_IN, 1, b'b')
6160
zeroconf.cache.async_add_records([record_with_10s_ttl, record_with_1s_ttl])
6261
question = r.DNSQuestion("_hap._tcp._local.", const._TYPE_PTR, const._CLASS_IN)
6362
now = r.current_time_millis()
64-
other_known_answers = set(
65-
[
66-
r.DNSPointer(
67-
"_hap._tcp.local.",
68-
const._TYPE_PTR,
69-
const._CLASS_IN,
70-
10000,
71-
'known-to-other._hap._tcp.local.',
72-
)
73-
]
74-
)
63+
other_known_answers = {
64+
r.DNSPointer(
65+
"_hap._tcp.local.",
66+
const._TYPE_PTR,
67+
const._CLASS_IN,
68+
10000,
69+
'known-to-other._hap._tcp.local.',
70+
)
71+
}
7572
zeroconf.question_history.add_question_at_time(question, now, other_known_answers)
7673
assert zeroconf.question_history.suppresses(question, now, other_known_answers)
77-
entries_with_cache = list(itertools.chain(*[cache.entries_with_name(name) for name in cache.names()]))
74+
entries_with_cache = list(itertools.chain(*(cache.entries_with_name(name) for name in cache.names())))
7875
await asyncio.sleep(1.2)
79-
entries = list(itertools.chain(*[cache.entries_with_name(name) for name in cache.names()]))
76+
entries = list(itertools.chain(*(cache.entries_with_name(name) for name in cache.names())))
8077
await aiozc.async_close()
8178
assert not zeroconf.question_history.suppresses(question, now, other_known_answers)
8279
assert entries != original_entries
@@ -367,7 +364,7 @@ def test_register_service_with_custom_ttl():
367364
name = "MyTestHome"
368365
info_service = r.ServiceInfo(
369366
type_,
370-
'%s.%s' % (name, type_),
367+
f'{name}.{type_}',
371368
80,
372369
0,
373370
0,
@@ -423,9 +420,9 @@ def test_tc_bit_defers():
423420
name2 = "knownname2"
424421
name3 = "knownname3"
425422

426-
registration_name = "%s.%s" % (name, type_)
427-
registration2_name = "%s.%s" % (name2, type_)
428-
registration3_name = "%s.%s" % (name3, type_)
423+
registration_name = f"{name}.{type_}"
424+
registration2_name = f"{name2}.{type_}"
425+
registration3_name = f"{name3}.{type_}"
429426

430427
desc = {'path': '/~paulsm/'}
431428
server_name = "ash-2.local."
@@ -502,9 +499,9 @@ def test_tc_bit_defers_last_response_missing():
502499
name2 = "knownname2"
503500
name3 = "knownname3"
504501

505-
registration_name = "%s.%s" % (name, type_)
506-
registration2_name = "%s.%s" % (name2, type_)
507-
registration3_name = "%s.%s" % (name3, type_)
502+
registration_name = f"{name}.{type_}"
503+
registration2_name = f"{name2}.{type_}"
504+
registration3_name = f"{name3}.{type_}"
508505

509506
desc = {'path': '/~paulsm/'}
510507
server_name = "ash-2.local."
@@ -729,7 +726,7 @@ def test_shutdown_while_register_in_process():
729726
name = "MyTestHome"
730727
info_service = r.ServiceInfo(
731728
type_,
732-
'%s.%s' % (name, type_),
729+
f'{name}.{type_}',
733730
80,
734731
0,
735732
0,

0 commit comments

Comments
 (0)