Is it possible to check if a type "matches" a generic type in Python?

Viewed 121

Not sure how to phrase the question correctly, so I'll give an example. Suppose I have the following code:

import typing as T

A = T.List[T.Any]
B = T.List[float]

Now I want to be able to check whether the type B "matches" (for lack of a better word) or "is a subtype" of the type A. I.e. would a value of type B be valid if it was returned from a method with the return type hint of A.

Using == works only if two types match literally (i.e. List[Any] == List[Any] is True, whereas List[Any] == List[float] is False). Since Any could represent any type, I'd like to be able to have a function is_matching(a, b) such that is_matching(List[float], List[Any]) would return True. Is there any built-in way to achieve this in Python?

0 Answers
Related