0

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'>
salius
  • 918
  • 1
  • 14
  • 30
  • Is it not just `pydantic.BaseModel`? – Kemp May 21 '21 at 15:48
  • @Kemp no, if you using some intermediate class as `BaceSchemaName` (which inherits from a `BaseModel`) – salius May 21 '21 at 15:52
  • 2
    Ah, my mistake, it's `Type[pydantic.BaseModel]` to allow derived classes. See [this answer](https://stackoverflow.com/a/46092347/3228591). – Kemp May 21 '21 at 15:54

1 Answers1

0

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!

Nil Andreu
  • 16
  • 2