1

Function runs perfectly, but I am getting error with mypy:

Unsupported target for indexed assignment

Here is my code:

def function(image: List[str], start: Tuple[int]):
    if image[start[0]][start[1]] == "*":
        return
    else:
        image[start[0]][start[1]] = "*"
        if start[0] > 0:
            function(image, [start[0]-1, start[1]])
        if start[0] < len(image) - 1:
            function(image, [start[0]+1, start[1]])
        if start[1] > 0:
            function(image, [start[0], start[1]-1])
        if start[1] < len(image) - 1:
            function(image, [start[0], start[1]+1])

The mistake is in line:

image[start[0]][start[1]] = "*"

image = ["*", "."] start = (1, 1) - tuple contains coordinates.

MV912HB
  • 51
  • 1
  • 6
  • 2
    With ``image: List[str]``, ``image[start[0]]`` is just a ``str``. Strings are immutable, their elements cannot be assigned to. – MisterMiyagi Dec 02 '20 at 20:49
  • yes, because `image[start[0]][start[1]] = "*"` does not support indexed assignment if `image: List[str]`. What are you *actually* passing to your function? – juanpa.arrivillaga Dec 02 '20 at 21:25
  • @MV912HB please provide an actual example in the question itself. – juanpa.arrivillaga Dec 02 '20 at 21:44
  • Can you please clarify how this code is supposed to work? The example input of ``image = ["*", "."]``, ``start = (1, 1)`` fails in the first line of the function, because it tries to access the second item of ``"*"`` – which does not exist. Is ``image`` actually supposed to be a two-dimensional list, as in ``function([['*', "*"], ["*", "*"]], (1, 1))``? – MisterMiyagi Dec 03 '20 at 08:28
  • 2
    A two-dimensional list is ``List[List[str]]``, not ``List[str]``. – MisterMiyagi Dec 03 '20 at 09:59
  • @MisterMiyagi Thanks, you helped me a lot! I did not pay attention. I am new to python, tried coding 6 weeks ago for the very first time. And correct type for image is Tuple[int, int]. – MV912HB Dec 03 '20 at 15:24

2 Answers2

4
self._balances:Mapping[str,Optional[float]]

If anyone comes here because of using Mapping, use MutableMapping instead, because you can only assign values to a mutable object

self._balances:MutableMapping[str,Optional[float]]
Peter
  • 773
  • 1
  • 7
  • 23
Ajay Tom George
  • 1,890
  • 1
  • 14
  • 26
0

Problem solved.
The correct types are:

image: List[List[str]], start: Tuple[int, int]
MV912HB
  • 51
  • 1
  • 6