[python오류] pydantic 어노테이션 오류 해결 방법

 

[python오류] pydantic 어노테이션 오류 해결 방법 


python 컴파일 중 어노테이션 오류 발생

문제없이 잘 사용하던 프로젝트 폴더를 옮기고 새로 세팅하려다 보니 아래와 같은 오류가 발생했다. Pydantic 관련 오류 같은데 새로 insatll 해도 동일하게 발생한다.

pydantic.errors.PydanticUserError: A non-annotated attribute was detected: `TESTING = False`. All model fields require a type annotation; if `TESTING` is not meant to be a field, you may be able to resolve this error by annotating it as a `ClassVar` or updating `model_config['ignored_types']` 

자꾸만 위와 같은 메시지가 뜨면서 컴파일이 되지 않는다. 뭐가 문제 일까? 해결방법은 무엇일까?


1. Python 컴파일 오류 발생

기존에 사용하던 Python 모듈은 다른 PC에 개발환경 세팅을 하고 있다. 하던 중 아래와 같은 오류가 발생한다. 원인은 Pydantic은 모든 모델 필드가 타입 어노테이션을 가져야 한다는게 이유이다. 기존에는 문제 없었는데..



2. 해결 방법

BaseSettings은 모든 모델 필드가 타입 어노테이션을 가져야 하므로 아래와 같이 수정해줍니다.

변경전
DB_USERNAME = "userdb"

변경후
DB_USERNAME: str = "userdb"


마무리

오늘은 pydantic.errors.PydanticUserError: A non-annotated attribute was detected 문구 오류 발생 시 처리 방법에 대해서 알아보았습니다. 간단하게 어노테이션 추가를 통해 오류를 수정했습니다. 쉽고 빠른 코딩라이프를 위해 모두 화이팅!

댓글

이 블로그의 인기 게시물

[python오류] pydantic BaseSettings 오류 해결 방법

[python오류] openai, googletrans 동시 사용 오류 발생 해결 방법