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.
请先登录再写评论。
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:
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.
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.
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)
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.
Reid
Download 2024.1
https://www.jetbrains.com/pycharm/download/other.html#2024.1
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)
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
(came here via a Google search…)
I just got bitten by this too, with this simple code fragment:
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)
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.