You need to import datetime, or use a string (remember, it is just an hint).
>>> def f(x: datetime):
... pass
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'datetime' is not defined
>>> def f(x: 'datetime'):
... pass
...
>>>
>>> from datetime import datetime
>>> def f(x: datetime):
... pass
...
>>>
Python 3.7.4
—
UPDATE. For a slightly different version of this issue import __future__.annotations
from __future__ import annotations
def f(dto: Dto):
pass
class Dto:
pass
This would otherwise fail in the function reference to the Dto class. Python4 will have this as the default behaviour. For now, the import must be the first statement of the file.
Tested in Python 3.8.10.
See: https://peps.python.org/pep-0563/