Skip to content

Commit a43753f

Browse files
authored
chore: enable and fix additional ruff rules (#1399)
1 parent d399a4e commit a43753f

55 files changed

Lines changed: 520 additions & 1411 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

bench/create_destory.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ async def _run() -> None:
1818
start = time.perf_counter()
1919
await _create_destroy(iterations)
2020
duration = time.perf_counter() - start
21-
print(
22-
f"Creating and destroying {iterations} Zeroconf instances took {duration} seconds"
23-
)
21+
print(f"Creating and destroying {iterations} Zeroconf instances took {duration} seconds")
2422

2523

2624
asyncio.run(_run())

bench/incoming.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ def generate_packets() -> List[bytes]:
178178

179179
def parse_incoming_message() -> None:
180180
for packet in packets:
181-
DNSIncoming(packet).answers
181+
DNSIncoming(packet).answers # noqa: B018
182182
break
183183

184184

bench/txt_properties.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
def process_properties() -> None:
1616
info._properties = None
17-
info.properties
17+
info.properties # noqa: B018
1818

1919

2020
count = 100000

build_ext.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
"""Build optional cython modules."""
22

3+
import logging
34
import os
45
from distutils.command.build_ext import build_ext
56
from typing import Any
67

8+
_LOGGER = logging.getLogger(__name__)
9+
710

811
class BuildExt(build_ext):
912
def build_extensions(self) -> None:
1013
try:
1114
super().build_extensions()
1215
except Exception:
13-
pass
16+
_LOGGER.info("Failed to build cython extensions")
1417

1518

1619
def build(setup_kwargs: Any) -> None:
@@ -20,8 +23,8 @@ def build(setup_kwargs: Any) -> None:
2023
from Cython.Build import cythonize
2124

2225
setup_kwargs.update(
23-
dict(
24-
ext_modules=cythonize(
26+
{
27+
"ext_modules": cythonize(
2528
[
2629
"src/zeroconf/_dns.py",
2730
"src/zeroconf/_cache.py",
@@ -44,12 +47,10 @@ def build(setup_kwargs: Any) -> None:
4447
],
4548
compiler_directives={"language_level": "3"}, # Python 3
4649
),
47-
cmdclass=dict(build_ext=BuildExt),
48-
)
50+
"cmdclass": {"build_ext": BuildExt},
51+
}
4952
)
50-
setup_kwargs["exclude_package_data"] = {
51-
pkg: ["*.c"] for pkg in setup_kwargs["packages"]
52-
}
53+
setup_kwargs["exclude_package_data"] = {pkg: ["*.c"] for pkg in setup_kwargs["packages"]}
5354
except Exception:
5455
if os.environ.get("REQUIRE_CYTHON"):
5556
raise

examples/async_apple_scanner.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232

3333
log = logging.getLogger(__name__)
3434

35+
_PENDING_TASKS: set[asyncio.Task] = set()
36+
3537

3638
def async_on_service_state_change(
3739
zeroconf: Zeroconf, service_type: str, name: str, state_change: ServiceStateChange
@@ -41,23 +43,21 @@ def async_on_service_state_change(
4143
return
4244
base_name = name[: -len(service_type) - 1]
4345
device_name = f"{base_name}.{DEVICE_INFO_SERVICE}"
44-
asyncio.ensure_future(_async_show_service_info(zeroconf, service_type, name))
46+
task = asyncio.ensure_future(_async_show_service_info(zeroconf, service_type, name))
47+
_PENDING_TASKS.add(task)
48+
task.add_done_callback(_PENDING_TASKS.discard)
4549
# Also probe for device info
46-
asyncio.ensure_future(
47-
_async_show_service_info(zeroconf, DEVICE_INFO_SERVICE, device_name)
48-
)
50+
task = asyncio.ensure_future(_async_show_service_info(zeroconf, DEVICE_INFO_SERVICE, device_name))
51+
_PENDING_TASKS.add(task)
52+
task.add_done_callback(_PENDING_TASKS.discard)
4953

5054

51-
async def _async_show_service_info(
52-
zeroconf: Zeroconf, service_type: str, name: str
53-
) -> None:
55+
async def _async_show_service_info(zeroconf: Zeroconf, service_type: str, name: str) -> None:
5456
info = AsyncServiceInfo(service_type, name)
5557
await info.async_request(zeroconf, 3000, question_type=DNSQuestionType.QU)
5658
print("Info from zeroconf.get_service_info: %r" % (info))
5759
if info:
58-
addresses = [
59-
"%s:%d" % (addr, cast(int, info.port)) for addr in info.parsed_addresses()
60-
]
60+
addresses = ["%s:%d" % (addr, cast(int, info.port)) for addr in info.parsed_addresses()]
6161
print(" Name: %s" % name)
6262
print(" Addresses: %s" % ", ".join(addresses))
6363
print(" Weight: %d, priority: %d" % (info.weight, info.priority))

examples/async_browser.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,26 @@
1818
AsyncZeroconfServiceTypes,
1919
)
2020

21+
_PENDING_TASKS: set[asyncio.Task] = set()
22+
2123

2224
def async_on_service_state_change(
2325
zeroconf: Zeroconf, service_type: str, name: str, state_change: ServiceStateChange
2426
) -> None:
2527
print(f"Service {name} of type {service_type} state changed: {state_change}")
2628
if state_change is not ServiceStateChange.Added:
2729
return
28-
asyncio.ensure_future(async_display_service_info(zeroconf, service_type, name))
30+
task = asyncio.ensure_future(async_display_service_info(zeroconf, service_type, name))
31+
_PENDING_TASKS.add(task)
32+
task.add_done_callback(_PENDING_TASKS.discard)
2933

3034

31-
async def async_display_service_info(
32-
zeroconf: Zeroconf, service_type: str, name: str
33-
) -> None:
35+
async def async_display_service_info(zeroconf: Zeroconf, service_type: str, name: str) -> None:
3436
info = AsyncServiceInfo(service_type, name)
3537
await info.async_request(zeroconf, 3000)
3638
print("Info from zeroconf.get_service_info: %r" % (info))
3739
if info:
38-
addresses = [
39-
"%s:%d" % (addr, cast(int, info.port))
40-
for addr in info.parsed_scoped_addresses()
41-
]
40+
addresses = ["%s:%d" % (addr, cast(int, info.port)) for addr in info.parsed_scoped_addresses()]
4241
print(" Name: %s" % name)
4342
print(" Addresses: %s" % ", ".join(addresses))
4443
print(" Weight: %d, priority: %d" % (info.weight, info.priority))
@@ -66,9 +65,7 @@ async def async_run(self) -> None:
6665
services = ["_http._tcp.local.", "_hap._tcp.local."]
6766
if self.args.find:
6867
services = list(
69-
await AsyncZeroconfServiceTypes.async_find(
70-
aiozc=self.aiozc, ip_version=ip_version
71-
)
68+
await AsyncZeroconfServiceTypes.async_find(aiozc=self.aiozc, ip_version=ip_version)
7269
)
7370

7471
print("\nBrowsing %s service(s), press Ctrl-C to exit...\n" % services)
@@ -90,9 +87,7 @@ async def async_close(self) -> None:
9087

9188
parser = argparse.ArgumentParser()
9289
parser.add_argument("--debug", action="store_true")
93-
parser.add_argument(
94-
"--find", action="store_true", help="Browse all available services"
95-
)
90+
parser.add_argument("--find", action="store_true", help="Browse all available services")
9691
version_group = parser.add_mutually_exclusive_group()
9792
version_group.add_argument("--v6", action="store_true")
9893
version_group.add_argument("--v6-only", action="store_true")

examples/async_service_info_request.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,7 @@ async def async_watch_services(aiozc: AsyncZeroconf) -> None:
3131
for info in infos:
3232
print("Info for %s" % (info.name))
3333
if info:
34-
addresses = [
35-
"%s:%d" % (addr, cast(int, info.port))
36-
for addr in info.parsed_addresses()
37-
]
34+
addresses = ["%s:%d" % (addr, cast(int, info.port)) for addr in info.parsed_addresses()]
3835
print(" Addresses: %s" % ", ".join(addresses))
3936
print(" Weight: %d, priority: %d" % (info.weight, info.priority))
4037
print(f" Server: {info.server}")

examples/browser.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,7 @@ def on_service_state_change(
2929
print("Info from zeroconf.get_service_info: %r" % (info))
3030

3131
if info:
32-
addresses = [
33-
"%s:%d" % (addr, cast(int, info.port))
34-
for addr in info.parsed_scoped_addresses()
35-
]
32+
addresses = ["%s:%d" % (addr, cast(int, info.port)) for addr in info.parsed_scoped_addresses()]
3633
print(" Addresses: %s" % ", ".join(addresses))
3734
print(" Weight: %d, priority: %d" % (info.weight, info.priority))
3835
print(f" Server: {info.server}")
@@ -52,9 +49,7 @@ def on_service_state_change(
5249

5350
parser = argparse.ArgumentParser()
5451
parser.add_argument("--debug", action="store_true")
55-
parser.add_argument(
56-
"--find", action="store_true", help="Browse all available services"
57-
)
52+
parser.add_argument("--find", action="store_true", help="Browse all available services")
5853
version_group = parser.add_mutually_exclusive_group()
5954
version_group.add_argument("--v6-only", action="store_true")
6055
version_group.add_argument("--v4-only", action="store_true")

examples/self_test.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,10 @@
3434
r.register_service(info)
3535
print(" Registration done.")
3636
print("2. Testing query of service information...")
37-
print(
38-
" Getting ZOE service: %s"
39-
% (r.get_service_info("_http._tcp.local.", "ZOE._http._tcp.local."))
40-
)
37+
print(" Getting ZOE service: %s" % (r.get_service_info("_http._tcp.local.", "ZOE._http._tcp.local.")))
4138
print(" Query done.")
4239
print("3. Testing query of own service...")
43-
queried_info = r.get_service_info(
44-
"_http._tcp.local.", "My Service Name._http._tcp.local."
45-
)
40+
queried_info = r.get_service_info("_http._tcp.local.", "My Service Name._http._tcp.local.")
4641
assert queried_info
4742
assert set(queried_info.parsed_addresses()) == expected
4843
print(f" Getting self: {queried_info}")

pyproject.toml

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,28 @@ cython = "^3.0.5"
6363
setuptools = "^65.6.3"
6464
pytest-timeout = "^2.1.0"
6565

66-
[tool.black]
66+
[tool.ruff]
67+
target-version = "py38"
6768
line-length = 110
68-
target_version = ['py37', 'py38', 'py39', 'py310', 'py311']
69-
skip_string_normalization = true
69+
70+
[tool.ruff.lint]
71+
ignore = [
72+
"S101", # use of assert
73+
"S104", # S104 Possible binding to all interfaces
74+
"UP031", # UP031 use f-strings -- too many to fix right now
75+
]
76+
select = [
77+
"B", # flake8-bugbear
78+
"C4", # flake8-comprehensions
79+
"S", # flake8-bandit
80+
"F", # pyflake
81+
"E", # pycodestyle
82+
"W", # pycodestyle
83+
"UP", # pyupgrade
84+
"I", # isort
85+
"RUF", # ruff specific
86+
]
87+
7088

7189
[tool.pylint.BASIC]
7290
class-const-naming-style = "any"

0 commit comments

Comments
 (0)