Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
What's causing mypy to give an `[assignment]` error in this nested for loop?
I started adding types to my (working) solution to Exercism's "Kindergarten Garden" exercise, to learn how typing with python and Mypy (strict) works. While doing so, I ran into a Mypy error that I can't figure out how to solve. Here's the full code:
class Garden:
SEEDCHAR_TO_SEED = {
"C": "Clover",
"G": "Grass",
"R": "Radishes",
"V": "Violets",
}
# Student list given when "students" isn't provided when initting instance of Garden
DEFAULT_STUDENTS = [
"Alice",
"Bob",
"Charlie",
"David",
"Eve",
"Fred",
"Ginny",
"Harriet",
"Ileana",
"Joseph",
"Kincaid",
"Larry",
]
# Init the instance of garden (params filled in when called)
def __init__(self, diagram: str, students: list[str] = DEFAULT_STUDENTS):
self.diagram: list[str] = diagram.splitlines()
# create the full seed garden (rather than using seedchars) in init
self.seed_diagram: list[list[str]] = []
for row in self.diagram:
self.seed_diagram.append([self.SEEDCHAR_TO_SEED[seed] for seed in row])
# Ensure that the list of students is in the correct (i.e. alphabetical) order
self.students: list[str] = sorted(students)
self.results: dict[str, list[str]] = {}
# Populate self.results with keys as each student, and values being empty lists
for student in self.students:
self.results[student] = []
for student in self.students:
for row in self.seed_diagram:
for seed in row[
self.students.index(student) * 2 : self.students.index(student) * 2
+ 2
]:
self.results[student].append(seed)
def plants(self, student: str) -> list[str]:
return self.results[student]
running mypy --strict --pretty
on the file gives this error:
kindergarten_garden.py:37: error: Incompatible types in assignment (expression has type "List[str]", variable has type "str") [assignment]
for row in self.seed_diagram:
^
Found 1 error in 1 file (checked 1 source file)
I've been running the code in a debugger to try to find the issue, but I don't see any assignments or expressions that could cause the error. All the types look correct. Am I missing something?
2 answers
The following users marked this post as Works for me:
User | Comment | Date |
---|---|---|
true_blue | (no comment) | Dec 31, 2022 at 18:04 |
The problem is that you are using row
twice with different types.
for row in self.diagram: # row is str here
for row in self.seed_diagram: # row is list[str] here
Renaming one or the other makes the error go away.
Why does this happen? Python has some somewhat counterintuitive scoping rules. Take the following example:
def foo():
l = [["hello"], ["world"]]
for x in l:
for y in x:
pass
print(y)
foo()
The foo
call actually prints world
! In Python, loops (and other conditionals) do not create a new block scope, and so in fact both row
variables in your code are one and the same, scoped to the function, and so mypy gets upset when you try to use it with different types.
0 comment threads
The following users marked this post as Works for me:
User | Comment | Date |
---|---|---|
true_blue | (no comment) | Dec 31, 2022 at 18:04 |
As the error message indicates, the assignment is to the variable row
. The for
loop will repeatedly assign to row
.
But why is there a problem? Because Python's scoping rules are bad/broken. There is no block scoping. The scope of any variable within a function is that entire function except for the bodies of nested functions. The scope of the iterating variable in a for
loop is not just the for
loop. The scope of variables within a block, e.g. within the body of a for
loop, is not just thatfor
loop. There is no shadowing of variables except by nested functions.
The upshot for your example is that you use row
in two different for
loops looping over two differently typed expressions, so row
ends up being assigned two different types because there is only one row
variable.
Specifically, for row in self.diagram:
leads to row
having type str
, but for row in self.seed_diagram:
would assign row
with a value of type list[str]
. Hence the error.
The most direct resolution would be to use a different variable name for each of these loops.
0 comment threads