pydantic dataclasses intelliSense在vscode中
我在继承的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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我能够通过从basemodel继承而不是使用 pydantic.dataclasses.dataclass 来解决此问题。我相信这是Pydantic Dataclasses或Pylance的错误,但是使用基本模型是一个可行的解决方案。
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.