Skip to content

Tag: python-typing

Polymorphism and Type Hints in Python

Consider the following case: Let’s say I’m calling get_base_instance in a context where I kow it will return a Sub instance – maybe based on what args I’m passing. Now I want to pass the returned instance to do_something_with_sub: The problem is that my IDE complains about passing a Ba…

Return type based on type argument

I have this code, which type checks fine on its own: But if I try to use it: I get Incompatible return value type (got “Tuple[BaseObject, …]”, expected “Tuple[DerivedObject, …]”)mypy(error) I can fix this with But what I would really like to do is specify that df2objects re…

Type hint for return value in subclass

I am writing a CustomEnum class in which I want to add some helper methods, that would then be available by the classes subclassing my CustomEnum. One of the methods is to return a random enum value, and this is where I am stuck. The function works as expected, but on the type-hinting side, I cannot figure ou…