处理 python 类型注释时的 79 个字符限制

Dealing with the 79 character limit when writing python type annotations

在编写脚本时,我最终得到了如下所示的函数签名

def do_multiprocess_action(some_argument: str, communication_pipe: typing.Optional[multiprocessing.connection.Connection]) -> subprocess.Popen:

为了符合 PEP8,我将定义拆分如下

def do_multiprocess_action(some_argument: str,
                           communication_pipe: typing.Optional[multiprocessing.connection.Connection]
                           ) -> subprocess.Popen:

但是使用类型注释,这一行太长了。处理此问题的惯用方法是什么?

您可以先定义注释,然后再编写函数:

MultiprocessingConnection = typing.Optional[
    multiprocessing.connection.Connection]

def do_multiprocess_action(some_argument: str, 
    communication_pipe: MultiprocessingConnection) -> subprocess.Popen: