Although variable current_timestamp (and thus also the function return value) and variable time are datetime.datetime class objects, the following code raises AttributeError: type object 'datetime.datetime' has no attribute 'datetime'
from datetime import datetime
def current_time() -> datetime.datetime:
current_timestamp: datetime.datetime = datetime.now()
return current_timestamp
time: datetime.datetime = current_time()
print(time)
Changing only the annotation for the function return value and the time variable solves the problem.
from datetime import datetime
def current_time() -> datetime:
current_timestamp: datetime.datetime = datetime.now()
return current_timestamp
time: datetime = current_time()
print(time)
Can anyone explain why the annotation datetime.datetime raises only an AttributeError for the function return value and the time variable, but not for the current_timestamp variable?
I am running Python 3.8.