簡體   English   中英

pydantic 模式/模型的 python 類型提示

[英]python type hinting for pydantic schema/model

我試圖在我的代碼中為每個 function 指定一個類型提示。 如何為等待任何 pydantic 模式(模型)的 function 指定類型提示?

這是我的代碼:

def hash_password(password: str, schema = None) -> str:
    if schema:
        del schema.password if hasattr(schema, 'password') else None
    return config.pwd_context.hash(password)

示例 model:

my_schema = schemas.UserBase(full_name='a b c')  # just a text
type(my_schema)  # <class 'app.schemas.UserBase'>

您可以從模塊中導入您的架構: from app.schemas import UserBase 然后將這個 object 輸入到您的 function 中:

def your_function() -> UserBase:
    ...

如果我理解正確,這就是你想要做的。 如果它不符合您的需求,請告訴我!

暫無
暫無

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

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