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?
Faced with such a problem after starting using mypy
.
To resolve this use types-pytz - this repository with stubs for pytz:
pip install types-pytzimport pytz from pytz.tzinfo import DstTzInfo
def get_timezone() -> DstTzInfo: return pytz.timezone('UTC')
BaseTzInfo works for me since they all seem to derive from that. It isn't exactly the same as the Union, but works.
def myfunc(tz: str) -> pytz.tzinfo.BaseTzInfo:
return pytztimezone(tz)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With