Python – Handles the 79-character limit when writing python type annotations

Handles the 79-character limit when writing python type annotations… here is a solution to the problem.

Handles the 79-character limit when writing python type annotations

While writing the script, I ended up with the function signature shown below

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

To comply with PEP8, I split the definition as follows

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

But for type annotations, this line is too long. What is the usual way to deal with this problem?

Solution

You can define the annotation first, and then write your function:

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

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

Related Problems and Solutions