[英]Type hints for Type[dataclass] is not working in Pycharm
Please check the following simple code.请检查以下简单代码。
@dataclass
class FooData:
bar: int
baz: str
FooData(1, 's') # works fine
def through() -> Type[FooData]:
return FooData
DataClass = through()
DataClass(1, 's') # warning, unexpected arguments
Type hint for the function or method with return Type[Dataclass] is not working. function 的类型提示或返回 Type[Dataclass] 的方法不起作用。
Is it a PyCharm bug or am I doing something wrong?是 PyCharm 错误还是我做错了什么?
Pycharm version: PyCharm 2021.3.3 (Professional Edition) Pycharm版本:PyCharm 2021.3.3(专业版)
Seems to be a problem with the way PyCharm handles the decorator. PyCharm 处理装饰器的方式似乎有问题。 I have the same issue.
我有同样的问题。 Doing the same thing, but also specifying the
__init__
method accordingly makes it behave as expected.做同样的事情,但也相应地指定
__init__
方法使其行为符合预期。
I would suggest using of Pydantic models instead.我建议改用Pydantic模型。 They do the same thing, are handled properly and provide a bunch of nice additional features, including but not limited to automatic validation.
它们做同样的事情,处理得当,并提供了许多不错的附加功能,包括但不限于自动验证。 The only difference is that they require you to initialize using keyword-arguments.
唯一的区别是它们要求您使用关键字参数进行初始化。
Here is an example.这是一个例子。
from pydantic import BaseModel
from typing import Type
class FooData(BaseModel):
bar: int
baz: str
FooData(bar=1, baz='s') # works fine
def through() -> Type[FooData]:
return FooData
Model = through()
Model(bar=1, baz='s') # also works fine
Other than that, this warrants a ticket in the JetBrains Issue Tracker.除此之外,这保证了 JetBrains 问题跟踪器中的一张票。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.