Is there a syntax for adding and comprehending python List generics of abstract types?

Viewed 42

I'm struggling with lists of abstract classes and PEP 484. I understand the reasoning behind not allowing upcasts in some circumstances, but in the following scenario, I find it silly and cumbersome:

MyPy rejects this:

c : List[SuperClass] = [ SubClass(0) ] + [ SubClass(i) for i in range(10)]

But allows this

a : List[SuperClass] = [ SubClass(0) ]
b : List[SuperClass] = [ SubClass(i) for i in range(10)]
c : List[SuperClass] = a + b

This looks like a type inference failure, and adds lots of cruft to the code, especially when using this very common idiom of operating on lists of subclasses of various types.

Is there a cleaner syntax for this, or a way to give hints to the type system?

0 Answers
Related