How to specify “nullable” return type with type hints
问题 Suppose I have a function: def get_some_date(some_argument: int=None) -> %datetime_or_None%: if some_argument is not None and some_argument == 1: return datetime.utcnow() else: return None How do I specify the return type for something that can be None ? 回答1: 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) ->