How can I alert python of the return type of a pickle.load() command within a class where load() command returns an object of the class.
I had hoped the straight-forward
class myClass:
@classmethod
def load(cls, filename) -> myClass:
with open(filename,'rb') as f:
return pickle.load(f)
would work, but python complains that myClass is not defined.
I'd like for python to know the class type for code completion, etc.
As you're probably aware, at the time when you define the classmethod,
myClassdoesn't yet exist, so you can't reference it directly.The prescribed alternative is to use string representations of the object. It's not perfect, but at the end of the day type-hinting is not a strict means of enforcement; it's just an informal hint.
From PEP 484:
In fact, there's an example very similar to yours from that PEP: