             (setf python-watched-config-files
                   (assoc-delete-all config-file python-watched-config-files))))
 
-        (pcase-dolist (`(,config-file . ,_) python-pre-commit-config-cache)
-          (when (string-prefix-p root config-file)
-            (setf python-pre-commit-config-cache
-                  (assoc-delete-all config-file python-pre-commit-config-cache))))
-
-        (pcase-dolist (`(,config-file . ,_) python-pyproject-cache)
-          (when (string-prefix-p root config-file)
-            (setf python-pyproject-cache
-                  (assoc-delete-all config-file python-pyproject-cache)))))
+        (dolist (cache '(python-pre-commit-config-cache python-pyproject-cache))
+          (pcase-dolist (`(,config-file . ,_) (symbol-value cache))
+            (when (string-prefix-p root config-file)
+              (setf (symbol-value cache)
+                    (assoc-delete-all config-file (symbol-value cache)))))))
 
       (setf python-project-requirements-cache
             (assoc-delete-all buf-file python-project-requirements-cache)))))

                 (setf python-black-command nil
                       python-black-d-command nil)))

      ;; Set `blackd' request headers
      (when-let (python-black-d-command
                 (pyproject-path (python-pyproject-path))
                 (black-d-request-headers
                  (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) ",")))))))))
        (make-local-variable 'python-black-d-request-headers-function)
        (setf python-black-d-request-headers-function (lambda () black-d-request-headers)))

      (python-black-on-save-mode 1))))


                              (funcall parser file))))))))
          python-watched-config-files)))

(defun python-pre-commit-config-file-path ()
  (find-file-from-project-root ".pre-commit-config.yaml"))

(defun python-pyproject-file-path ()
  (find-file-from-project-root "pyproject.toml"))

(defvar python-pre-commit-config-cache nil)
(defvar python-pyproject-cache nil)

(cl-defmacro python-config-file-content (name &key file-path cache-var parser)
  `(defun ,name ()
     (let* ((config-file ,file-path)
            (cache-value (and config-file
                              (file-exists-p config-file)
                              (assoc-default config-file ,cache-var))))
       (if cache-value
           cache-value
         (when config-file
           (python-watch-config-file config-file (quote ,cache-var) (quote ,parser))
           (when-let ((content (funcall (quote ,parser) config-file)))
             (push (cons config-file content) ,cache-var)
             content))))))

(python-config-file-content python-pre-commit-config
                            :file-path (python-pre-commit-config-file-path)
                            :cache-var python-pre-commit-config-cache
                            :parser python-parse-config-file)

(python-config-file-content python-pyproject
                            :file-path (python-pyproject-file-path)
                            :cache-var python-pyproject-cache
                            :parser python-parse-config-file)

(defun python-use-poetry-p ()
  (when-let ((pyproject-path (python-pyproject-file-path)))
    (and
     (with-temp-buffer
       (insert-file-contents pyproject-path)
