wbolster / emacs-python-black

Emacs package to reformat Python using black-macchiato
BSD 3-Clause "New" or "Revised" License
95 stars 11 forks source link

i am baffled why black doesn't work automatically #15

Open jowens opened 1 year ago

jowens commented 1 year ago

I'm using, as the readme says,

(use-package python-black
  :demand t
  :after python
  :hook (python-mode . python-black-on-save-mode-enable-dwim))

Then I see that:

python-mode-hook is a variable defined in ‘python.el’.

Its value is
(yasnippet-snippets--fixed-indent python-black-on-save-mode-enable-dwim)

This is all good, but then when I load up a python file, it isn't in black minor mode:

Enabled minor modes: Auto-Composition Auto-Compression Auto-Encryption
Blink-Cursor Eldoc Electric-Indent File-Name-Shadow Font-Lock
Global-Eldoc Global-Font-Lock Line-Number Mac-Mouse-Wheel Menu-Bar
Recentf Shell-Dirtrack Show-Paren Tooltip Transient-Mark Yas
Yas-Global

(Information about these minor modes follows the major mode info.)

Python mode defined in ‘python.el’:
Major mode for editing Python files.

I can manually call python-black-on-save-mode in a Python buffer and everything then works as expected. But why is this not automatic? Baffled. Any tips on how to debug?

jowens commented 1 year ago

GNU Emacs 28.2 (build 1, aarch64-apple-darwin22.2.0, Carbon Version 169 AppKit 2299.3) of 2023-01-12

(define-package "python-black" "1.2.0" "Reformat Python using python-black" '((emacs "25") (dash "2.16.0") (reformatter "0.3")) :commit "e1bbf574a952562ddeadb0caa42c44016136c2c9" :authors '(("wouter bolsterlee" . "wouter@bolsterl.ee")) :maintainer '("wouter bolsterlee" . "wouter@bolsterl.ee") :keywords '("languages") :url "https://github.com/wbolster/emacs-python-black")
wbolster commented 1 year ago

python-black-on-save-mode-enable-dwim uses heuristics to autodetect if a file should be autoformatted:

in your case the autodetection that your project uses black likely doesn't work. having a line like this in pyproject.toml should make that work:

[tool.black]

note that no further configuration options are needed; the header suffices.

alternatively, you could change the hook to unconditionally enable formatting for all python files:

(use-package python-black
  :demand t
  :after python
  :hook (python-mode . python-black-on-save-mode))

however, that can be annoying, e.g. when opening files in the sites-packages/ directory, etc.

jowens commented 1 year ago

I like the dwim aspect. I think auto-detect is great, and the right thing to do in my case. However, I'm just wanting to use it for one standalone .py file. It doesn't have a "pyproject.toml". It's just one file.

In an alternative to an auxiliary file, might there be a comment I can put at the top of my file that would allow dwim to detect I'd like to use black? Right now the first line is #!/usr/bin/env python3 and I would happily manually add a directive that allow dwim to make the right conclusion.

wbolster commented 1 year ago

the dwim command exists only for use in hooks, for the reasons outlined above.

i think you're looking for the normal python-black-on-save-mode toggle. you can run that manually to toggle autoformatting for single file, regardless of what the dwim hook decided

jowens commented 1 year ago

Sure, I'm just asking if I can add a comment in a python file that dwim will recognize (can dwim be augmented to look for such a comment). I like using dwim! I just don't want to manually toggle every single time I visit that file. I'd rather add a comment once for a file I edit often if dwim doesn't, um, do what I mean.

wbolster commented 1 year ago

no, such a magic comment honoured by this package does not exist. not too thrilled about the idea either, the point of black is to not have debatable cruft in source code.

i think it's fundamentally impossible to come up with a ‘dwim’ that works in all cases. if you have concrete ideas on how to improve it, suggestions welcome

wbolster commented 1 year ago

maybe something with dir-locals? 🤔

(i never use those myself)

woochica commented 1 week ago

I just don't want to manually toggle every single time I visit that file.

@jowens why would you want to use python-black-on-save-mode-enable-dwim over python-black-on-save-mode? If you set the hook to python-black-on-save-mode, then you will have the formatter on by default for python codes. If you want it on only for a specific file though, then I also don't understand why wouldn't you just create a pyproject.toml as they recommend it in the black's manual too.