question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

I hope fastapi can support TypeVar

See original GitHub issue

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn’t find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google “How to X in FastAPI” and didn’t find any information.
  • I already read and followed all the tutorial in the docs and didn’t find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

from typing import TypeVar, Generic

from fastapi import Depends
from sqlalchemy.orm import Session

from app.core.database_engine.db_core import get_db
from app.core.table_class import HasIdTable, DateCreateTable, DateCreateUpdateTable

T = TypeVar('T', bound=HasIdTable)


class DbAdaptor(Generic[T]):
    def __init__(self, db: Session = Depends(get_db)):
        self.db = db

    def add(self, data_element: T, is_commit: bool = True) -> dict:
        if isinstance(data_element, DateCreateTable):
            data_element.create_stamp()
        self.db.add(data_element)
        if is_commit:
            self.db.commit()
        return data_element.get_dict()

    def read_by_id(self, id: int) -> T:
        rt: T = self.db.query(T).filter_by(id=id).first()
        return rt

@bp.post('/create', description='create data to table')
def create(dba: DbAdaptor[SampleTable] = Depends(), data: str = Body(..., embed=True)):
            """create data into the table"""
            data = SampleTable(data=data,
                               link=str(random.randint(0, 99999)),
                               title=data,
                               content=data)
            return dba.add(data)

Description

I’m sorry, I’m no English user but I want to achieve this function.

This is the code in my project, but guess what happened when I opened swagger.

スクリーンショット 2021-09-18 22 48 27

This shouldn’t happen.

I just want to simplify the operation of the database.

I just want to make operating the database easier through my code.

Under normal circumstances, I can do this without thinking about more specific details.

Wanted Solution

I hope it can show a normal swagger.

Wanted Code

null

Alternatives

do not use the typevar, But it’s not fun.

Operating System

macOS

Operating System Details

No response

FastAPI Version

fastapi~=0.68.1

Python Version

python3.8

Additional Context

No response

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:9

github_iconTop GitHub Comments

4reactions
JeanArhancetcommented, Nov 1, 2021

I have opened a Pull Request for this issue. I used the functions of Pydantic to retrieve the arguments

2reactions
jakub-baciccommented, Oct 20, 2021

I haven’t checked your code but we just had a similar issue in our project and… it seems that the issue is fixed with python 3.9. Try to check that first and see if that helps.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python Types Intro - FastAPI
By declaring types for your variables, editors and tools can give you better support. This is just a quick tutorial / refresher about...
Read more >
tiangolo/fastapi - Gitter
Can anyone point me to full CRUD example using databases and SQLAlchemy core in a ... to understand how to replace CRUD sync...
Read more >
Call async object function from a fastapi method - Stack Overflow
database.database import access_database SchemaType = TypeVar("SchemaType", bound=BaseModel) class CRUDBase(Generic[SchemaType]): def __init__( ...
Read more >
Confusing way to use TypeVar - Python Help - Discussions on ...
Unfortunately mypy doesn't support it yet, so you'll have to mimick it using bounded TypeVar s like the examples in the PEP. In...
Read more >
Field Types - pydantic
pydantic supports many common types from the Python standard library. ... If you want to validate the values of an infinite generator you...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found