title: Type hints: Is it a bad practice to alias primitive data types?
body: <p>In Python documentation for typing &amp; type hints we have the below <a href="https://docs.python.org/3/library/typing.html#type-aliases" rel="nofollow noreferrer">example</a>:</p>
  <pre><code>Vector = List[float]
  
  def scale(scalar: float, vector: Vector) -&gt; Vector:
      return [scalar * num for num in vector]
  </code></pre>
  <p><code>Vector</code> type alias clearly shows that type aliases are useful for simplifying complex type signatures.</p>
  <p><strong>However, what about aliasing primitive data types?</strong></p>
  <p>Let's contrast two basic examples of function signatures:</p>
  <pre><code>URL = str    
  
  def process_url(url: URL) -&gt; URL:
      pass
  </code></pre>
  <p>vs.</p>
  <pre><code>def process_url(url: str) -&gt; str:
      pass
  </code></pre>
  <p>Version with type alias <code>URL</code> for primitive type <code>str</code> is:</p>
  <ul>
  <li>self-documenting (among others, now I can skip documenting returned value, as it should be clearly an url),</li>
  <li>resistant to type implementation change (I can switch URL to be <code>Dict</code> or <code>namedtuple</code> later on without changing functions signatures).</li>
  </ul>
  <p>The problem is I cannot find anyone else following such practice. I am simply afraid that I am unintentionally abusing type hints to implement my own ideas instead of following their intended purpose.</p>
  <hr />
  <p><strong>Note from 2020-10</strong></p>
  <p>Python 3.9 introduces &quot;flexible function and variable annotations&quot;, which allows to make annotations like:</p>
  <pre class="lang-py prettyprint-override"><code>def speed_1(distance: &quot;feet&quot;, time: &quot;seconds&quot;) -&gt; &quot;miles per hour&quot;:
      pass
  
  def speed_2(
      distance: Annotated[float, &quot;feet&quot;], time: Annotated[float, &quot;seconds&quot;]
  ) -&gt; Annotated[float, &quot;miles per hour&quot;]:
      pass
  </code></pre>
  <p>Which renders aliasing data types for documenting purposes rather redundant!</p>
  <p>See:</p>
  <ul>
  <li><a href="https://www.python.org/dev/peps/pep-0593/" rel="nofollow noreferrer">https://www.python.org/dev/peps/pep-0593/</a></li>
  <li><a href="https://realpython.com/python39-new-features/#annotated-type-hints" rel="nofollow noreferrer">https://realpython.com/python39-new-features/#annotated-type-hints</a></li>
  </ul>