title: Python: how to override type hint on an instance attribute in a subclass?
body: <p>Before you dive in, here is my question: how can I use type hints in a subclass to specify a different type on an instance attribute?</p>
  <p>If you are unclear on what that means, read below, where I have drawn up an example to clarify things.</p>
  <hr />
  <p><strong>Full Explanation</strong></p>
  <p>I have an abstract class <code>Foo</code>, and a subclass of <code>Foo</code> called <code>SubclassOfFoo</code>.</p>
  <p><code>Foo</code> has an abstract method <code>get_something</code> that returns an object of type <code>Something</code>.</p>
  <p><code>Something</code> has a subclass called <code>SubclassOfSomething</code>.  <code>SubclassOfSomething</code> has an additional method <code>something_special</code>.</p>
  <p><code>SubclassOfFoo</code> overrides <code>get_something</code> to return an object of type <code>SubclassOfSomething</code>.  Then, <code>SubclassOfFoo</code> tries to use <code>SubclassOfSomething</code>'s method <code>something_special</code>.</p>
  <p>However, currently my PyCharm's inspections are reporting <code>Unresolved attribute reference 'something_special' for class 'Something'</code>.  I am trying to figure out the correct way to fix this.</p>
  <p>This is all very confusing, so I have made a nice little code snippet to help here:</p>
  <pre><code>from abc import ABC, abstractmethod
  
  
  class Something:
      def __init__(self):
          self.attr = 0
  
  
  class SubclassOfSomething(Something):
      def __init__(self):
          Something.__init__(self)
  
      def something_special(self):
          self.attr = 1
  
  
  class Foo(ABC):
      def __init__(self):
          self.my_class = self.get_something()
  
      @abstractmethod
      def get_something(self) -&gt; Something:
          pass
  
  
  class SubclassOfFoo(Foo):
      def __init__(self):
          Foo.__init__(self)
  
      def get_something(self) -&gt; SubclassOfSomething:
          return SubclassOfSomething()
  
      def do_something_special(self):
          self.my_class.something_special()
  </code></pre>
  <p>Basically, in order to get everything to work out, I can do one of several things:</p>
  <ol>
  <li>Remove the type hint on the return of <code>get_something</code> within <code>Foo</code></li>
  <li>Use a type hint in <code>SubclassOfFoo</code> for <code>self.my_class</code> to clear things up</li>
  <li>Use generics?</li>
  </ol>
  <p>Option 1. is what I am trying to avoid</p>
  <p>Option 2. is not bad, but I can't figure it out</p>
  <p>Option 3. is also an option.</p>
  <p>I am also open to other options, as I am sure there is a better way.</p>
  <p><strong>Can you please help me figure out the correct way to handle this?</strong></p>
  <hr />
  <p><strong>What I Have Tried</strong></p>
  <p>To emulate option 2., I tried using <code>typing.Type</code> as suggested here: <a href="https://stackoverflow.com/questions/46092104/subclass-in-type-hinting">Subclass in type hinting</a></p>
  <p>However, this was not working for me.</p>