我正在开发一个 python 包,它可以让一个人从
dataclass
和 YAML
文件创建 python JSON
-es。
这个想法是减少样板文件,并为文件中定义的类型提供运行时类型检查。dataclass
.
例子:
from anton import yaml_conf
@yaml_conf(conf_path="/path/to/a/yaml_file.yaml")
class Example:
x: int
y: float = 3.14
obj = Example()
obj.x # Outputs what is there in the file and ensures the type is an integer value.
obj.y # Outputs what is there in the file and ensures the type is an floating point value.
目前,我已经实现了一些类型的解析和类型检查,并计划添加更多。
当前版本的 API 只允许用户始终从单个文件加载。 我想让用户在程序运行时的任何时候从任何文件加载。
代码库中的这个函数进行加载和类型检查以及对象创建。
我想修改它,以便用户可以将路径作为 obj 初始化的一部分传递,例如:
obj = Example() # Uses default file mentioned in the decorator
obj = Example("/path/to/a/new/file.yaml")
这是我目前实施的改变。
这似乎可以让用户为不同的初始化传递不同的文件。
我希望 python 类型检查器知道一个新参数已添加到该
modified_init
函数,因此当我尝试传入新文件路径时不会抛出类型错误。
在最近的更改之前,类型检查器(幸运的是)知道不需要参数。
Pylance 错误信息:
Expected no arguments to "Class" constructor
.
mypy错误信息:
error: Too many arguments for "Class" [call-arg]
.
问题:我如何克服这个问题并让类型检查器知道它必须期待一个参数?