[Python] type hint returns NameError: name 'datetime' not defined

Discussão em 'Python' iniciado por Stack, Fevereiro 8, 2025.

  1. Stack

    Stack Membro Participativo

    I have this function below;

    def time_in_range(start, end, x):
    """Return true if x is in the range [start, end]"""
    if start <= end:
    return start <= x <= end
    else:
    return start <= x or x <= end


    The function parameters are all datetime type. I want to add typing hint to the function. This is what I did;

    def time_in_range(start: datetime, end: datetime, x: datetime) -> bool:
    """Return true if x is in the range [start, end]"""
    if start <= end:
    return start <= x <= end
    else:
    return start <= x or x <= end


    I get the error NameError: name 'datetime' is not defined. What is the proper way to add typing for this function?

    I am using python v3.7

    Continue reading...

Compartilhe esta Página