Skip to content

Commit 7dbc8ac

Browse files
committed
Python linting: Add ruff rules for Pandas-vet and Pytest-style
1 parent 5fb6496 commit 7dbc8ac

27 files changed

+250
-232
lines changed

.pre-commit-config.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ repos:
5151
- id: validate-pyproject
5252

5353
- repo: https://github.com/pre-commit/mirrors-mypy
54-
rev: v1.5.1
54+
rev: v1.6.0
5555
hooks:
5656
- id: mypy
5757
args:

blockchain/diophantine_equation.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ def extended_gcd(a: int, b: int) -> tuple[int, int, int]:
8383
(1, -2, 3)
8484
8585
"""
86-
assert a >= 0 and b >= 0
86+
assert a >= 0
87+
assert b >= 0
8788

8889
if b == 0:
8990
d, x, y = a, 1, 0
@@ -92,7 +93,8 @@ def extended_gcd(a: int, b: int) -> tuple[int, int, int]:
9293
x = q
9394
y = p - q * (a // b)
9495

95-
assert a % d == 0 and b % d == 0
96+
assert a % d == 0
97+
assert b % d == 0
9698
assert d == a * x + b * y
9799

98100
return (d, x, y)

ciphers/xor_cipher.py

+12-6
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ def encrypt(self, content: str, key: int) -> list[str]:
3838
"""
3939

4040
# precondition
41-
assert isinstance(key, int) and isinstance(content, str)
41+
assert isinstance(key, int)
42+
assert isinstance(content, str)
4243

4344
key = key or self.__key or 1
4445

@@ -56,7 +57,8 @@ def decrypt(self, content: str, key: int) -> list[str]:
5657
"""
5758

5859
# precondition
59-
assert isinstance(key, int) and isinstance(content, list)
60+
assert isinstance(key, int)
61+
assert isinstance(content, list)
6062

6163
key = key or self.__key or 1
6264

@@ -74,7 +76,8 @@ def encrypt_string(self, content: str, key: int = 0) -> str:
7476
"""
7577

7678
# precondition
77-
assert isinstance(key, int) and isinstance(content, str)
79+
assert isinstance(key, int)
80+
assert isinstance(content, str)
7881

7982
key = key or self.__key or 1
8083

@@ -99,7 +102,8 @@ def decrypt_string(self, content: str, key: int = 0) -> str:
99102
"""
100103

101104
# precondition
102-
assert isinstance(key, int) and isinstance(content, str)
105+
assert isinstance(key, int)
106+
assert isinstance(content, str)
103107

104108
key = key or self.__key or 1
105109

@@ -125,7 +129,8 @@ def encrypt_file(self, file: str, key: int = 0) -> bool:
125129
"""
126130

127131
# precondition
128-
assert isinstance(file, str) and isinstance(key, int)
132+
assert isinstance(file, str)
133+
assert isinstance(key, int)
129134

130135
try:
131136
with open(file) as fin, open("encrypt.out", "w+") as fout:
@@ -148,7 +153,8 @@ def decrypt_file(self, file: str, key: int) -> bool:
148153
"""
149154

150155
# precondition
151-
assert isinstance(file, str) and isinstance(key, int)
156+
assert isinstance(file, str)
157+
assert isinstance(key, int)
152158

153159
try:
154160
with open(file) as fin, open("decrypt.out", "w+") as fout:

conversions/decimal_to_hexadecimal.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ def decimal_to_hexadecimal(decimal: float) -> str:
5757
>>> decimal_to_hexadecimal(-256) == hex(-256)
5858
True
5959
"""
60-
assert type(decimal) in (int, float) and decimal == int(decimal)
60+
assert type(decimal) in (int, float)
61+
assert decimal == int(decimal)
6162
decimal = int(decimal)
6263
hexadecimal = ""
6364
negative = False

data_structures/binary_tree/binary_search_tree_recursive.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import unittest
1313
from collections.abc import Iterator
1414

15+
import pytest
16+
1517

1618
class Node:
1719
def __init__(self, label: int, parent: Node | None) -> None:
@@ -359,7 +361,7 @@ def test_put(self) -> None:
359361
assert t.root.left.left.parent == t.root.left
360362
assert t.root.left.left.label == 1
361363

362-
with self.assertRaises(Exception): # noqa: B017
364+
with pytest.raises(Exception): # noqa: B017
363365
t.put(1)
364366

365367
def test_search(self) -> None:
@@ -371,7 +373,7 @@ def test_search(self) -> None:
371373
node = t.search(13)
372374
assert node.label == 13
373375

374-
with self.assertRaises(Exception): # noqa: B017
376+
with pytest.raises(Exception): # noqa: B017
375377
t.search(2)
376378

377379
def test_remove(self) -> None:
@@ -517,7 +519,7 @@ def test_get_max_label(self) -> None:
517519
assert t.get_max_label() == 14
518520

