title: Extending stub file for a third-party library/module
body: <p>I am using the <a href="https://github.com/aio-libs/yarl/blob/master/yarl/__init__.pyi" rel="noreferrer"><code>yarl</code></a> library's <code>URL</code> object.</p>
  
  <p>It has a quasi-private attribute, <code>._val</code>, which is a <code>urllib.parse.SplitResult</code> object but has no type annotation in <code>yarl/__init__.pyi</code>.  (Understandably so, if the developer does not want to formally make it part of the public API.)</p>
  
  <p>However, I have chosen to use <code>URL._val</code> at my own risk.  A dummy example:</p>
  
  <pre><code># urltest.py
  from urllib.parse import SplitResult
  from typing import Tuple
  
  from yarl import URL
  
  
  def foo(u: URL) -&gt; Tuple[str, str, str]:
      sr: SplitResult = u._val
      return sr[:3]
  </code></pre>
  
  <p>But <code>mypy</code> doesn't like this, because it complains:</p>
  
  <pre><code>$ mypy urltest.py
  "URL" has no attribute "_val"
  </code></pre>
  
  <p>So, how can I, within my own project, "tack on" (or extend) an instance attribute annotation to <code>URL</code> so that it can be used through the rest of my project?  I.e.</p>
  
  <pre><code>from yarl import URL
  
  URL._val: SplitResult
  # ...
  </code></pre>
  
  <p>(mypy does not like this either; "Type cannot be declared in assignment to non-self attribute.")</p>
  
  <hr>
  
  <h3>Update</h3>
  
  <p>I've tried creating a new stub file, in <code>stubs/yarl/__init__.pyi</code>:</p>
  
  <pre><code>from urllib.parse import SplitResult
  
  class URL:
      _val: SplitResult
  </code></pre>
  
  <p>And then setting <code>export MYPYPATH='.../stubs'</code> as described in <a href="https://mypy.readthedocs.io/en/latest/stubs.html" rel="noreferrer">stub files</a>.  However, this <strong>overrides, not extends,</strong> the existing annotations, so everything <em>but</em> <code>._val</code> throws and error:</p>
  
  <blockquote>
    <p>error: "URL" has no attribute "with_scheme"</p>
    
    <p>error: "URL" has no attribute "host"</p>
    
    <p>error: "URL" has no attribute "fragment"</p>
  </blockquote>
  
  <p>...and so on.</p>