1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] `mypy` doesn't allow unpacking

Discussão em 'Python' iniciado por Stack, Setembro 28, 2024 às 00:22.

  1. Stack

    Stack Membro Participativo

    Consider the following

    from datetime import datetime

    def tle_request(date_time: list[int]) -> datetime:
    # Attempting to create a datetime object using unpacking
    general_date: datetime = datetime(*date_time[:3]) # This causes a mypy error
    return general_date

    # Example usage
    my_time = tle_request([2023, 9, 27])
    print(f"Hello {my_time}")


    Although it works:

    ❯ python main.py
    Hello 2023-09-27 00:00:00


    mypy is prompting the following error:

    ❯ mypy main.py
    main.py:5: error: Argument 1 to "datetime" has incompatible type "*list[int]"; expected "tzinfo | None" [arg-type]
    Found 1 error in 1 file (checked 1 source file)


    That is very weird because if I change datetime(*date_time[:3]) with datetime(date_time[0], date_time[1], date_time[2]). I works:

    ❯ mypy main.py
    Success: no issues found in 1 source file


    Why? Unpacking is really necessary and I see no reason for mypy to complain about it.

    Continue reading...

Compartilhe esta Página