如何检查一种类型是否符合 another/typehint

How to check one type conforms to another/typehint

我想要一种确保类型符合类型提示的方法,例如:

check_type(str, str) -> True
check_type(str, typing.Union(str, int)) -> True
check_type(typing.List[str], typing.List(typing.Any)) -> True

我看到 Mypy 有 is_subtype 但不接受键入 instance/raw 类型,而是接受专有的 Type 实例?

实现此目标的最佳方法是什么?

由于没有任何回应,而我需要此功能来完成另一项任务,因此我构建了自己的小模块。

不是一个详尽的解决方案,但适用于我可能遇到的情况。我会根据需要继续添加!

https://pypi.org/project/valid-typing