Question
Type of Union or Union of Types
In Python, is Type[Union[A, B, C]]
the same as Union[Type[A], Type[B], Type[C]]
?
I think they are equivalent, and the interpreter seems to agree.
ChatGPT (which, from my past experience, tends to be wrong with this type of questions) disagrees so I was wondering which one is more correct.
To be clear: I want a union of the types of A, B or C, not a union of instances of A, B or C. The first option is shorter and IMHO more readable.
In other words, given these definitions:
class A:
pass
class B:
pass
class C:
pass
def foo(my_class: Type[Union[A, B, C]]):
pass
I expect this usage to be correct:
foo(A) # pass the class A itself
But not this one:
foo(A()) # pass an instance of A
3 72
3