Skip to content

Commit 636dd57

Browse files
Fix ty invalid assignment (#15222)
* Fix ty invalid assignment diagnostics * updating DIRECTORY.md * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix unused typing import * Fix gradient accumulation type handling * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix automatic differentiation gradient dtype handling --------- Co-authored-by: kadubhumika <kadubhumika@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 62d049a commit 636dd57

12 files changed

Lines changed: 52 additions & 16 deletions

File tree

cellular_automata/conways_game_of_life.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ def generate_images(cells: list[list[int]], frames: int) -> list[Image.Image]:
7878
# Create output image
7979
img = Image.new("RGB", (len(cells[0]), len(cells)))
8080
pixels = img.load()
81+
assert pixels is not None
8182

8283
# Save cells to image
8384
for x in range(len(cells)):

cellular_automata/one_dimensional.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ def generate_image(cells: list[list[int]]) -> Image.Image:
5555
# Create the output image
5656
img = Image.new("RGB", (len(cells[0]), len(cells)))
5757
pixels = img.load()
58+
assert pixels is not None
5859
# Generates image
5960
for w in range(img.width):
6061
for h in range(img.height):

data_structures/binary_tree/non_recursive_segment_tree.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
from __future__ import annotations
4040

4141
from collections.abc import Callable
42-
from typing import Any, TypeVar
42+
from typing import TypeVar, cast
4343

4444
T = TypeVar("T")
4545

@@ -57,10 +57,9 @@ def __init__(self, arr: list[T], fnc: Callable[[T, T], T]) -> None:
5757
... lambda a, b: (a[0] + b[0], a[1] + b[1])).query(0, 2)
5858
(6, 9)
5959
"""
60-
any_type: Any | T = None
6160

6261
self.N: int = len(arr)
63-
self.st: list[T] = [any_type for _ in range(self.N)] + arr
62+
self.st: list[T] = [cast(T, None) for _ in range(self.N)] + arr
6463
self.fn = fnc
6564
self.build()
6665

data_structures/heap/binomial_heap.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ def insert(self, val):
222222
if val < self.min_node.val:
223223
self.min_node = new_node
224224
# Put new_node as a bottom_root in heap
225+
assert self.bottom_root is not None
225226
self.bottom_root.left = new_node
226227
new_node.parent = self.bottom_root
227228
self.bottom_root = new_node
@@ -283,6 +284,7 @@ def delete_min(self):
283284

284285
# Update bottom root
285286
self.bottom_root = self.bottom_root.parent
287+
assert self.bottom_root is not None
286288
self.bottom_root.left = None
287289

288290
# Update min_node

data_structures/linked_list/doubly_linked_list.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,25 @@
22
https://en.wikipedia.org/wiki/Doubly_linked_list
33
"""
44

5+
from __future__ import annotations
6+
7+
from typing import Any
8+
59

610
class Node:
7-
def __init__(self, data):
11+
def __init__(self, data: Any):
812
self.data = data
9-
self.previous = None
10-
self.next = None
13+
self.previous: Node | None = None
14+
self.next: Node | None = None
1115

1216
def __str__(self):
1317
return f"{self.data}"
1418

1519

1620
class DoublyLinkedList:
1721
def __init__(self):
18-
self.head = None
19-
self.tail = None
22+
self.head: Node | None = None
23+
self.tail: Node | None = None
2024

2125
def __iter__(self):
2226
"""
@@ -93,13 +97,18 @@ def insert_at_nth(self, index: int, data):
9397
new_node.next = self.head
9498
self.head = new_node
9599
elif index == length:
100+
assert self.tail is not None
96101
self.tail.next = new_node
102+
assert self.tail is not None
97103
new_node.previous = self.tail
98104
self.tail = new_node
99105
else:
100106
temp = self.head
107+
assert temp is not None
101108
for _ in range(index):
102109
temp = temp.next
110+
assert temp is not None
111+
assert temp.previous is not None
103112
temp.previous.next = new_node
104113
new_node.previous = temp.previous
105114
new_node.next = temp
@@ -141,23 +150,32 @@ def delete_at_nth(self, index: int):
141150
if length == 1:
142151
self.head = self.tail = None
143152
elif index == 0:
153+
assert self.head is not None
144154
self.head = self.head.next
155+
assert self.head is not None
145156
self.head.previous = None
146157
elif index == length - 1:
158+
assert self.tail is not None
147159
delete_node = self.tail
148160
self.tail = self.tail.previous
161+
assert self.tail is not None
149162
self.tail.next = None
150163
else:
151164
temp = self.head
165+
assert temp is not None
152166
for _ in range(index):
153167
temp = temp.next
168+
assert temp is not None
154169
delete_node = temp
170+
assert temp.next is not None
171+
assert temp.previous is not None
155172
temp.next.previous = temp.previous
156173
temp.previous.next = temp.next
157174
return delete_node.data
158175

159176
def delete(self, data) -> str:
160177
current = self.head
178+
assert current is not None
161179

162180
while current.data != data: # Find the position to delete
163181
if current.next:
@@ -172,6 +190,8 @@ def delete(self, data) -> str:
172190
self.delete_tail()
173191

174192
else: # Before: 1 <--> 2(current) <--> 3
193+
assert current.previous is not None
194+
assert current.next is not None
175195
current.previous.next = current.next # 1 --> 3
176196
current.next.previous = current.previous # 1 <--> 3
177197
return data

data_structures/linked_list/singly_linked_list.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def __init__(self):
4545
>>> linked_list.head is None
4646
True
4747
"""
48-
self.head = None
48+
self.head: Node | None = None
4949

