Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

687
Views
Unexpected type warning raised with list in PyCharm

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)])

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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

over 4 years ago · Santiago Trujillo Report

0

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.

over 4 years ago · Santiago Trujillo Report

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!