簡體   English   中英

我如何擴展通用類

[英]How can I extend a generic class

我想在python中擴展一個通用類,並在我的基類中重寫它的一些方法,下面的示例無法編譯

   class __SyncProductRepo(SyncStore[Product]): ...

如何擴展通用基類SyncStore

我正在通過venv環境使用python 3.7

有關更多信息,請參見以下摘錄

我有一個如下的通用類

   class AsyncStore(Generic[I]): ...

其中[I]是可識別類別的孩子

   class Identifiable(Generic[T]): ...

[T]是可識別(get_id)方法的返回類型,在AsyncStore中,確保類型I是可識別的子級,我將其包括在內

   I = TypeVar('I', bound=Identifiable[Any])

這是我打算使用以下代碼Product.py的方式,如下所示

   class Product(BaseModel, Identifiable[str]):

    def get_id(self) -> str:
        return self._id
   ...

__SyncproductRepo.py如下

   class __SyncProductRepo(SyncStore[Product]): ...

我的期望是應該編譯並執行

   class __SyncProductRepo(SyncStore[Product]): ...

這不會編譯並且失敗

   TypeError: Parameters to generic types must be types. Got <module 'src.models.Product' ...

感謝@shmee的回答,我找到了問題所在。 我導入了錯誤的產品而不是產品。 謝謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM