[Python] Use return type of other function for type annotation

Discussão em 'Python' iniciado por Stack, Maio 20, 2025.

  1. Stack

    Stack Membro Participativo

    I want to implement a function that creates a CSV reader and annotate it correctly.

    Simplified example:

    import csv

    def create_my_csv( fh ) -> WHAT_IS_NEEDED_HERE?:
    return csv.reader( fh, delimiter=";", strict=True )


    The problem is that csv.reader() is a function which is imported from the _csv module. This module also contains the Reader class. So maybe I could use _csv.Reader as return type - but this would make use of an implementation detail.

    I would rather write something like def create_my_csv( fh ) -> return_type_of( csv.reader )

    Continue reading...

Compartilhe esta Página