pythontype-hintingpytz

Type hint pytz timezone


I have a function that returns a pytz.timezone('...') object. For example for the function below, what should be the type hint for the return?

def myfunc(tz_str: str) -> ????:
  return pytz.timezone(tz_str)

And in general, how should we type hint objects from installed modules?


Solution

  • Faced with such a problem after starting using mypy

    . To resolve this use types-pytz - this repository with stubs for pytz:

    import pytz
    from pytz.tzinfo import DstTzInfo
    
    def get_timezone() -> DstTzInfo:
       return pytz.timezone('UTC')