dotfiles/emacs/.emacs.d/config/init-python.el

105 lines
3.4 KiB
EmacsLisp
Raw Normal View History

;; -*- lexical-binding: t; -*-
;; Python configuration
(use-package python
:defer t
:straight (:type built-in)
:config
2021-02-21 03:03:33 +00:00
(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)
2021-04-15 19:27:10 +00:00
default-directory)))
(add-to-list 'flycheck-disabled-checkers 'python-flake8))
2021-10-05 15:09:13 +00:00
;; Run a python repl at the project root
(defun projectile-run-python ()
(interactive)
(let ((default-directory (projectile-project-root)))
(call-interactively #'run-python)))
(with-eval-after-load 'projectile
(general-def projectile-command-map
"s p" #'projectile-run-python
"x p" #'projectile-run-python))
;; pyvenv to track virtual environments
(use-package pyvenv
:defer 3
:config
(pyvenv-mode)
(pyvenv-tracking-mode))
;; pyenv to track Python version
2021-02-26 15:28:50 +00:00
(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))
2021-03-02 01:29:41 +00:00
(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")))))
2021-02-26 15:28:50 +00:00
(add-hook 'switch-buffer-functions #'pyenv-switch-buffer-hook)
2021-03-02 01:29:41 +00:00
(add-hook 'eshell-directory-change-hook #'pyenv-use-corresponding)
(add-hook 'pyenv-mode-hook #'pyenv-use-corresponding-vterm-hook)
2021-02-26 15:28:50 +00:00
:custom
(pyenv-show-active-python-in-modeline nil))
2021-04-07 20:55:14 +00:00
;; 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))
2021-02-24 21:52:01 +00:00
;; 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)