pydantic dataclasses intelliSense在vscode中

发布于 2025-01-24 11:43:23 字数 1141 浏览 0 评论 0原文

我在继承的Pydantic Dataclass上收到Intellisense错误。我能够将其复制到两个操作系统,一个在Vscode中,另一个在Pycharm中。

您可以使用此片段复制IntelliSense错误:

from abc import ABC, abstractmethod
from pydantic import BaseModel, dataclasses
from typing import List

# python --version -> Python 3.10.2
# pydantic=1.9.0
# Running on MacOS Monterey 12.3.1


@dataclasses.dataclass(frozen=True, eq=True)
class PersonABC(ABC):
    name: str

    @abstractmethod
    def print_bio(self):
        pass

@dataclasses.dataclass(frozen=True, eq=True)
class Jeff(PersonABC):
    age: int

    def print_bio(self):
        print(f"{self.name}: {self.age}")     # In VScode, self.name is not registered as a known variable:
                                              # `Cannot access member "name" for type "Jeff"
                                              # Member "name" is unknownPylancereportGeneralTypeIssues`

class Family(BaseModel):
    person: List[PersonABC]


jeff = Jeff(name="Jeff Gruenbaum", age=93)
print(Family(person=[jeff]))

该错误发生在实现的 print_bio 函数中。完整的错误是上面的评论。 有没有办法解决此问题并恢复Intellisense?

I am receiving an intellisense error on an inherited pydantic dataclass. I was able to replicate this on two operating systems, one in VSCode, the other in pycharm.

You can replicate the intellisense error with this snippet:

from abc import ABC, abstractmethod
from pydantic import BaseModel, dataclasses
from typing import List

# python --version -> Python 3.10.2
# pydantic=1.9.0
# Running on MacOS Monterey 12.3.1


@dataclasses.dataclass(frozen=True, eq=True)
class PersonABC(ABC):
    name: str

    @abstractmethod
    def print_bio(self):
        pass

@dataclasses.dataclass(frozen=True, eq=True)
class Jeff(PersonABC):
    age: int

    def print_bio(self):
        print(f"{self.name}: {self.age}")     # In VScode, self.name is not registered as a known variable:
                                              # `Cannot access member "name" for type "Jeff"
                                              # Member "name" is unknownPylancereportGeneralTypeIssues`

class Family(BaseModel):
    person: List[PersonABC]


jeff = Jeff(name="Jeff Gruenbaum", age=93)
print(Family(person=[jeff]))

The error takes place in the implemented print_bio function. The full error is in the comments above.
Is there a way to resolve this and gain back the intellisense?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光暖心i 2025-01-31 11:43:23

我能够通过从basemodel继承而不是使用 pydantic.dataclasses.dataclass 来解决此问题。我相信这是Pydantic Dataclasses或Pylance的错误,但是使用基本模型是一个可行的解决方案。

from abc import ABC, abstractmethod
from typing import List
from pydantic import BaseModel

# python --version -> Python 3.10.2
# pydantic=1.9.0
# Running on MacOS Monterey 12.3.1


class PersonABC(ABC, BaseModel):
    name: str

    @abstractmethod
    def print_bio(self):
        pass

class Jeff(PersonABC):
    age: int

    def print_bio(self):
        print(f"{self.name}: {self.age}")     # Now the intellisense error does not appear.

class Family(BaseModel):
    person: List[PersonABC]


jeff = Jeff(name="Jeff Gruenbaum", age=93)
print(Family(person=[jeff]))

I was able to resolve this by inheriting from BaseModel, instead of using pydantic.dataclasses.dataclass. I believe this is a bug with pydantic dataclasses or with Pylance, but using the BaseModel is a workable solution.

from abc import ABC, abstractmethod
from typing import List
from pydantic import BaseModel

# python --version -> Python 3.10.2
# pydantic=1.9.0
# Running on MacOS Monterey 12.3.1


class PersonABC(ABC, BaseModel):
    name: str

    @abstractmethod
    def print_bio(self):
        pass

class Jeff(PersonABC):
    age: int

    def print_bio(self):
        print(f"{self.name}: {self.age}")     # Now the intellisense error does not appear.

class Family(BaseModel):
    person: List[PersonABC]


jeff = Jeff(name="Jeff Gruenbaum", age=93)
print(Family(person=[jeff]))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文