chore: migrate to f-string (#1481) · python-zeroconf/python-zeroconf@d20d8c1

16 files changed

lines changed

Original file line numberDiff line numberDiff line change

@@ -55,12 +55,12 @@ def async_on_service_state_change(

5555

async def _async_show_service_info(zeroconf: Zeroconf, service_type: str, name: str) -> None:

5656

info = AsyncServiceInfo(service_type, name)

5757

await info.async_request(zeroconf, 3000, question_type=DNSQuestionType.QU)

58-

print("Info from zeroconf.get_service_info: %r" % (info))

58+

print(f"Info from zeroconf.get_service_info: {info!r}")

5959

if info:

60-

addresses = ["%s:%d" % (addr, cast(int, info.port)) for addr in info.parsed_addresses()]

61-

print(" Name: %s" % name)

62-

print(" Addresses: %s" % ", ".join(addresses))

63-

print(" Weight: %d, priority: %d" % (info.weight, info.priority))

60+

addresses = [f"{addr}:{cast(int, info.port)}" for addr in info.parsed_addresses()]

61+

print(f" Name: {name}")

62+

print(f" Addresses: {', '.join(addresses)}")

63+

print(f" Weight: {info.weight}, priority: {info.priority}")

6464

print(f" Server: {info.server}")

6565

if info.properties:

6666

print(" Properties are:")

@@ -82,7 +82,7 @@ def __init__(self, args: Any) -> None:

8282

async def async_run(self) -> None:

8383

self.aiozc = AsyncZeroconf(ip_version=ip_version)

8484

await self.aiozc.zeroconf.async_wait_for_start()

85-

print("\nBrowsing %s service(s), press Ctrl-C to exit...\n" % ALL_SERVICES)

85+

print(f"\nBrowsing {ALL_SERVICES} service(s), press Ctrl-C to exit...\n")

8686

kwargs = {

8787

"handlers": [async_on_service_state_change],

8888

"question_type": DNSQuestionType.QU,

Original file line numberDiff line numberDiff line change

@@ -35,12 +35,12 @@ def async_on_service_state_change(

3535

async def async_display_service_info(zeroconf: Zeroconf, service_type: str, name: str) -> None:

3636

info = AsyncServiceInfo(service_type, name)

3737

await info.async_request(zeroconf, 3000)

38-

print("Info from zeroconf.get_service_info: %r" % (info))

38+

print(f"Info from zeroconf.get_service_info: {info!r}")

3939

if info:

40-

addresses = ["%s:%d" % (addr, cast(int, info.port)) for addr in info.parsed_scoped_addresses()]

41-

print(" Name: %s" % name)

42-

print(" Addresses: %s" % ", ".join(addresses))

43-

print(" Weight: %d, priority: %d" % (info.weight, info.priority))

40+

addresses = [f"{addr}:{cast(int, info.port)}" for addr in info.parsed_scoped_addresses()]

41+

print(f" Name: {name}")

42+

print(f" Addresses: {', '.join(addresses)}")

43+

print(f" Weight: {info.weight}, priority: {info.priority}")

4444

print(f" Server: {info.server}")

4545

if info.properties:

4646

print(" Properties are:")

@@ -68,7 +68,7 @@ async def async_run(self) -> None:

6868

await AsyncZeroconfServiceTypes.async_find(aiozc=self.aiozc, ip_version=ip_version)

6969

)

7070
71-

print("\nBrowsing %s service(s), press Ctrl-C to exit...\n" % services)

71+

print(f"\nBrowsing {services} service(s), press Ctrl-C to exit...\n")

7272

self.aiobrowser = AsyncServiceBrowser(

7373

self.aiozc.zeroconf, services, handlers=[async_on_service_state_change]

7474

)

Original file line numberDiff line numberDiff line change

@@ -30,11 +30,11 @@ async def async_watch_services(aiozc: AsyncZeroconf) -> None:

3030

tasks = [info.async_request(aiozc.zeroconf, 3000) for info in infos]

3131

await asyncio.gather(*tasks)

3232

for info in infos:

33-

print("Info for %s" % (info.name))

33+

print(f"Info for {info.name}")

3434

if info:

35-

addresses = ["%s:%d" % (addr, cast(int, info.port)) for addr in info.parsed_addresses()]

36-

print(" Addresses: %s" % ", ".join(addresses))

37-

print(" Weight: %d, priority: %d" % (info.weight, info.priority))

35+

addresses = [f"{addr}:{cast(int, info.port)}" for addr in info.parsed_addresses()]

36+

print(f" Addresses: {', '.join(addresses)}")

37+

print(f" Weight: {info.weight}, priority: {info.priority}")

3838

print(f" Server: {info.server}")

3939

if info.properties:

4040

print(" Properties are:")

Original file line numberDiff line numberDiff line change

@@ -26,12 +26,12 @@ def on_service_state_change(

2626
2727

if state_change is ServiceStateChange.Added:

2828

info = zeroconf.get_service_info(service_type, name)

29-

print("Info from zeroconf.get_service_info: %r" % (info))

29+

print(f"Info from zeroconf.get_service_info: {info!r}")

3030
3131

if info:

32-

addresses = ["%s:%d" % (addr, cast(int, info.port)) for addr in info.parsed_scoped_addresses()]

33-

print(" Addresses: %s" % ", ".join(addresses))

34-

print(" Weight: %d, priority: %d" % (info.weight, info.priority))

32+

addresses = [f"{addr}:{cast(int, info.port)}" for addr in info.parsed_scoped_addresses()]

33+

print(f" Addresses: {', '.join(addresses)}")

34+

print(f" Weight: {info.weight}, priority: {info.priority}")

3535

print(f" Server: {info.server}")

3636

if info.properties:

3737

print(" Properties are:")

@@ -75,7 +75,7 @@ def on_service_state_change(

7575

if args.find:

7676

services = list(ZeroconfServiceTypes.find(zc=zeroconf))

7777
78-

print("\nBrowsing %d service(s), press Ctrl-C to exit...\n" % len(services))

78+

print(f"\nBrowsing {len(services)} service(s), press Ctrl-C to exit...\n")

7979

browser = ServiceBrowser(zeroconf, services, handlers=[on_service_state_change])

8080
8181

try:

Original file line numberDiff line numberDiff line change

@@ -34,7 +34,7 @@

3434

r.register_service(info)

3535

print(" Registration done.")

3636

print("2. Testing query of service information...")

37-

print(" Getting ZOE service: %s" % (r.get_service_info("_http._tcp.local.", "ZOE._http._tcp.local.")))

37+

print(f" Getting ZOE service: {r.get_service_info('_http._tcp.local.', 'ZOE._http._tcp.local.')}")

3838

print(" Query done.")

3939

print("3. Testing query of own service...")

4040

queried_info = r.get_service_info("_http._tcp.local.", "My Service Name._http._tcp.local.")

Original file line numberDiff line numberDiff line change

@@ -90,7 +90,6 @@ line-length = 110

9090

ignore = [

9191

"S101", # use of assert

9292

"S104", # S104 Possible binding to all interfaces

93-

"UP031", # UP031 use f-strings -- too many to fix right now

9493

]

9594

select = [

9695

"B", # flake8-bugbear

Original file line numberDiff line numberDiff line change

@@ -102,7 +102,7 @@ def entry_to_string(self, hdr: str, other: Optional[Union[bytes, str]]) -> str:

102102

self.get_class_(self.class_),

103103

"-unique" if self.unique else "",

104104

self.name,

105-

"=%s" % cast(Any, other) if other is not None else "",

105+

f"={cast(Any, other)}" if other is not None else "",

106106

)

107107
108108
Original file line numberDiff line numberDiff line change

@@ -208,18 +208,20 @@ def is_probe(self) -> bool:

208208

return self._num_authorities > 0

209209
210210

def __repr__(self) -> str:

211-

return "<DNSIncoming:{%s}>" % ", ".join(

212-

[

213-

"id=%s" % self.id,

214-

"flags=%s" % self.flags,

215-

"truncated=%s" % self.truncated,

216-

"n_q=%s" % self._num_questions,

217-

"n_ans=%s" % self._num_answers,

218-

"n_auth=%s" % self._num_authorities,

219-

"n_add=%s" % self._num_additionals,

220-

"questions=%s" % self._questions,

221-

"answers=%s" % self.answers(),

222-

]

211+

return "<DNSIncoming:{}>".format(

212+

", ".join(

213+

[

214+

f"id={self.id}",

215+

f"flags={self.flags}",

216+

f"truncated={self.truncated}",

217+

f"n_q={self._num_questions}",

218+

f"n_ans={self._num_answers}",

219+

f"n_auth={self._num_authorities}",

220+

f"n_add={self._num_additionals}",

221+

f"questions={self._questions}",

222+

f"answers={self.answers()}",

223+

]

224+

)

223225

)

224226
225227

def _read_header(self) -> None:

Original file line numberDiff line numberDiff line change

@@ -128,15 +128,17 @@ def _reset_for_next_packet(self) -> None:

128128

self.allow_long = True

129129
130130

def __repr__(self) -> str:

131-

return "<DNSOutgoing:{%s}>" % ", ".join(

132-

[

133-

"multicast=%s" % self.multicast,

134-

"flags=%s" % self.flags,

135-

"questions=%s" % self.questions,

136-

"answers=%s" % self.answers,

137-

"authorities=%s" % self.authorities,

138-

"additionals=%s" % self.additionals,

139-

]

131+

return "<DNSOutgoing:{}>".format(

132+

", ".join(

133+

[

134+

f"multicast={self.multicast}",

135+

f"flags={self.flags}",

136+

f"questions={self.questions}",

137+

f"answers={self.answers}",

138+

f"authorities={self.authorities}",

139+

f"additionals={self.additionals}",

140+

]

141+

)

140142

)

141143
142144

def add_question(self, record: DNSQuestion) -> None:

Original file line numberDiff line numberDiff line change

@@ -95,7 +95,7 @@ def ip6_to_address_and_index(adapters: List[Any], ip: str) -> Tuple[Tuple[str, i

9595

cast(int, adapter.index),

9696

)

9797
98-

raise RuntimeError("No adapter found for IP address %s" % ip)

98+

raise RuntimeError(f"No adapter found for IP address {ip}")

9999
100100
101101

def interface_index_to_ip6_address(adapters: List[Any], index: int) -> Tuple[str, int, int]:

@@ -106,7 +106,7 @@ def interface_index_to_ip6_address(adapters: List[Any], index: int) -> Tuple[str

106106

if isinstance(adapter_ip.ip, tuple):

107107

return cast(Tuple[str, int, int], adapter_ip.ip)

108108
109-

raise RuntimeError("No adapter found for index %s" % index)

109+

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

110110
111111
112112

def ip6_addresses_to_indexes(

@@ -154,15 +154,15 @@ def normalize_interface_choice(

154154

result.extend(get_all_addresses())

155155

if not result:

156156

raise RuntimeError(

157-

"No interfaces to listen on, check that any interfaces have IP version %s" % ip_version

157+

f"No interfaces to listen on, check that any interfaces have IP version {ip_version}"

158158

)

159159

elif isinstance(choice, list):

160160

# First, take IPv4 addresses.

161161

result = [i for i in choice if isinstance(i, str) and ipaddress.ip_address(i).version == 4]

162162

# Unlike IP_ADD_MEMBERSHIP, IPV6_JOIN_GROUP requires interface indexes.

163163

result += ip6_addresses_to_indexes(choice)

164164

else:

165-

raise TypeError("choice must be a list or InterfaceChoice, got %r" % choice)

165+

raise TypeError(f"choice must be a list or InterfaceChoice, got {choice!r}")

166166

return result

167167
168168