简体   繁体   English

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

[英]python type hinting for pydantic schema/model

I'm trying to specify a type hinting for every function in my code.我试图在我的代码中为每个 function 指定一个类型提示。 How I can specify the type hinting for a function which waiting for any pydantic schema (model)?如何为等待任何 pydantic 模式(模型)的 function 指定类型提示?

Here is my code:这是我的代码:

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)

Example model:示例 model:

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

You could import your schema from the module: from app.schemas import UserBase .您可以从模块中导入您的架构: from app.schemas import UserBase And then type hint this object to your function:然后将这个 object 输入到您的 function 中:

def your_function() -> UserBase:
    ...

If I understood correctly, this is what you wanted to do.如果我理解正确,这就是你想要做的。 If it does not fit on your needs, let me know!如果它不符合您的需求,请告诉我!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM