django-stub: django 와 mypy를 같이 사용할 수 있게 해준다.
poetry 가 관리할 수 있게 django-stubs, mypy 를 추가해준다.
poetry add django-stubs==1.9.0 mypy==0.931
pyproject.toml 파일안에 아래의 코드를 추가 해준다.
# mypy 에게 django plugin 을 사용 # strict = true 이어야지만 argument 와 return 값에 # type hint 가 제대로 설정이 되어있는지 확인한다. [tool.mypy] plugins = ["mypy_django_plugin.main"] python_version = 3.9 strict = true # [[]] 의 형식을 사용하면 overrides 를 할 수 있다. # ignore_errors = true 는 mypy 검사를 하지 않는 것 # *.migrations.* 는 개발자가 수정한 것이 아니기때문에 # mypy 검사를 할 이유가 없다. [[tool.mypy.overrides]] module = "*.migrations.*" ignore_errors = true # 위의 이유와 같다. [[tool.mypy.overrides]] module = "manage" ignore_errors = true # django_settings_module 안에는 project settings 경로를 적어주면 된다. [tool.django-stubs] django_settings_module = "good_place.settings"
mypy 실행
poetry run mypy .
실행 결과
에러가 표시된 부분들을 체크해서 type hint 를 적어주면 해결된다.