python – type hint returns NameError: name datetime not defined

python – type hint returns NameError: name datetime not defined

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

Either import datetime and use datetime.datetime as hint, or from datetime import datetime and use datetime as hint.

python – type hint returns NameError: name datetime not defined

Leave a Reply

Your email address will not be published.