Updated from 2024.2.2 RC to 2024.2.2 and now PyCharm cant infer types

Hi,

I was on PyCharm 2024.2.2 (Professional Edition) RC and moved to plain PyCharm 2024.2.2 (Professional Edition) using the JetBrains Toolbox.

I am working on a project where we are diligent about making sure that our code is well typed btw, so almost everything has a proper type.

Now my editor stopped being able to infer types almost all the time.

This means that refactoring struct fields (like Pydantic models) leaves behind initializers without the correct names (the old names) and even stuff like `my_var: MyType = MyType(…)` leaves behind an `Any` as the type, so, Cmd+hovering the variable (or even the Typehint), just shows `Inferred type: Any` on the tooltip.

In other places, it does infer the type but does not highlight that a field does not exist within that type, for example :

```
person = Person("Joao")
return dict(person_name=person.potatoes)
``` 
Does not highlight “potatoes” as not existing, although its inferred, and if o Cmd+click on “potatoes” it does show “Cannot find declaration to go to”.

Just for testing highlighting, I put a return statement and the code imediatly after it was highlighted as “unreachable code”.

 

Something got broken in the move, I did try “Repair IDE” and “Invalidate caches”, let it restart and re-index and re-scan but didnt change anything.

What am i missing?

1
Hello,
Have you tried Invalidating Caches with all options toggled? If no, please try with all options checked. 
If this doesn't help, could you please share idea.log? You can upload it to our secure file hosting and tell us the upload ID: https://uploads.services.jetbrains.com/
0

Hi!

Yes I have. Both “Repair IDE” and “Invalidate caches” as stated previously. With all options checked.

Here is the file:

Upload id: 2024_09_27_u2VWZj5NPaqjgfABe5uxAz (file: pycharm-logs-20240927-15454314081168136680752015.zip)

1
Thank you. I see some interpreter-related exceptions; please check if creating a new virtual environment helps to resolve the problem. 
If the problem remains unresolved, please share a simplified version of your project and an environment file for your Python interpreter so we can check the problem on our side.
0

Hi,

Could we setup a screensharing session instead? My project is a monorepo and its quite large.

We are also on a tight deadline right now, and I dont really have time to thinker too much.

0
Unfortunately, scheduling such a session might take a lot of time due to a significant load.  
Is it possible to pinpoint the exact code fragment/file where the issue is reproducible? Could it be related to particular libraries (typing, dataclass, etc.)?
0

Hi, I dont believe so. This happens mostly everywhere. I havent been able to pinpoint a culprit yet.

 

In this specific project we use always one of these three: 

- Normal python classes as in, exactly “class InvoiceDataAggregator:” with an `__init__` and typed parameters, a very few public methods and a lot more private ones. 

- Pydantic Models (for DTOs), exactly as “class InvoiceLineData(PydanticModel):” where `PydanticModel` is a base class with some default `ConfigDict` options (for forcing validation and forbidding extra fields) and a few helper functions.

- SqlAlchemy entities (classes) exactly as “class Invoice(Base, CommonFieldsMixin, AsDictMixin):”

 

Maybe 90% of our code base explicitly uses types in method parameters and return values, models/entities, etc. Which greatly helps us in finding typos and exploring the code base (we jump to definition, references and do refactoring using the tooling, etc).

 

While writing this, I opened up a small TUI app test (completely standalone project) I made recently and noticed that if I rename field in a Pydantic model it is using I see no change except when I hover the field with CMD pressed I dont get a blue highlighting anymore (same as I mentioned previously) instead of a proper yellow highlighting (unknown field) like I would normally expect. But if I use the “Refactor rename” it does update everywhere else in the app and “undo” correctly reverts the change between multiple files.

I dont know if this is useful or not.

 

Please do let me know.

 

Thanks! 

0
Sorry for the delayed response.
I reproduced the problem,and I think it is already reported: https://youtrack.jetbrains.com/issue/PY-56624/PyCharm-does-not-infer-type-for-pydantic-model-fields-fields-type-although-provided-in-sources
Please vote for it to raise its relevance and receive updates regarding its status.
0

Hi, double check if that Pydantic issue isnt covered by this plugin: https://plugins.jetbrains.com/plugin/12861-pydantic

I dont have the issue with just Pydantic classes though.

0

请先登录再写评论。