简体   繁体   中英

python type hinting for pydantic schema/model

I'm trying to specify a type hinting for every function in my code. How I can specify the type hinting for a function which waiting for any pydantic schema (model)?

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:

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 . And then type hint this object to your 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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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