[Python] How can I check having functions with specific return type but without return value...

Discussão em 'Python' iniciado por Stack, Abril 19, 2025.

  1. Stack

    Stack Membro Participativo

    Ruff has a nice rule ANN201 that checks the return type of a function. Example:

    def add(a, b): # wrong!
    return a + b

    def add(a: int, b: int) -> int: # right
    return a + b


    This also applies for functions that don't return anything, so I have to specify None as return type. Example

    def log(s: str): # wrong!
    print(s)

    def log(s: str) -> None: # right
    print(s)


    I'd like to setup a rule that verifies if a function as a different return type than None, then I need to return a value. Example:

    def add(a: int, b: int) -> int:
    a + b
    # no `return` but still right?


    I understand that Python doesn't enforce this. But I'd like to have this check made with ruff, because similar pieces of code were giving some issues in my code.

    Is this possible?

    Continue reading...

Compartilhe esta Página