Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Annotated 활용 제안 #85

Open
KimTom89 opened this issue Dec 5, 2023 · 0 comments
Open

Annotated 활용 제안 #85

KimTom89 opened this issue Dec 5, 2023 · 0 comments
Labels
enhancement New feature or request

Comments

@KimTom89
Copy link
Collaborator

KimTom89 commented Dec 5, 2023

정의

Annotated 클래스는 파이썬 타입 힌트와 FastAPI의 의존성 주입 시스템을 세밀하게 제어하기 위한 도구입니다.
Annotated을 사용하면 더 많은 정보를 타입 힌트에 추가하고 FastAPI가 이 정보를 활용하도록 할 수 있습니다.

Annotated 활용해야하는 이유

  • FastAPI 공식사이트 및 블로그에서 참고한 장점들을 정리

1. 기본값 선언이 더 직관적이다.

  • 이 부분은 솔직히 공감이 잘 안된다...
# Before
kind: str = Query(default="recv"),

# After
kind: Annotated[str, Query()] = "recv",

2. 중복코드량 감소

# 데이터베이스 의존성 주입
DBSession = Annotated[Session, Depends(get_db)]

@router.post("/memo_form_update")
async def memo_form_update(
    request: Request,
    db: DBSession,
    ...

3. parmater 순서에 대한 제한이 없음

4. 의존성주입을 세부적으로 커스터마이징 가능

5. API 문서화

  • 그누보드6는 자동 문서화를 사용하지 않으니 상관없을듯..

결론

  • 2, 4번의 활용방법이 적절할 것으로 판단됨.
  • Annotated를 사용하지 않은 현 상태가 가독성이 더 좋은 경우가 많은 것으로 보임.
  • 일부 복잡한 유효성 검사 및 중복된 파라미터의 체크 등에는 적용이 필요할 것으로 판단됨.
    • 토큰
    • 캡챠
    • 일부 파라미터의 유효성검사
@KimTom89 KimTom89 added the enhancement New feature or request label Dec 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant