Skip to content

Instantly share code, notes, and snippets.

@kracekumar
Last active June 17, 2022 11:16
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kracekumar/77d29c7410199fd2cda4 to your computer and use it in GitHub Desktop.
Save kracekumar/77d29c7410199fd2cda4 to your computer and use it in GitHub Desktop.
Emacs auto remove unused import statements in current python file
;;; auto-remove.el --- Auto remove unused functions in python
;;; Commentary:
;; Uses external tool autoflake to remove unused imports from a Python file.
;;; Code:
(defcustom python-autoflake-path (executable-find "autoflake")
"Autoflake executable path.
Allows working with a virtualenv without actually adding support
for it."
:group 'python
:type 'string)
(defun python-remove-unused-imports ()
"Use Autoflake to remove unused function.
$ autoflake --remove-all-unused-imports -i unused_imports.py"
(interactive)
(when (eq major-mode 'python-mode)
(shell-command (format "%s --remove-all-unused-imports -i %s"
python-autoflake-path
(shell-quote-argument (buffer-file-name))))
(revert-buffer t t t))
nil)
(eval-after-load 'python
'(if python-autoflake-path
(add-hook 'before-save-hook 'python-remove-unused-imports)
(message "Unable to find autoflake. Configure `python-autoflake-path`")))
(provide 'auto-remove)
;;; auto-remove.el ends here
@jaseemabid
Copy link

@kracekumar Looks good but a few tips.

  1. The elisp style guide mention not to use a line for just ).
  2. Autoflake path should be customisable. Define a defcustom
    and add it to python group so that folks can just to M-x customize-group RET python RET. Use a path if available.

Code:

;; Auto remove unused functions in python
(defcustom python-autoflake-path (shell-command-to-string "which autoflake")
  "Autoflake executable path.

Allows working with a virtualenv without actually adding support
for it."
  :group 'python
  :type 'string)

(defun python-remove-unused-imports()
  "Use Autoflake to remove unused function"
  "autoflake --remove-all-unused-imports -i unused_imports.py"
  (interactive)
  (shell-command
   (format "%s --remove-all-unused-imports -i %s"
       python-autoflake-path
           (shell-quote-argument (buffer-file-name))))
  (revert-buffer t t t))

@jaseemabid
Copy link

Discovered executable-find

;; Auto remove unused functions in python
(defcustom python-autoflake-path (executable-find "autoflake")
  "Autoflake executable path.

Allows working with a virtualenv without actually adding support
for it."
  :group 'python
  :type 'string)

@jaseemabid
Copy link

Some error checking will also help.

(unless python-autoflake-path
  (error "Unable to find autoflake. Configure `python-autoflake-path`"))

@kracekumar
Copy link
Author

@jaseemabid Thanks. Modified init.el accordingly. Also added before-save-hook.

@jaseemabid
Copy link

@kracekumar The first time you visit a python buffer, it adds a before-save-hook to all buffers. So, saving a .el will still run this hook. Need to fix that part.

@kracekumar
Copy link
Author

@jaseemabid I just encountered that. Finding right way to fix it.

@jaseemabid
Copy link

@jaseemabid
Copy link

@kracekumar FYI, gists themselves are git repositories. Please fetch from my fork and have a look.

@kracekumar
Copy link
Author

@jaseemabid: Thanks! It works.

@jaseemabid
Copy link

@kracekumar Can you post errors raised by revert-buffer?

@jaseemabid
Copy link

Is it happening the first time you are trying to save the file?

[Errno 2] No such file or directory: '/Users/j/tmp/test.py'  
Error: (error "Cannot revert nonexistent file /Users/j/tmp/test.py")
Wrote /Users/j/tmp/test.py

@jaseemabid
Copy link

@kracekumar after-save-hook should solve it, right?

@jaseemabid
Copy link

See patch 4d6871d here https://gist.github.com/jaseemabid/3def1e104cbff09ad803

4d6871d3881111c47fd35458770af817f5bafb01 
Author: Jaseem Abid <jaseemabid@gmail.com>
Date:   Mon Aug 18 00:55:17 2014 +0530

    Run hook after saving file

    - Makes sense to run flake after a file is saved with recent edits.

    - Fixes errors on reverting from non existent files. This can happen
      first time a file is saved.

@pythonhacker
Copy link

Guys - this would be more useful if you could provide the full path to autoflake script in the comments of auto-remove.el . I am right now struggling to find autoflake, though I guess I wrote the original version of it :)

@jaseemabid
Copy link

@pythonhacker You can do (setq python-autoflake-path "/this/is/where/autoflake/is.py")

PS: We made a repo https://github.com/kracekumar/missing-python-mode

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment