Julian
Julian

Reputation: 222

Unexpected type(s): (int, int) Possible type(s): (SupportsIndex, None) (slice, Iterable[None])

What's wrong with this code:

split_list = [3600, 3600, 3600, 3600, 3600, 3600, 3600, 3600, 45]
split_list2 = [None, None, None, None, None, None, None, None, None, None, None, None]
result = [3600, 3600, 3600, 3600, 3600, 3600, 3600, 3600, 45, None, None, None]

for i in range(len(split_list)):
    split_list2[i] = split_list[i]

In PyCharm it issues a warning;

Unexpected type(s): (int, int) Possible type(s): (SupportsIndex, None) (slice, Iterable[None])

But the script runs just fine and this code works exactly as I expected. I don't like warnings in my IDE though, any quick fixes?

Upvotes: 8

Views: 10858

Answers (3)

Tippman
Tippman

Reputation: 67

Marking the directory with venv as "excluded" helped me. Do not forget to reboot app.

Upvotes: -1

Kre4
Kre4

Reputation: 95

I faced a similar problem but with dict:

def test(my_api, request_data, expectation):
        with expectation as error:
            response = my_api.get_by_id(request_data['id'])
#                                                    ^
# ---------------------------------------------------|
# Unexpected type(s): (str) Possible type(s): (SupportsIndex) (slice)(SupportsIndex) (slice)

The problem was solved by explicitly specifying the argument type:

def test(my_api, request_data: dict, expectation):

I think that in the case of an array you can also specify type:

split_list: list = [3600, 3600, 3600, 3600, 3600, 3600, 3600, 3600, 45]

Upvotes: 0

bad_coder
bad_coder

Reputation: 12940

This warning is solved by updating to PyCharm to 2021.2.2.

It seems to be a bug in earlier versions of the IDE's static type checker.

One user reported in the comments that this bug regressed in the PyCharm 2021.2.3 release.

I just tested it again using PyCharm 2022.1 Professional Edition and the bug has again been solved. Here's a screenshot:

screenshot of code in IDE's editor window

Upvotes: 7

Related Questions