Python 3: UnboundLocalError: local variable referenced before assignment [duplicate]

This is because, even though Var1 exists, you’re also using an assignment statement on the name Var1 inside of the function (Var1 -= 1 at the bottom line). Naturally, this creates a variable inside the function’s scope called Var1 (truthfully, a -= or += will only update (reassign) an existing variable, but for reasons unknown … Read more

How to specify “nullable” return type with type hints

You’re looking for Optional. Since your return type can either be datetime (as returned from datetime.utcnow()) or None you should use Optional[datetime]: from typing import Optional def get_some_date(some_argument: int=None) -> Optional[datetime]: # as defined From the documentation on typing, Optional is shorthand for: Optional[X] is equivalent to Union[X, None]. where Union[X, Y] means a value … Read more