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.

What's causing mypy to give an `[assignment]` error in this nested for loop?

+4
−0

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?

History
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

2 answers

+6
−0

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.

History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+3
−1

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.

History
Why does this post require moderator attention?
You might want to add some details to your flag.

1 comment thread

Scoping (3 comments)

Sign up to answer this question »