Mypy:如何使用模块中的所有 类 创建别名类型
Mypy: How to create an alias type with all classes from a module
我有一个模块 foo
定义了很多 类,例如
class A():
...
class B():
...
class C():
...
...
我想创建一个包含所有这些 类 的 "foo type" 别名,即
my_foo_type = Union[A, B, C, ...]
然而,类 太多了,我不想输入它们,但有程序化的解决方案。我通过
访问模块中定义的所有 类
for name, obj in inspect.getmembers(foo):
if inspect.isclass(obj):
print(obj)
如何将它与类型别名联系起来?
我认为这不可能。我不知道你想用你的 classes 做什么,但根据你的用例,你可以:
- 使你的 class 成为基础 class 的子class(它也可以改进你的模块的设计);
- 使用将为您生成类型变量的外部脚本(并且 运行 每次您在模块中添加或删除 class 时使用此脚本);
- 其他:)
我有一个模块 foo
定义了很多 类,例如
class A():
...
class B():
...
class C():
...
...
我想创建一个包含所有这些 类 的 "foo type" 别名,即
my_foo_type = Union[A, B, C, ...]
然而,类 太多了,我不想输入它们,但有程序化的解决方案。我通过
访问模块中定义的所有 类for name, obj in inspect.getmembers(foo):
if inspect.isclass(obj):
print(obj)
如何将它与类型别名联系起来?
我认为这不可能。我不知道你想用你的 classes 做什么,但根据你的用例,你可以:
- 使你的 class 成为基础 class 的子class(它也可以改进你的模块的设计);
- 使用将为您生成类型变量的外部脚本(并且 运行 每次您在模块中添加或删除 class 时使用此脚本);
- 其他:)