I have a class that requires a sequence of actions to be taken:
class SomeModel:
def __init__(self):
pass
def predict(self, X):
return None
class Model:
def __init__(self):
self.inner_model = None
def _check_trained(self):
# Simplified; real version has more checks.
assert self.inner_model is not None
def train(self, X, y):
self.inner_model = SomeModel()
# More code here
def predict(self, X):
self._check_trained()
return self.inner_model.predict(X)
Pylance gives a type error on self.inner_model.predict
as self.inner_model
could be None
. However, this is prevented by the previous check. Unrolling the train check function would fix this but would be unwieldly.
Is there a way to verify the not-None property using a function? Or will I need to explicitly disable the type check for this line?
Additional context: VSCode 1.65.2 Pylance v2022.3.2 Pylance on basic Type Checking Mode.