Python type hints and context managers

后端 未结 4 1795
执念已碎
执念已碎 2020-12-15 15:05

How should a context manager be annotated with Python type hints?

import typing

@contextlib.contextmanager
def foo() -> ???:
    yield

4条回答
  •  不知归路
    2020-12-15 15:59

    The return type of the function wrapped by a context manager is Iterator[None].

    from contextlib import contextmanager
    from typing import Iterator
    
    @contextmanager
    def foo() -> Iterator[None]:
        yield
    

提交回复
热议问题