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.
Post History
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...
#1: Initial revision
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: ```python 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?