Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

typing.ClassVar in model is no longer understood / recognised - False unresolved attribute reference #899

Closed
skewty opened this issue Mar 15, 2024 · 13 comments · Fixed by #919
Labels
bug Something isn't working

Comments

@skewty
Copy link

skewty commented Mar 15, 2024

I am seeing a regression in the newest plugin build / version.
ClassVar regression.

from pydantic import BaseModel, RootModel

class Foo(RootModel):
    TEST: ClassVar[str] = "Hello World"

class Bar(BaseModel):
    TEST: ClassVar[str] = "Hello World"
    
print(Foo.TEST)  # TEST is underlined as warning - unresolved attribute reference
print(Bar.TEST)  # TEST is underlined as warning - unresolved attribute reference

I can Ctrl-Click on TEST and it goes to the correct place in source code and it pulls up correct documentation on hover for that value. It is odd that the inspection comes up.

@skewty
Copy link
Author

skewty commented Mar 17, 2024

Perhaps related to #894

@viktorkertesz
Copy link

I have similar problem with this code:

class Test(BaseModel):
    _url: str = "https://someurl"

    def get(self):
        print(self._url)  # Unresolved attribute reference '_url' for class 'Test' 

I suspect bugs with #858. Maybe private attributes shouldn't be checked as those are generally ignored by Pydantic anyway.

@skewty, I believe #894 is solved in latest release, I couldn't reproduce it.
Perhaps ClassVar type hint and private attributes are handled differently from normal attributes and cause these problems.

@GitToby
Copy link

GitToby commented Mar 19, 2024

getting the same as @viktorkertesz since update to 0.4.12

@chbndrhnns
Copy link

This also relates to inner classes:

from pydantic import BaseModel


class Model(BaseModel):
    class Inner(BaseModel):
        user: str
        group: str
    vlan_id: int
    name: str


def test_():
    Model.Inner()

@skewty
Copy link
Author

skewty commented Mar 20, 2024

cls.__name__ is not recognised either (plug-in version 0.4.12)

@skewty
Copy link
Author

skewty commented Mar 20, 2024

@koxudaxi if you could give me a quick intro / tour of the plug-in code and where you expect the issue to be, I'd be willing to help out with some of these fixes.. I am expecting my work load to drop off some at the end of this week and I can probably dig into this one.

@koxudaxi
Copy link
Owner

Sorry I have not been able to respond due to a pile of tasks.

Direct warnings are output here.

registerProblem(node.node.lastChildNode.psi, "Unresolved attribute reference '${name}' for class '${pyClass.name}' ")

Currently, classVar is excluded from the pydantic field support in the filter here.

.filterNot { PyTypingTypeProvider.isClassVar(it, context) }

Perhaps an additional argument is needed to disable this filterNot only when called from the inspection.

I want to do this when I have time, but it is a bit difficult to do today.

@koxudaxi koxudaxi added the bug Something isn't working label Mar 20, 2024
@chbndrhnns
Copy link

I think the case with an inner class worked before the last release. My use case is a builder class that I put as an inner class to a model

@skewty
Copy link
Author

skewty commented Apr 10, 2024

I have sat down a few times to learn how this plugin works but find myself lacking the time to learn everything / enough to fix this issue. I started with some videos on YouTube and learned about PSI and ... this plugin / your work was featured in an interview for a reason.. It may very well be one of their most complex / fancy plugins. Congrats.

@mcskatkat
Copy link

+1 this issue 👍🏻

My code is now full of # noinspection Pydantic 😢

Any kind of getattr on a pydantic BaseModel subclass is now flagged as Unresolved attribute.

Includes (probably partial list):

  • __name__ (or any other dunder attr)
  • essential pydantic implanted model_* attrs, such as .model_fields and .model_config
  • assigned names in the class body that pydantic intentionally leaves alone to remain class attributes, including annotated with ClassVar and private names (_not_a_field: int).

@koxudaxi
Copy link
Owner

I got help from Jetbrains to re-enable unittest in 2024.1.
#917
I will be able to try to fix the issue.

@koxudaxi
Copy link
Owner

@skewty
Thank you for spending your time trying to resolve the issue.
We should improve the code to sort existing complex code.
For example, PyCharm uses PSI stubs for dataclass.

I know everyone requests me for bug fixes and additional features for my OSS, including the plugin.
But, It isn't easy to get the time :(
I work as a full-time software engineer during the day, so I can only work for OSS after the kids go to bed.
I know this is a rather critical problem, but I can't quite come up with a concrete plan to develop OSS full-time.
If anyone has any good ideas for full time OSS development, please let me know.
It's a more difficult task than creating valuable software 😅

I have the critical task of making slides for next month's PyConUS talk this week, but I promise to start working on the issue this weekend.

@skewty
Copy link
Author

skewty commented Apr 11, 2024

Open-Source Exploitation - https://youtu.be/9YQgNDLFYq8?si=fkC41JH0iFwLoR77&t=1325

I feel I understand. I don't want to burn you out and I don't want this to become undesirable to support.

For others

If this bug affects you greatly, you should probably be supporting this project financially.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants