I have a class variable as shown below:
class MyClass:
def __init__(self):
self.value: MyOtherClass | None = None
self.initialize_value()
def initialize_value(self):
self.value = MyOtherClass()
def use_value(self):
return self.value.used
self.value is guaranteed to be initialized as an instance of MyOtherClass before being used in self.use_value(); however, it must remain as None in the constructor. The dilemma here is how to properly typehint this situation.
error: Item "None" of "Optional[MyOtherClass]" has no attribute "used" [union-attr]self.initialize_value(), I get an Instance attribute value defined outside __init__ error from PylintWhat is the best way to go about this? Thanks a lot!
To avoid repetition, when such an attribute is used throughout the class in multiple methods, a common pattern for me is protecting it and defining a property that raises an error, if the attribute behind it is not set:
class MyClass:
def __init__(self):
self._value: MyOtherClass | None = None
self.initialize_value()
@property
def value(self) -> MyOtherClass:
if self._value is None:
raise AttributeError("...")
return self._value
def initialize_value(self):
self._value = MyOtherClass()
def use_value(self):
return self.value.used
This also removes ambiguity for the type checker since the return type of the property is always MyOtherClass.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With