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

[Python] How can I specify a tzinfo object as a Type Hint in Python?

Discussão em 'Python' iniciado por Stack, Outubro 3, 2024 às 13:32.

  1. Stack

    Stack Membro Participativo

    Would you know what is the correct type to use when specifying datetime.now().astimezone().tzinfo?

    I tried specifying the type of this datetime.now().astimezone().tzinfo Python (3.10.5) object using Type Hints in several ways (thus the libraries imported in the code below), but this far nothing works and I get error: Incompatible types in assignment (expression has type "tzinfo | None", variable has type "timezone") [assignment]. I already looked at other solutions in vain...

    import nb_mypy
    %load_ext nb_mypy
    %reload_ext nb_mypy
    %nb_mypy On
    %nb_mypy DebugOff

    import pytz
    from datetime import datetime, timedelta, date, timezone
    from dateutil.relativedelta import relativedelta
    from typing import Generator, Any
    from types import ModuleType
    from pytz.tzinfo import DstTzInfo, StaticTzInfo

    time_zn_in_graph: timezone = datetime.now().astimezone().tzinfo


    I settled on timezone above because that's the type I get:

    print(type(datetime.now().astimezone().tzinfo))

    <class 'datetime.timezone'>

    but I still get the error...

    [​IMG]

    Continue reading...

Compartilhe esta Página