Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

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

75%
+4 −0
Q&A 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...

2 answers  ·  posted 2y ago by true_blue‭  ·  last activity 2y ago by Moshi‭

Question python mypy
#2: Nominated for promotion by user avatar Alexei‭ · 2022-12-29T07:59:16Z (almost 2 years ago)
#1: Initial revision by user avatar true_blue‭ · 2022-12-28T00:56:31Z (almost 2 years ago)
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?