Skip to content

Commit 9be235a

Browse files
committed
Modernize type hints and other ruff fixes
1 parent 532ace5 commit 9be235a

File tree

5 files changed

+25
-34
lines changed

5 files changed

+25
-34
lines changed

docs/source/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
# documentation root, use os.path.abspath to make it absolute, like shown here.
1212
#
1313
import os
14-
import sys
1514
import re
15+
import sys
1616

1717
sys.path.insert(0, os.path.abspath("../.."))
1818

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import os
44
import re
55

6-
from setuptools import setup, find_packages
6+
from setuptools import find_packages, setup
77

88
PROJECT_ROOT = os.path.dirname(__file__)
99

src/priority/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -*- coding: utf-8 -*-
21
"""
32
priority: HTTP/2 priority implementation for Python
43
"""

src/priority/priority.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -*- coding: utf-8 -*-
21
"""
32
priority/tree
43
~~~~~~~~~~~~~
@@ -8,8 +7,6 @@
87

98
import heapq
109

11-
from typing import List, Tuple, Optional
12-
1310

1411
class PriorityError(Exception):
1512
"""
@@ -92,9 +89,9 @@ class Stream:
9289
def __init__(self, stream_id: int, weight: int = 16) -> None:
9390
self.stream_id = stream_id
9491
self.weight = weight
95-
self.children: List[Stream] = []
96-
self.parent: Optional[Stream] = None
97-
self.child_queue: List[Tuple[int, Stream]] = []
92+
self.children: list[Stream] = []
93+
self.parent: Stream | None = None
94+
self.child_queue: list[tuple[int, Stream]] = []
9895
self.active = True
9996
self.last_weight = 0
10097
self._deficit = 0
@@ -109,7 +106,7 @@ def weight(self, value: int) -> None:
109106
# weight between 1 and 256 (inclusive)."
110107
if not isinstance(value, int):
111108
raise BadWeightError("Stream weight should be an integer")
112-
elif not (1 <= value <= 256):
109+
if not (1 <= value <= 256):
113110
raise BadWeightError("Stream weight must be between 1 and 256 (inclusive)")
114111
self._weight = value
115112

