How should I annotate the return type of a method that can return multiple different types of objects?
Specifically this is the method I'm having trouble with:
def _bin_factory(self) -> Any:
"""
Returns a bin with the specificed algorithm,
heuristic, and dimensions
"""
if self.algorithm == 'guillotine':
return guillotine.Guillotine(self.bin_width, self.bin_height, self.rotation,
self.rectangle_merge, self.split_heuristic)
elif self.algorithm == 'shelf':
return shelf.Sheet(self.bin_width, self.bin_height, self.rotation, self.wastemap)
elif self.algorithm == 'maximal_rectangle':
return maximal_rectangles.MaximalRectangle(self.bin_width, self.bin_height, self.rotation)
raise ValueError('Error: No such Algorithm')
I tried Union[shelf.Sheet, guillotine.Guillotine, maximal_rectangles.MaximalRectangle] but MyPy gives me a ton of errors where I use the _bin_factory method later on in my code. The errors seem to center around the fact that all three object types in the Union have different attributes from one another.