Right to the point, here below is the sample code which will raise error in PyCharm:
list1 = [0] * 5
list1[0] = ''
list2 = [0 for n in range(5)]
list2[0] = ''
PyCharm then return an error on both line 2 and line 4 as below:
Unexpected type(s):(int, str)Possible type(s):(SupportsIndex, int)(slice, Iterable[int])
Running the code would not cause any error, but PyCharm keep raising the above message when I am coding. I wonder why PyCharm would give this error and how can I solve this with cleanest code. Thanks.
Edit: Sorry here was a typo in line 3 of the sample code which has been corrected.(list2 = [0 for n in range(0)]
should be list2 = [0 for n in range(5)]
)
CodePudding user response:
In your case PyCharm sees you first line and thinks that the type of list is List[int]
. I mean it is a list of integers.
You may tell that you list is not int-typed and can accept any value this way:
from typing import Any, List
list1: List[Any] = [0] * 5
list1[0] = ''
I used typing module just to explain the idea. Here is a simple way to announce that list1 is just a list of anything:
list1: list = [0] * 5
list1[0] = ''
Also consider to use as strict typing as possible. It may help you to prevent bugs.
If you need both strings and ints then use this:
from typing import List, Union
list1: List[Union[int, str]] = [0] * 5
# Starting from Python 3.10 you may use List[int|str] instead
Docs here: https://docs.python.org/3/library/typing.html
CodePudding user response:
this is just a warning from the code inspection tool.
It is basically saying that you created a list of integers (containing five items equal to 0
), and then you are replacing one integer with a string.
This does not generate errors in python, as it is extremely flexible with the data types, and you can generate list with different data types if you want.
It is just warning you that something might go wrong if you use that list later in some code that expects only integers and finds instead an item that is a string.
You can solve this by disabling the inspection, or by creating directly a list of strings.
list1 = [""] * 5
list2 = ["" for n in range(0)]
or also
list1 = ["0"] * 5
list2 = ["0" for n in range(0)]
Or you can just ignore the warning, provided that you keep in mind that the list might contain different data types.
CodePudding user response:
As others have said, this is just a warning that tells you to take care.
If you want to be explicit about your list taking arbitrary values, you can provide an appropriate type hint:
from typing import Any, List
list1: List[Any] = [0] * 5
list1[0] = ''
If you intend to put either integers or strings, but nothing else in the list you can also do list1: List[int|str] = [0] * 5
starting with Python 3.10, or use the clunky typing.Union
on older versions.
CodePudding user response:
You can't assign to a list like list2
unless you've already initialized the list with n 1
elements.
list2 = [0 for n in range(0)]
is the same as list2 = []
. Try using append
to add elements to the end of the list
>>> list2.append('')
>>> list2[0]
''