dotfiles/emacs/.emacs.d/config/init-python.el
2021-04-15 15:27:10 -04:00

94 lines
3.1 KiB
EmacsLisp

;; -*- lexical-binding: t; -*-
;; Python configuration
(use-package python
:defer t
:straight (:type built-in)
:config
(add-hook 'python-mode-hook #'highlight-indent-guides-mode)
;; Redefine the python-mypy flycheck checker to account for projectile-compilation-dir
(flycheck-define-checker python-mypy
"Mypy syntax and type checker. Requires mypy>=0.580.
See URL `http://mypy-lang.org/'."
:command ("mypy"
"--show-column-numbers"
(config-file "--config-file" flycheck-python-mypy-config)
(option "--cache-dir" flycheck-python-mypy-cache-dir)
source-original)
:error-patterns
((error line-start (file-name) ":" line (optional ":" column)
": error:" (message) line-end)
(warning line-start (file-name) ":" line (optional ":" column)
": warning:" (message) line-end)
(info line-start (file-name) ":" line (optional ":" column)
": note:" (message) line-end))
:modes python-mode
;; Ensure the file is saved, to work around
;; https://github.com/python/mypy/issues/4746.
:predicate flycheck-buffer-saved-p
:working-directory (lambda (checker)
(or (projectile-compilation-dir)
default-directory)))
(add-to-list 'flycheck-disabled-checkers 'python-flake8))
;; pyvenv to track virtual environments
(use-package pyvenv
:defer 3
:config
(pyvenv-mode)
(pyvenv-tracking-mode))
;; pyenv to track Python version
(use-package pyenv
:straight (:host github :repo "aiguofer/pyenv.el")
:if (executable-find "pyenv")
:defer 3
:init
(setq pyenv-executable (executable-find "pyenv"))
:config
;; Patch version alias stuff I don't use to stop spamming messages
(defun pyenv--version-alias-file ()
"Get peynv version alias file path."
(when pyenv-use-alias
(pyenv--call-process "version-alias-file")))
(global-pyenv-mode)
(defun pyenv-switch-buffer-hook (prev curr)
(pyenv-use-corresponding))
(defun pyenv-use-corresponding-vterm-hook ()
(when (eq major-mode 'vterm-mode)
(vterm-send-C-e)
(vterm-send-C-u)
(vterm-send-string (format "export PYENV_VERSION=%s\n" (getenv "PYENV_VERSION")))))
(add-hook 'switch-buffer-functions #'pyenv-switch-buffer-hook)
(add-hook 'eshell-directory-change-hook #'pyenv-use-corresponding)
(add-hook 'pyenv-mode-hook #'pyenv-use-corresponding-vterm-hook)
:custom
(pyenv-show-active-python-in-modeline nil))
;; A pipenv integration
(use-package pipenv
:defer 2
:hook (python-mode . pipenv-mode))
;; LSP using Microsoft's pyright language server
(use-package lsp-pyright
:hook (python-mode . (lambda ()
(require 'lsp-pyright)
(lsp-deferred)))
:custom
(lsp-pyright-use-library-code-for-types t)
(lsp-pyright-multi-root t)
:general
(python-mode-map "C-c C-d" #'lsp-describe-thing-at-point))
;; isort to sort Python imports
(use-package py-isort
:commands (py-isort-buffer py-isort-region)
:config
(setq py-isort-options '("-m=3"))
:general
(python-mode-map "C-c C-i" #'py-isort-buffer))
(provide 'init-python)