@@ -158,7 +155,7 @@ def remove_child(
158155
# it in the old one
159156
self.children.remove(child)
160157

161-
new_queue: List[Tuple[int, Stream]] = []
158+
new_queue: list[tuple[int, Stream]] = []
162159

163160
while self.child_queue:
164161
level, stream = heapq.heappop(self.child_queue)
@@ -264,7 +261,7 @@ def _stream_cycle(new_parent: Stream, current: Stream) -> bool:
264261
parent = parent.parent # type: ignore[assignment]
265262
if parent.stream_id == current.stream_id:
266263
return True
267-
elif parent.stream_id == 0:
264+
if parent.stream_id == 0:
268265
return False
269266

270267
raise PriorityLoop(
@@ -339,7 +336,7 @@ def _exclusive_insert(
339336
def insert_stream(
340337
self,
341338
stream_id: int,
342-
depends_on: Optional[int] = None,
339+
depends_on: int | None = None,
343340
weight: int = 16,
344341
exclusive: bool = False,
345342
) -> None:
@@ -383,7 +380,7 @@ def insert_stream(
383380
def reprioritize(
384381
self,
385382
stream_id: int,
386-
depends_on: Optional[int] = None,
383+
depends_on: int | None = None,
387384
weight: int = 16,
388385
exclusive: bool = False,
389386
) -> None:

test/test_priority.py

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,23 @@
1-
# -*- coding: utf-8 -*-
21
"""
32
test_priority
43
~~~~~~~~~~~~~
54
65
Tests for the Priority trees
76
"""
87

9-
import operator
108
import collections
119
import itertools
10+
import operator
11+
from collections.abc import Iterable
12+
from typing import Any
1213

1314
import pytest
14-
1515
from hypothesis import given, settings
16-
from hypothesis.stateful import invariant, RuleBasedStateMachine, rule
17-
from hypothesis.strategies import integers, lists, tuples, sampled_from
16+
from hypothesis.stateful import RuleBasedStateMachine, invariant, rule
17+
from hypothesis.strategies import integers, lists, sampled_from, tuples
1818

1919
import priority
2020

21-
from typing import Iterable, List, Dict, Any
22-
23-
2421
STREAMS_AND_WEIGHTS = lists(
2522
elements=tuples(integers(min_value=1), integers(min_value=1, max_value=255)),
2623
unique_by=operator.itemgetter(0),
@@ -59,7 +56,7 @@ def readme_tree():
5956
def active_readme_streams_from_filter(
6057
filtered: Iterable[int],
6158
blocked: bool = True,
62-
) -> List[int]:
59+
) -> list[int]:
6360
"""
6461
Given a collection of filtered streams, determine which ones are active.
6562
This applies only to the readme tree at this time, though in future it
@@ -81,13 +78,11 @@ def active_readme_streams_from_filter(
8178
}
8279
filtered = set(filtered)
8380

84-
def get_expected(tree: Dict[Any, Any]) -> List[int]:
81+
def get_expected(tree: dict[Any, Any]) -> list[int]:
8582
expected = []
8683

8784
for stream_id in tree:
88-
if stream_id not in filtered and blocked:
89-
expected.append(stream_id)
90-
elif stream_id in filtered and not blocked:
85+
if (stream_id not in filtered and blocked) or (stream_id in filtered and not blocked):
9186
expected.append(stream_id)
9287
else:
9388
expected.extend(get_expected(tree[stream_id]))
@@ -311,22 +306,22 @@ def test_priority_allows_inserting_stream_with_absent_parent(self, exclusive):
311306
p.insert_stream(stream_id=3, depends_on=1, exclusive=exclusive, weight=32)
312307

313308
# Iterate 10 times to prove that the parent stream starts blocked.
314-
first_ten_ids = [next(p) for _ in range(0, 10)]
309+
first_ten_ids = [next(p) for _ in range(10)]
315310
assert first_ten_ids == [3] * 10
316311

317312
# Unblock the parent.
318313
p.unblock(1)
319314

320315
# Iterate 10 times, expecting only the parent.
321-
next_ten_ids = [next(p) for _ in range(0, 10)]
316+
next_ten_ids = [next(p) for _ in range(10)]
322317
assert next_ten_ids == [1] * 10
323318

324319
# Insert a new stream into the tree with default priority.
325320
p.insert_stream(stream_id=5)
326321

327322
# Iterate 10 more times. Expect the parent, and the new stream, in
328323
# equal amounts.
329-
next_ten_ids = [next(p) for _ in range(0, 10)]
324+
next_ten_ids = [next(p) for _ in range(10)]
330325
assert next_ten_ids == [5, 1] * 5
331326

332327
@pytest.mark.parametrize("exclusive", [True, False])
@@ -341,22 +336,22 @@ def test_priority_reprioritizing_stream_with_absent_parent(self, exclusive):
341336
p.reprioritize(stream_id=3, depends_on=1, exclusive=exclusive, weight=32)
342337

343338
# Iterate 10 times to prove that the parent stream starts blocked.
344-
first_ten_ids = [next(p) for _ in range(0, 10)]
339+
first_ten_ids = [next(p) for _ in range(10)]
345340
assert first_ten_ids == [3] * 10
346341

347342
# Unblock the parent.
348343
p.unblock(1)
349344

350345
# Iterate 10 times, expecting only the parent.
351-
next_ten_ids = [next(p) for _ in range(0, 10)]
346+
next_ten_ids = [next(p) for _ in range(10)]
352347
assert next_ten_ids == [1] * 10
353348

354349
# Insert a new stream into the tree with default priority.
355350
p.insert_stream(stream_id=5)
356351

357352
# Iterate 10 more times. Expect the parent, and the new stream, in
358353
# equal amounts.
359-
next_ten_ids = [next(p) for _ in range(0, 10)]
354+
next_ten_ids = [next(p) for _ in range(10)]
360355
assert next_ten_ids == [5, 1] * 5
361356

362357
@pytest.mark.parametrize("count", range(2, 10000, 100))
@@ -386,7 +381,7 @@ def test_can_insert_stream_with_exclusive_dependency_on_0(self, depends_on):
386381

387382
p.insert_stream(stream_id=5, depends_on=depends_on, exclusive=True)
388383

389-
next_ten_ids = [next(p) for _ in range(0, 10)]
384+
next_ten_ids = [next(p) for _ in range(10)]
390385
assert next_ten_ids == [5] * 10
391386

392387
@pytest.mark.parametrize("weight", [None, 0.5, float("inf"), "priority", object])

0 commit comments

Comments
 (0)