EDIT I disabled this myself because it caused Emacs to spend a lot of CPU time on certain documents. I just live with a poor fill mode - I would like for something to do it right correctly:
some_list.append("This is a very long string which I would like to " "break in a sensible way")
/EDIT
I’ve been banging my head about the same problem for some time and finally found this solution.
It seems to work for me. Unfortunately, I think this disables the filling of any quoted strings in all modes, not just Python. I'm sure someone with stronger elisp-fu than me can suggest a modification that limits it to python mode, but this is the best solution suggested above, IMO.
This decision is taken from this related answer - go answer this question with some love if you like it.
(defun odd-number-of-single-quotes-this-paragraph-so-far () (oddp (how-many "'" (save-excursion (backward-paragraph) (point)) (point)))) (defun odd-number-of-double-quotes-this-paragraph-so-far () (oddp (how-many "\"" (save-excursion (backward-paragraph) (point)) (point)))) (add-to-list 'fill-nobreak-predicate 'odd-number-of-single-quotes-this-paragraph-so-far) (add-to-list 'fill-nobreak-predicate 'odd-number-of-double-quotes-this-paragraph-so-far)
"" "
source share