emacs-python-black
emacs-python-black copied to clipboard
package conficts with removing trailing whitespace
I installed python-black so I can use black-macchiato to reformat regions, because the project I'm working on does not use black. So I do not run black on save. Actually running black works very well, both on the whole buffer and on regions. However, after installing, deleting trailing whitespace on save doesn't work anymore.
Is there a way I can change my configuration so that the python-mode py-trailing-whitespace-smart-delete-p
works again?
if you don't use the ‘on-save-mode’ this package shouldn't do anything.
i am not sure what the problem is but i suspect it may not be this package?
I think it might be because of the reformatter package, which is required by python-black. I uninstalled both python-black and reformatter, and my trailing white space removal worked again. When I reinstall python-black, it no longer works.
‘no longer works’ needs clarification. any errors?
what is your exact configuration?
This is my configuration:
(use-package jedi
:ensure t)
(use-package python-mode
:ensure t
:init
:custom (py-trailing-whitespace-smart-delete-p t)
:config
(add-hook 'python-mode-hook
(lambda () (set (make-local-variable 'comment-inline-offset) 2)))
(add-hook 'python-mode-hook 'jedi:setup)
(setq jedi:complete-on-dot t)
)
(use-package py-isort
:ensure t
:config (add-hook 'before-save-hook 'py-isort-before-save))
(use-package python-black
:ensure t
:custom (python-black-extra-args '("-S")))
When I uninstall python-black and reformatter (and comment out the lines in my .emacs so they don't get reinstalled automatically), my trailing white space is removed on save.
When I install python-black, my trailing white space is no longer removed on save. Isort works as expected, and there are no errors or messages that have to do with trailing whitespace. Running M-x delete-trailing-whitespace
works as expected. (python-black itself runs without problems when I run it manually as well)
If you have no idea what would cause this, I'll just have to remember to run delete-trailing-whitespace manually.
very confusing indeed. can you try without this package, but with reformatter.el installed?
Good idea! With reformatter installed and python-black uninstalled, trailing whitespace gets removed on save.
puzzling 😕
this package shouldn't do anything unless explicitly asked for, e.g. manual command, or via a on-save-hook. (source code is pretty straight-forward, no magic in there.)
could it be that some other (python related?) package somehow detects/uses this package if it is installed? just thinking out loud
you also try (in a python buffer!) M-x describe-symbol
and type before-save-hook
.
sample output for me, using black + isort (and global-whitespace-cleanup-mode
):
Value in #<buffer foo.py>
(python-black-buffer
python-isort-buffer
t)
This is my output:
before-save-hook is a variable defined in ‘files.el’.
Its value is (delete-trailing-whitespace t)
Original value was nil
Local in buffer toegang.py; global value is
(py-isort-before-save)
It could be that python-mode does something weird. I can't really read lisp, but to me it doesn't look like py-trailing-whitespace-smart-delete-p
checks any other packages (https://gitlab.com/python-mode-devs/python-mode/-/blob/master/python-mode.el), but it's probably the most likely candidate for issues, since it's called 'smart'. I'll play around a bit next week and see if I can get it to work with global-whitespace-cleanup-mode
instead.
It probably is some weird interaction between python-mode and python-black. I managed to fix it by removing the smart delete setting from python-mode and adding a before-save hook for deleting trailing whitespace instead. Thanks for all your help, especially since it turned out not to be an issue with python-black itself!
My new setup in case anyone else ever runs into this:
(use-package python-mode
:ensure t
:init
:config
(add-hook 'python-mode-hook
(lambda () (set (make-local-variable 'comment-inline-offset) 2)))
(add-hook 'python-mode-hook 'jedi:setup)
(add-hook 'before-save-hook 'delete-trailing-whitespace)
(setq jedi:complete-on-dot t))
(@RosanneZe you may find this interesting)
i encountered a very similar issue today, and it reminded me of this issue. however i wasn't even editing a python file, so there was no python-black.el
, nor any other active autoformatters.
the symptom: whitespace wouldn't be removed on save, despite whitespace-cleanup-mode
being enabled in the buffer.
enabling that mode adds whitespace-cleanup-mode-write-file
to write-file-functions
. so far so good, but it didn't actually work.
it turned out that whitespace-cleanup-mode-write-file
checks the variable whitespace-cleanup-mode-only-if-initially-clean
which happens to be t
by default, and my file was not ‘clean’ when opening.
the fix? manually run M-x whitespace-cleanup
once, save the file, close the file, reopen it. everything worked again! 🥳