519521
t.empty()
520-
with self.assertRaises(Exception): # noqa: B017
522+
with pytest.raises(Exception): # noqa: B017
521523
t.get_max_label()
522524

523525
def test_get_min_label(self) -> None:
@@ -526,7 +528,7 @@ def test_get_min_label(self) -> None:
526528
assert t.get_min_label() == 1
527529

528530
t.empty()
529-
with self.assertRaises(Exception): # noqa: B017
531+
with pytest.raises(Exception): # noqa: B017
530532
t.get_min_label()
531533

532534
def test_inorder_traversal(self) -> None:

data_structures/hashing/tests/test_hash_map.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,14 @@ def _run_operation(obj, fun, *args):
6565

6666
@pytest.mark.parametrize(
6767
"operations",
68-
(
68+
[
6969
pytest.param(_add_items, id="add items"),
7070
pytest.param(_overwrite_items, id="overwrite items"),
7171
pytest.param(_delete_items, id="delete items"),
7272
pytest.param(_access_absent_items, id="access absent items"),
7373
pytest.param(_add_with_resize_up, id="add with resize up"),
7474
pytest.param(_add_with_resize_down, id="add with resize down"),
75-
),
75+
],
7676
)
7777
def test_hash_map_is_the_same_as_dict(operations):
7878
my = HashMap(initial_block_size=4)

data_structures/linked_list/circular_linked_list.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ def delete_nth(self, index: int = 0) -> Any:
124124
if not 0 <= index < len(self):
125125
raise IndexError("list index out of range.")
126126

127-
assert self.head is not None and self.tail is not None
127+
assert self.head is not None
128+
assert self.tail is not None
128129
delete_node: Node = self.head
129130
if self.head == self.tail: # Just one node
130131
self.head = self.tail = None
@@ -137,7 +138,8 @@ def delete_nth(self, index: int = 0) -> Any:
137138
for _ in range(index - 1):
138139
assert temp is not None
139140
temp = temp.next
140-
assert temp is not None and temp.next is not None
141+
assert temp is not None
142+
assert temp.next is not None
141143
delete_node = temp.next
142144
temp.next = temp.next.next
143145
if index == len(self) - 1: # Delete at tail

digital_image_processing/test_digital_image_processing.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ def test_median_filter():
7373

7474
def test_sobel_filter():
7575
grad, theta = sob.sobel_filter(gray)
76-
assert grad.any() and theta.any()
76+
assert grad.any()
77+
assert theta.any()
7778

7879

7980
def test_sepia():

graphs/graph_adjacency_list.py

+29-31
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
from pprint import pformat
2323
from typing import Generic, TypeVar
2424

25+
import pytest
26+
2527
T = TypeVar("T")
2628

2729

@@ -185,42 +187,42 @@ def __assert_graph_edge_exists_check(
185187
directed_graph: GraphAdjacencyList,
186188
edge: list[int],
187189
) -> None:
188-
self.assertTrue(undirected_graph.contains_edge(edge[0], edge[1]))
189-
self.assertTrue(undirected_graph.contains_edge(edge[1], edge[0]))
190-
self.assertTrue(directed_graph.contains_edge(edge[0], edge[1]))
190+
assert undirected_graph.contains_edge(edge[0], edge[1])
191+
assert undirected_graph.contains_edge(edge[1], edge[0])
192+
assert directed_graph.contains_edge(edge[0], edge[1])
191193

192194
def __assert_graph_edge_does_not_exist_check(
193195
self,
194196
undirected_graph: GraphAdjacencyList,
195197
directed_graph: GraphAdjacencyList,
196198
edge: list[int],
197199
) -> None:
198-
self.assertFalse(undirected_graph.contains_edge(edge[0], edge[1]))
199-
self.assertFalse(undirected_graph.contains_edge(edge[1], edge[0]))
200-
self.assertFalse(directed_graph.contains_edge(edge[0], edge[1]))
200+
assert not undirected_graph.contains_edge(edge[0], edge[1])
201+
assert not undirected_graph.contains_edge(edge[1], edge[0])
202+
assert not directed_graph.contains_edge(edge[0], edge[1])
201203

202204
def __assert_graph_vertex_exists_check(
203205
self,
204206
undirected_graph: GraphAdjacencyList,
205207
directed_graph: GraphAdjacencyList,
206208
vertex: int,
207209
) -> None:
208-
self.assertTrue(undirected_graph.contains_vertex(vertex))
209-
self.assertTrue(directed_graph.contains_vertex(vertex))
210+
assert undirected_graph.contains_vertex(vertex)
211+
assert directed_graph.contains_vertex(vertex)
210212

