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.

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

Parent

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 attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

Post
+3
−2

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 attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

Scoping (3 comments)
Scoping
Moshi‭ wrote almost 2 years ago

I wouldn't go so far as to say that Python's scoping rules are broken. For instance, let's take a simple "Find a value" example:

for value in my_list:
    if some_check(value):
        result = value

# Use result here

In other languages, you would declare result outside the loop with let or var or something. E.g. something like

result
for value in my_list:
    if some_check(value):
        result = value

But wait, expressions can be used as statements (that's how function calls work), so Python has no way of knowing that we are trying to declare rather than just access result. And anyway, this is inelegant from a pythonic view. A declaration is just a useless statement in that it does nothing of note, so it is noise (Which is why Python just has assignment). As such, Python just decides that having variables be function rather than block scoped is neater.

Derek Elkins‭ wrote almost 2 years ago

And yet here we are with someone caught off guard by them. Most languages don't do what Python does and JavaScript, which has/had vaguely similar scoping to Python's, has moved away from this and toward block scoping.

If declarations change the behavior of the code, then they are not useless. The example you give is a great example of why Python's approach is a bad idea. If my_list is empty or some_check(value) is never true, then result is never defined. Handling this is not as simple as checking result is None. Requiring declaration to extend the scope would clearly have avoided the undefined variable case but also likely would have tipped the author off about this case.

There are plenty of ways Python could have handled this better with no additional annotation burden or at least having any annotations be optional and concise to distinguish between different scoping behavior.

I used "bad/broken" exactly because one can argue that any behavior is "just a design choice".

Moshi‭ wrote almost 2 years ago

Fair enough. In most cases however, it doesn't really matter what scope something is in since it's rather rare (and questionable to do) that you want two identically named variables in the same function in the first place.

Is it bad? Depends on one's sensibilities. I just wanted to make the case that it wasn't broken in the sense that there are reasons for its choices, even if one might not agree with them.