正确使用 Generator 类型

mic*_*.io 18 python generator python-3.x

我正在尝试将类型添加到返回生成器的方法中。每当我使用指定的返回类型运行此程序时,都会引发 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 9, in <module>
    from .marshaller import Marshaller
  File "./src/marshaller.py", line 9, in <module>
    class Marshaller:
  File "./src/marshaller.py", line 29, in Marshaller
    def inbox_files(self) -> Generator[RecordsFile]:
  File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/typing.py", line 254, in inner
    return func(*args, **kwds)
  File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/typing.py", line 630, in __getitem__
    _check_generic(self, params)
  File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/typing.py", line 208, in _check_generic
    raise TypeError(f"Too {'many' if alen > elen else 'few'} parameters for {cls};"
TypeError: Too few parameters for typing.Generator; actual 1, expected 3
Run Code Online (Sandbox Code Playgroud)

¯\_(?)_/¯

che*_*ner 21

您必须明确指定发送类型和返回类型,即使两者都是None.

def inbox_files(self) -> Generator[RecordsFile,None,None]:
    return self._search_directory(self._inbox)
Run Code Online (Sandbox Code Playgroud)

请注意,yield 类型是您可能认为的返回类型。发送类型是您可以传递给生成器send方法的值的类型。返回类型是在产生所有可能的值后可以嵌入到StopIteration引发的异常中的值的类型next。考虑:

def foo():
    yield 3
    return "hi"

f = foo()
Run Code Online (Sandbox Code Playgroud)

第一次调用next(f)将返回 3;第二个会提高StopIteration("hi")。)


您无法发送或返回的生成器只是一个可迭代或迭代器(显然可以使用)。

def inbox_files(self) -> Iterable[RecordsFile]:  # Or Iterator[RecordsFile]
    return self._search_directory(self._inbox)
Run Code Online (Sandbox Code Playgroud)

_search_directory本身返回一个生成器/可迭代对象,而不是一个实例RecordsFile

def _search_directory(self, directory: str) -> Iterable[RecordsFile]:
Run Code Online (Sandbox Code Playgroud)

  • 为了后代回答我自己,它似乎记录在这里:https://docs.python.org/3/library/typing.html#typing.Generator (3认同)