                                    (concat (python-pre-commit-virtualenv-path "black") "/bin/black")))
                             ((and (python-use-poetry-p)
                                   (member "black" requirements))
+                             ;; Set `python-black-command'
                              (make-local-variable 'python-black-command)
                              (setf python-black-command "poetry")
                              (make-local-variable 'python-black--base-args)
-                             (setf python-black--base-args (append '("run" "black") python-black--base-args)))
+                             (setf python-black--base-args (append '("run" "black") python-black--base-args))
+                             ;; Set `python-black-d-command'
+                             (when-let ((null (string-search "pypoetry" python-black-d-command))
+                                        (blackd-location
+                                         (with-temp-buffer
+                                           (if (zerop
+                                                (condition-case err
+                                                    (call-process "poetry" nil t nil "run" "which" python-black-d-command)
+                                                  (error (message "%s" (error-message-string err)) nil)))
+                                               (string-trim (buffer-string))
+                                             nil))))
+                               (make-local-variable 'python-black-d-command)
+                               (setf python-black-d-command blackd-location))
+                             ;; Set `blackd' request headers
+                             (setf python-black-d-request-headers-function
+                                   (lambda ()
+                                     (and (python-pyproject-path)
+                                          (let-alist (python-parse-pyproject)
+                                            `(,@(if .tool.black.line-length
+                                                    `(,(cons "X-Line-Length" (format "%s" .tool.black.line-length))))
+                                              ,@(if (and .tool.black.skip-string-normalization
+                                                         (not (eq .tool.black.skip.string-normalization :false)))
+                                                    `(,(cons "X-Skip-String-Normalization" "true")))
+                                              ,@(if (or .tool.black.fast .tool.black.safe)
+                                                    `(,(cons "X-Fast-Or-Safe" (if .tool.black.fast "fast" "safe"))))
+                                              ,@(if (or .tool.black.pyi .tool.black.target-version)
+                                                    `(,(cons "X-Python-Variant"
+                                                             (if .tool.black.pyi
+                                                                 "pyi"
+                                                               (funcall 'string-join
+                                                                        (append .tool.black.target-version) ",")))))))))))
                             ((executable-find python-black-command)
                              t))
                   (python-black-on-save-mode 1))))
 
-            (with-eval-after-load 'reformatter
+            (with-eval-after-load 'python-isort
               (let ((requirements (python-project-requirements)))
                 (when (cond ((and (python-use-pre-commit-p requirements)
                                   (python-pre-commit-config-has-hook-p "isort"))
-                             (setf python-isort-executable
+                             (make-local-variable 'python-isort-command)
+                             (setf python-isort-command
                                    (concat (python-pre-commit-virtualenv-path "isort") "/bin/isort")))
                             ((and (python-use-poetry-p)
                                   (member "isort" requirements))
-                             (setf python-isort-executable "poetry"
+                             (make-local-variable 'python-isort-command)
+                             (make-local-variable 'python-isort-arguments)
+                             (setf python-isort-command "poetry"
                                    python-isort-arguments
                                    (append '("run" "isort") python-isort-arguments)))
                             ((executable-find python-isort-executable)
                              (when (and python-black-command
                                         (executable-find python-black-command))
+                               (make-local-variable 'python-isort-arguments)
                                (setf python-isort-arguments
-                                     (append '("--profile" "black") python-isort-arguments)))))
+                                     (append '("--profile" "black") python-isort-arguments)))
+                             t))
                   (python-isort-on-save-mode 1))))
 
-            (use-package importmagic
-              :delight
-              :config
+            (with-eval-after-load 'importmagic
               (when (let ((requirements (python-project-requirements)))
                       (and (member "importmagic" requirements)
                            (member "epc" requirements)))

        (goto-char (point-min))
        (string-lines (string-trim (buffer-string)))))))

(defun python-parse-requirement-spec (req)
  (save-match-data
    (if (not (null (string-match "^-r +\\(.+\\)$" req)))
        (mapcar 'python-parse-requirement-spec
                (python-project-requirements-from-file
                 (match-string-no-properties 1 req)))
      (let* ((requirement-spec (split-string req ";"))
             (package-version-spec (string-trim (car requirement-spec))))
        (car (split-string package-version-spec "=="))))))

(defvar python-project-requirements-cache nil)
(defun python-project-requirements ()

                       (with-temp-buffer
                         (insert-file-contents (concat pep518-config-file-dir "pyproject.toml"))
                         (goto-char (point-min))
                         (re-search-forward "^\\[tool.pyright\\]$" nil t nil)))))))))

(use-package lsp-sourcekit
  :after (lsp-mode))
