Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override type-hint of a parameter in __init__

On this code:

class Obj():
    def a(self):
        pass


class ObjChild(Obj):
    def b(self):
        pass


class A:
    def __init__(self, obj: Obj):
        self.obj = obj


class B(A):
    def __init__(self, obj: ObjChild):
        super().__init__(obj)
        
    def kuku(self):
        self.obj.b()

Pycharm warns me about the last line that:

Unresolved attribute reference 'b' for class 'Obj'

But I type-hinted the obj that is being passed in the __init__ to be ObjChild, so it should know statically that self.obj is a ObjChild instance, and would have the b method.

How can I make Pycharm realize that? It messes up my autocompletions in the project.

like image 1000
omer mazig Avatar asked Dec 20 '25 08:12

omer mazig


1 Answers

Make A a Generic:

from typing import Generic, TypeVar


class Obj:
    def a(self):
        pass


_Obj = TypeVar('_Obj', bound=Obj)


class ObjChild(Obj):
    def b(self):
        pass


class A(Generic[_Obj]):
    def __init__(self, obj: _Obj):
        self.obj = obj


class B(A[ObjChild]):
    def __init__(self, obj: ObjChild):
        super().__init__(obj)
        self.obj.b()

Unless you make A a generic, there's no connection between passing an ObjChild to A.__init__ and A.obj being an ObjChild.

like image 190
Samwise Avatar answered Dec 23 '25 00:12

Samwise



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!