我有两个模块,它们为同一API的不同版本实现类,即my_api_v2.py
和my_api_v3.py
。现在,我想编写一个客户端类,该客户端类可与其中一些类一起使用并通过模块进行参数化,以便这些类必须来自同一模块(即相同的API版本)。基本上是模块的typing.Generic
而不是类...
from typing import Generic, TypeVar
from my_package import my_api_v2, my_api_v3
T = TypeVar('T', my_api_v2, my_api_v3) # ??
class Foo(Generic[T]):
def bar(self, x: T) -> T:
...
...然后可以像这样使用:
def process_foo(foo: Foo[my_api_v2]) -> Foo[my_api_v2]:
... # Arg and return val must come from the same module
键入模块是否可以实现这种功能?