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
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...
Answer
#2: Post edited
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
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.