我正在尝试将类型添加到返回生成器的方法中。每当我使用指定的返回类型运行此程序时,都会引发 TypeError。
添加引号或删除输入可修复错误,但这似乎是 hack。当然,有一种正确的方法可以做到这一点。
def inbox_files(self) -> "Generator[RecordsFile]":
...
# OR
def inbox_files(self):
...
Run Code Online (Sandbox Code Playgroud)
from typing import Generator, List
from .records_file import RecordsFile
Class Marshaller:
...
def inbox_files(self) -> Generator[RecordsFile]:
return self._search_directory(self._inbox)
def _search_directory(self, directory: str) -> RecordsFile:
for item_name in listdir(directory):
item_path = path.join(item_name, directory)
if path.isdir(item_path):
yield from self._search_directory(item_path)
elif path.isfile(item_path):
yield RecordsFile(item_path)
else:
print(f"[WARN] Unknown item found: {item_path}")
Run Code Online (Sandbox Code Playgroud)
生成以下堆栈跟踪:
Traceback (most recent call last):
File "./bin/data_marshal", line 8, in <module>
from src.app import App
File "./src/app.py", line …Run Code Online (Sandbox Code Playgroud)