As of today, the problem persists, both for pydantic's BaseModel
classes, as well as the pydantic version of @dataclass
decorator.
In case of BaseModel
, add the following piece of code to your imports:
from typing import TYPE_CHECKING
from pydantic import BaseModel
if TYPE_CHECKING:
from dataclasses import dataclass as _basemodel_decorator
else:
_basemodel_decorator = lambda x: x
Then, decorate all classes as follows:
@_basemodel_decorator
class MyClass(BaseModel):
foo: int
bar: str
Alternatively, if you are using pydantic's version of the dataclass
decorator boilerplate code is simpler:
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from dataclasses import dataclass
else:
from pydantic.dataclasses import dataclass
Then continue as usual:
@dataclass
class MyClass2:
foo: int
bar: str
More info:
Credit: https://github.com/microsoft/python-language-server/issues/1898#issuecomment-809975087
On the TYPE_CHECKING
constant: https://docs.python.org/3/library/typing.html#typing.TYPE_CHECKING
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.