我有这个代码,但让我烦恼的是我必须进行f
两次转换:
with h5py.File(computed_properties_path, "r") as f:
# get the set of computed metrics
computed_metrics = set()
# iterating through the file iterates through the keys which are dataset names
f = cast(Iterable[str], f)
dataset_name: str
for dataset_name in f:
# re-cast it as a file
f = cast(h5py.File, f)
dataset_group = index_hdf5(f, [dataset_name], h5py.Group)
for metric_name in dataset_group:
logger.info(f"Dataset: {dataset_name}, Metric: {metric_name}")
我只是想告诉静态类型检查器,如果我遍历一个文件,我将得到字符串(它们是文件中组和数据集的键)。
我尝试创建这个.pyi
存根来创建一个执行此操作的类,但出现错误,提示文件未定义。我猜这是因为 Pylance 现在仅依赖于我的存根,而不是在原始文件中查找额外的定义。
我已经通过 Claude 和 ChatGPT 尝试了很多不同的选项,但似乎无法弄清楚如何扩展类型提示,以便 Pylance 知道遍历h5py.File
对象会产生字符串。