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

80%
+6 −0
Q&A What's causing mypy to give an `[assignment]` error in this nested for loop?

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 m...

posted 2y ago by Moshi‭  ·  edited 2y ago by Moshi‭

Answer
#2: Post edited by user avatar Moshi‭ · 2022-12-28T01:38:08Z (almost 2 years ago)
Python's initialization-is-declaration philosophy makes this wording a bit confusing
  • The problem is that you are declaring `row` twice.
  • ```py
  • for row in self.diagram: # row is str here
  • ```
  • ```py
  • 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 odd scoping rules. Take the following example:
  • ```py
  • 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.
  • The problem is that you are using `row` twice with different types.
  • ```py
  • for row in self.diagram: # row is str here
  • ```
  • ```py
  • 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:
  • ```py
  • 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.
#1: Initial revision by user avatar Moshi‭ · 2022-12-28T01:34:51Z (almost 2 years ago)
The problem is that you are declaring `row` twice.

```py
for row in self.diagram: # row is str here
```

```py
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 odd scoping rules. Take the following example:

```py
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.