5050
def __iter__(self) -> Iterator[Any]:
5151
"""
@@ -153,8 +153,10 @@ def __setitem__(self, index: int, data: Any) -> None:
153153
if not 0 <= index < len(self):
154154
raise ValueError("list index out of range.")
155155
current = self.head
156+
assert current is not None
156157
for _ in range(index):
157158
current = current.next_node
159+
assert current is not None
158160
current.data = data
159161

160162
def insert_tail(self, data: Any) -> None:
@@ -215,8 +217,10 @@ def insert_nth(self, index: int, data: Any) -> None:
215217
self.head = new_node
216218
else:
217219
temp = self.head
220+
assert temp is not None
218221
for _ in range(index - 1):
219222
temp = temp.next_node
223+
assert temp is not None
220224
new_node.next_node = temp.next_node
221225
temp.next_node = new_node
222226

@@ -316,10 +320,13 @@ def delete_nth(self, index: int = 0) -> Any:
316320
self.head = self.head.next_node
317321
else:
318322
temp = self.head
323+
assert temp is not None
319324
for _ in range(index - 1):
320325
temp = temp.next_node
326+
assert temp is not None
321327
delete_node = temp.next_node
322-
temp.next_node = temp.next_node.next_node
328+
assert delete_node is not None
329+
temp.next_node = delete_node.next_node
323330
return delete_node.data
324331

325332
def is_empty(self) -> bool:

fractals/mandelbrot.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ def get_image(
109109
"""
110110
img = Image.new("RGB", (image_width, image_height))
111111
pixels = img.load()
112+
assert pixels is not None
112113

113114
# loop through the image-coordinates
114115
for image_x in range(image_width):

machine_learning/automatic_differentiation.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
from __future__ import annotations
1111

12-
from collections import defaultdict
1312
from enum import Enum
1413
from types import TracebackType
1514
from typing import Any, Self
@@ -258,7 +257,7 @@ def gradient(self, target: Variable, source: Variable) -> np.ndarray | None:
258257
"""
259258

260259
# partial derivatives with respect to target
261-
partial_deriv = defaultdict(lambda: 0)
260+
partial_deriv: dict[Variable, np.ndarray] = {}
262261
partial_deriv[target] = np.ones_like(target.to_ndarray())
263262

264263
# iterating through each operations in the computation graph
@@ -270,7 +269,10 @@ def gradient(self, target: Variable, source: Variable) -> np.ndarray | None:
270269
# of variables with respect to the target
271270
dparam_doutput = self.derivative(param, operation)
272271
dparam_dtarget = dparam_doutput * partial_deriv[operation.output]
273-
partial_deriv[param] += dparam_dtarget
272+
partial_deriv[param] = (
273+
partial_deriv.get(param, np.zeros_like(dparam_dtarget))
274+
+ dparam_dtarget
275+
)
274276

275277
if param.result_of and param.result_of != OpType.NOOP:
276278
operation_queue.append(param.result_of)

networking_flow/minimum_cut.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def mincut(graph: list[list[int]], source: int, sink: int) -> list[tuple[int, in
7171
parent = [-1] * (len(residual))
7272
res = []
7373
while bfs(residual, source, sink, parent):
74-
path_flow = float("inf")
74+
path_flow = max(max(row) for row in residual)
7575
s = sink
7676

7777
while s != source:

neural_network/input_data.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import os
2222
import typing
2323
import urllib
24+
import urllib.request
2425

2526
import numpy as np
2627
from tensorflow.python.framework import dtypes, random_seed

0 commit comments

Comments
 (0)