title: What type hint to specify when a function returns either an object of a certain type or None?
body: <p>Since I discovered type hints in Python, I started using them, because I think they are useful for the reader of the code. I'm looking forward that these type hints will eventually become type static checkers in the future, because it would make code a lot more reliable, and that's very important for a serious project.</p>
  
  <p>Apart from that, I've a lot of functions where the return type can either be <code>BSTNode</code> or <code>None</code>. It can be <code>None</code> for example because in a search function, if no <code>BSTNode</code> is found, <code>None</code> is simply returned. Is there a way of specifying that the return type could also be <code>None</code>?</p>
  
  <p>Currently I'm using something as follows</p>
  
  <pre><code>def search(self, x) -&gt; BSTNode:
      pass
  </code></pre>
  
  <p>I know I can use strings as type hints, but I'm not sure it's the correct or best way of doing it.</p>
  
  <pre><code>def search(self, x) -&gt; "BSTNode | None":
      pass
  </code></pre>