본문 바로가기
Python

VSCode - Pylint로 Python 코드 퀄리티 올리기

by 올엠 2024. 3. 3.
반응형

 https://code.visualstudio.com/docs/python/linting

 

이전 글에서 에러 해결과 함께 정리해보았는데, 여기서 조금 Lint 부분만 정리해 본다.

우선 python 용 Linting 기능을 담당하는 pylint를 설치하자.

pip3 install pylint

이후에는 Language Server를 Jedi로 설정할 것을 추천한다.

아래 설정 화면은 VSCode의 플러그인 메뉴에서 Python의 톱니 바퀴를 선택하여 환경설정을 선택할 수 있다. 그리고 아래와 같이 Jedi를 선택하자.

이렇게 구성한 이후 VS Code를 전체 종료하고 다시 설정을 진행해보기 바란다.

정상적으로 Linting이 완료되면, 아래와 같이 Problem 창에서 현재 코드의 오류, 경고등을 확인해 준다.

Add Note

추가로 만약 PyLint 오류가 번거로운 상황이 발생하면, 글로벌 설정을 통해 조정이 가능하다. 아래 그림과 같이 Code -> Preferences->Settings로 이동하여 Linting을 설정할 수 있다.


아래 그림과 같이 버그도 존재하므로, 일부는 무시하고 사용하거나, 예외 설정을 하면 좋다.

https://github.com/PyCQA/pylint/issues/4584

아래와 같이 Pylint Args를 추가하면 보다 다양한 코드를 볼 수 있다. 다만 아래와 같이 특정 코드만 비활성화 하는 추가 설정을 args로 직접 추가하게 되면, 전체적인 구성이 args를 따라가게 되어, 아래 코드만 비활성화 되고 전체가 활성화 되게 된다.

 

따라서 아래와 같이 추가하여, 꼭 필요한 내용에 대해 수정이 가능하다.

--disable=W0703,C0111,W0142,W0403,W0613,W0232,R0903,R0913, C0103, R0914,C0304,F0401,W0402,E1101,W0614,C0111,C0301

 

# Disabled messages
#    Pointless
#       W0142 = *args and **kwargs support
#       W0403 = Relative imports
#       W0613 = Unused argument
#       W0232 = Class has no __init__ method
#       R0903 = Too few public methods
#       R0913 = Too many arguments
#       C0103 = Invalid name
#       R0914 = Too many local variables
#       C0304 = Final newline missing
#
#    PyLint's module importation is unreliable
#       F0401 = Unable to import module
#       W0402 = Uses of a deprecated module
#       E1101 = Module x has no y member
#
#    Already an error when wildcard imports are used
#       W0614 = Unused import from wildcard
#
#    Stricter messages that can be disabled until everything else has been fixed
#       C0111 = Missing docstring
#       C0301 = Line too long

추가로 비활성화 하고자 하는 코드가 있다면 아래 내용을 보고 추가하면 된다.

GitHub - janjur/readable-pylint-messages: List of pylint human readable message ids and dev readable codes

그리고 특정하게 예외를 하고자 한다면, 프로젝트 메인 폴더에 .pylintrc 파일을 만들어서 세부적으로 설정이 가능하다.

대표적으로 No name 'BaseModel' in module 'pydantic' 에 대한 오류는 오탐이다.

아래와 같이 .pylintrc파일을 만들고, extension-pkg-whitelist=pydantic 를 입력해주자.

[MASTER]
extension-pkg-whitelist=pydantic


 
반응형