Unexpected Type Hint Warnings

I have a python project that I originally setup with a venv using python 3.12. I recently had to switch it to a venv using 3.11. After making the switch I have been seeing a lot of unexpected type hint warnings that I was not seeing before.

For example:

response_path = pathlib.Path("path/to/my/file")
with open(str(response_path), 'wb') as fp:
    pickle.dump(response, fp)  # WARNING: Expected type 
                               #'SupportsWrite[bytes]', got 
                               # 'BinaryIO' instead 
...
for i, box_1 in enumerate(boxes):
    for j in range(i + 1, len(boxes)):  # WARNING: Expected type 
                                        #'SupportsIndex', got 'int' 
                                        # instead
...
events: list[Event] = []
# Add some Event objects to the list
some_event = events[0].my_attribute # WARNING: Unexpected type(s): (int) 
                                    # Possible type(s): (SupportsIndex) 
                                    # (slice)

I have tried Invalidate Caches…, Repair IDE, and Restore Default Settings, but none of those resolve the issue. However, the warnings disappear if I revert back the 3.12 venv.

7

There are similar problems for me, and when I write a project, even though all the items are entered correctly, it gives an error warning under some words.

1

I should note that I also recently updated to Build #PY-242.22855.92. Even after reverting back to python 3.12 I am still seeing some of the problematic warnings. Specifically the file pointer warning about SupportsWrite[bytes].

Switching back to Build #PY-242.21829.153 appears to have fixed many/most of the issues.

2

got the same issue on Intellij idea 2024.2.2 when indexing list by an integer…

# Unexpected type(s): (int) Possible type(s): (SupportsIndex) (slice) 

1

I prefer to use the previous version for now, this bug caused a big loss in our collection.

I don't understand why the programmers need to update it when Pycharm is working well.

You also gave us a habit that we cannot work with any other similar idea. Those who use it regularly will understand what I'm talking about.

1
# Write the JSON payload to the file, explicitly specifying encoding
with open(file_path, 'w', encoding='utf-8') as out_data:
    json.dump(payload, out_data, indent=4, sort_keys=True)

PyCharm 2024.2.2 (Professional Edition)
Build #PY-242.22855.92, built on September 19, 2024

out_data in the json.dump throws the following warning:

Expected type 'SupportsWrite[str]', got 'TextIO' instead
3

I am really sorry for such an uncommitted team that we are involved in the irresponsibility of these unprofessional people. After all these days, the problem is still not fixed, it is better to use the free versions of similar companies.

-2

should work for JetBrains IDEs

if typing.TYPE_CHECKING:
    from _typeshed import SupportsWrite
    file: SupportsWrite[str]

with open(file_path, "w") as file:
    json.dump(obj=data, fp=file, indent=4)

0

Same problem having warning “Expected type 'SupportsWrite[str]', got 'TextIO' instead”. 

https://www.devgem.io/posts/resolving-pycharm-type-hint-warning-for-pickle-dump-method suggests using Pycharm Inspection Comments which works for me

# noinspection PyTypeChecker
0

(came here via a Google search…)

I just got bitten by this too, with this simple code fragment:

column_names = [chr(i + ord('A') - 1) for i in range(1, len(title_list) + 1, 1)]
last_col: str = column_names[-1]

PyCharm 2024.3 Build #PY-243.21565.199 says that every item in the range call is wrong (it expects SupportsIndex rather than int apparently), and it's the same story with the “-1” in the next line.

Looks to me that the type checker is broken, so how do I/we report that?

In the meantime I'm doing the same as “Spel” suggested above and suppressing the check where it's getting it wrong - but I really don't like to have to do that, type check is useful (usually)

0

请先登录再写评论。