211213
def __assert_graph_vertex_does_not_exist_check(
212214
self,
213215
undirected_graph: GraphAdjacencyList,
214216
directed_graph: GraphAdjacencyList,
215217
vertex: int,
216218
) -> None:
217-
self.assertFalse(undirected_graph.contains_vertex(vertex))
218-
self.assertFalse(directed_graph.contains_vertex(vertex))
219+
assert not undirected_graph.contains_vertex(vertex)
220+
assert not directed_graph.contains_vertex(vertex)
219221

220222
def __generate_random_edges(
221223
self, vertices: list[int], edge_pick_count: int
222224
) -> list[list[int]]:
223-
self.assertTrue(edge_pick_count <= len(vertices))
225+
assert edge_pick_count <= len(vertices)
224226

225227
random_source_vertices: list[int] = random.sample(
226228
vertices[0 : int(len(vertices) / 2)], edge_pick_count
@@ -281,8 +283,8 @@ def test_init_check(self) -> None:
281283
self.__assert_graph_edge_exists_check(
282284
undirected_graph, directed_graph, edge
283285
)
284-
self.assertFalse(undirected_graph.directed)
285-
self.assertTrue(directed_graph.directed)
286+
assert not undirected_graph.directed
287+
assert directed_graph.directed
286288

287289
def test_contains_vertex(self) -> None:
288290
random_vertices: list[int] = random.sample(range(101), 20)
@@ -297,12 +299,8 @@ def test_contains_vertex(self) -> None:
297299

298300
# Test contains_vertex
299301
for num in range(101):
300-
self.assertEqual(
301-
num in random_vertices, undirected_graph.contains_vertex(num)
302-
)
303-
self.assertEqual(
304-
num in random_vertices, directed_graph.contains_vertex(num)
305-
)
302+
assert (num in random_vertices) == undirected_graph.contains_vertex(num)
303+
assert (num in random_vertices) == directed_graph.contains_vertex(num)
306304

307305
def test_add_vertices(self) -> None:
308306
random_vertices: list[int] = random.sample(range(101), 20)
@@ -507,9 +505,9 @@ def test_add_vertex_exception_check(self) -> None:
507505
) = self.__generate_graphs(20, 0, 100, 4)
508506

509507
for vertex in random_vertices:
510-
with self.assertRaises(ValueError):
508+
with pytest.raises(ValueError):
511509
undirected_graph.add_vertex(vertex)
512-
with self.assertRaises(ValueError):
510+
with pytest.raises(ValueError):
513511
directed_graph.add_vertex(vertex)
514512

515513
def test_remove_vertex_exception_check(self) -> None:
@@ -522,9 +520,9 @@ def test_remove_vertex_exception_check(self) -> None:
522520

523521
for i in range(101):
524522
if i not in random_vertices:
525-
with self.assertRaises(ValueError):
523+
with pytest.raises(ValueError):
526524
undirected_graph.remove_vertex(i)
527-
with self.assertRaises(ValueError):
525+
with pytest.raises(ValueError):
528526
directed_graph.remove_vertex(i)
529527

530528
def test_add_edge_exception_check(self) -> None:
@@ -536,9 +534,9 @@ def test_add_edge_exception_check(self) -> None:
536534
) = self.__generate_graphs(20, 0, 100, 4)
537535

538536
for edge in random_edges:
539-
with self.assertRaises(ValueError):
537+
with pytest.raises(ValueError):
540538
undirected_graph.add_edge(edge[0], edge[1])
541-
with self.assertRaises(ValueError):
539+
with pytest.raises(ValueError):
542540
directed_graph.add_edge(edge[0], edge[1])
543541

544542
def test_remove_edge_exception_check(self) -> None:
@@ -560,9 +558,9 @@ def test_remove_edge_exception_check(self) -> None:
560558
more_random_edges.append(edge)
561559

562560
for edge in more_random_edges:
563-
with self.assertRaises(ValueError):
561+
with pytest.raises(ValueError):
564562
undirected_graph.remove_edge(edge[0], edge[1])
565-
with self.assertRaises(ValueError):
563+
with pytest.raises(ValueError):
566564
directed_graph.remove_edge(edge[0], edge[1])
567565

568566
def test_contains_edge_exception_check(self) -> None:
@@ -574,14 +572,14 @@ def test_contains_edge_exception_check(self) -> None:
574572
) = self.__generate_graphs(20, 0, 100, 4)
575573

576574
for vertex in random_vertices:
577-
with self.assertRaises(ValueError):
575+
with pytest.raises(ValueError):
578576
undirected_graph.contains_edge(vertex, 102)
579-
with self.assertRaises(ValueError):
577+
with pytest.raises(ValueError):
580578
directed_graph.contains_edge(vertex, 102)
581579

582-
with self.assertRaises(ValueError):
580+
with pytest.raises(ValueError):
583581
undirected_graph.contains_edge(103, 102)
584-
with self.assertRaises(ValueError):
582+
with pytest.raises(ValueError):
585583
directed_graph.contains_edge(103, 102)
586584

587585

0 commit comments

Comments
 (0)