Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vim plugin formatter fails with error #3318

Closed
nullromo opened this issue Oct 7, 2022 · 8 comments
Closed

Vim plugin formatter fails with error #3318

nullromo opened this issue Oct 7, 2022 · 8 comments
Labels
C: vim Vim plugin T: user support OP looking for assistance or answers to a question

Comments

@nullromo
Copy link

nullromo commented Oct 7, 2022

Describe the bug

When I try to use vim to run Black, I get the following message:

Error detected while processing function black#Black:
line 10:
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "<string>", line 143 in Black
TypeError: __init__(): got an unexpected keyword argument 'preview'
Press ENTER or type command to continue

This happens when calling :Black from a python buffer and then the code does not format.

To Reproduce

Use this .vimrc:

autocmd!
set nocompatible
let mapleader=","

if empty(glob('~/.vim/autoload/plug.vim'))
    silent !curl -fLo ~/.vim/autoload/plug.vim --create-dirs
        \ https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
    autocmd VimEnter * PlugInstall --sync | source $MYVIMRC
endif
call plug#begin('~/.vim/plugged')
Plug 'psf/black'
call plug#end()

autocmd BufWritePre *.py execute ':Black'
let g:black_skip_magic_trailing_comma = 1

inoremap jk <ESC>
nnoremap ; :

and open vim with vim -u <path to small .vimrc>. Then run :PlugInstall to install the plugin. Then open a python file and run :Black.

Expected behavior

The plugin should function properly and format the file in the buffer.

Environment

  • Black's version: latest
  • OS and Python version: WSL, python 3.8

Additional context

I tried uninstalling and reinstalling the plugin, and I also tried uninstalling and reinstalling Black via pip.

@nullromo nullromo added the T: bug Something isn't working label Oct 7, 2022
@felix-hilden felix-hilden added the C: vim Vim plugin label Oct 8, 2022
@bolshoytoster
Copy link

@nullromo I believe this is caused by a version mismatch; your system (pip) version is behind your vim plugin version.

You could try uninstalling via pip and running pip install git+https://github.com/psf/black to install a slightly more up to date version.


We should probably make Mode ignore extra arguments or just give a warning so this doesn’t happen again in future, but I’m not sure if dataclass supports that.

@felix-hilden
Copy link
Collaborator

Dataclasses does not support that beyond optional arguments, but there are workarounds 🤮. Let's not do that.

Let us know if upgrading solved the issue! I'm also open to catching the type error (?) in the appropriate place and issuing a warning. But it won't help previous releases.

@rk012raj
Copy link

rk012raj commented Oct 9, 2022 via email

@nullromo
Copy link
Author

nullromo commented Oct 10, 2022

I ran both pip install git+https://github.com/psf/black from the command line and :PlugUpdate from vim, and the error is still present.

Old Black version:

black, 22.10.0 (compiled: yes)
Python (CPython) 3.8.10

New Black version:

black, 22.10.1.dev1+gb60b85b (compiled: no)
Python (CPython) 3.8.10

Commit hash from the directory where vim installed the Black plugin:

b60b85b234d6a575f636d0a125478115f993c90c

(same as current main branch head)

@ichard26
Copy link
Collaborator

ichard26 commented Oct 10, 2022

The vim plugin manages its own black installation by using its own virtual environment. Upgrading the system black will do nothing. Have you tried :BlackUpgrade? I'll note that there is an open PR to allow the plugin to use the system Black (#3309).

@ichard26 ichard26 added T: user support OP looking for assistance or answers to a question and removed T: bug Something isn't working labels Oct 10, 2022
@nullromo
Copy link
Author

tl;dr it's working. thanks! 🎉

This was the output from :BlackUpgrade:

Upgrading Black with pip...Traceback (most recent call last):
                                                               File "/home/kkovacs/.vim/black/bin/pip", line 5, in <module>
                                                                                                                               from pip._internal.cli.main import main
                                                                                                                                                                        File "/home/kkovacs/.vim/black/lib/python3.8/site-packages/pip/_internal/cli/main.py", line 10, in <module>
                                                                                                                                                                                                                                                                                       from pip._internal.cli.autocompletion import autocomplete
                     File "/home/kkovacs/.vim/black/lib/python3.8/site-packages/pip/_internal/cli/autocompletion.py", line 9, in <module>
                                                                                                                                             from pip._internal.cli.main_parser import create_main_parser
                                                                                                                                                                                                           File "/home/kkovacs/.vim/black/lib/python3.8/site-packages/pip/_internal/cli/main_parser.py", line 7, in <module>
                                                                                                                                                                                                                                                                                                                                from pip._internal.cli import cmdoptions
                                             File "/home/kkovacs/.vim/black/lib/python3.8/site-packages/pip/_internal/cli/cmdoptions.py", line 24, in <module>
                                                                                                                                                                  from pip._internal.exceptions import CommandError
                                                                                                                                                                                                                     File "/home/kkovacs/.vim/black/lib/python3.8/site-packages/pip/_internal/exceptions.py", line 10, in <module>
         from pip._vendor.six import iteritems
                                              ModuleNotFoundError: No module named 'pip._vendor.six'

DONE! You are all set, thanks for waiting ✨ 🍰 ✨
Press ENTER or type command to continue

Yes, the formatting looks like that. Here it is in a readable form:

Upgrading Black with pip...Traceback (most recent call last):
    File "/home/kkovacs/.vim/black/bin/pip", line 5, in <module>
        from pip._internal.cli.main import main
    File "/home/kkovacs/.vim/black/lib/python3.8/site-packages/pip/_internal/cli/main.py", line 10, in <module>
        from pip._internal.cli.autocompletion import autocomplete
    File "/home/kkovacs/.vim/black/lib/python3.8/site-packages/pip/_internal/cli/autocompletion.py", line 9, in <module>
        from pip._internal.cli.main_parser import create_main_parser
    File "/home/kkovacs/.vim/black/lib/python3.8/site-packages/pip/_internal/cli/main_parser.py", line 7, in <module>
        from pip._internal.cli import cmdoptions
    File "/home/kkovacs/.vim/black/lib/python3.8/site-packages/pip/_internal/cli/cmdoptions.py", line 24, in <module>
        from pip._internal.exceptions import CommandError
    File "/home/kkovacs/.vim/black/lib/python3.8/site-packages/pip/_internal/exceptions.py", line 10, in <module>
        from pip._vendor.six import iteritems
ModuleNotFoundError: No module named 'pip._vendor.six'

DONE! You are all set, thanks for waiting ✨ 🍰 ✨
Press ENTER or type command to continue

And here's the output from :BlackVersion:

Black, version 21.8b0 on Python 3.8.10 (default, Jun 22 2022, 20:18:18)
[GCC 9.4.0].
Press ENTER or type command to continue

so yes, it looks like the vim plugin version does not line up with the vim plugin's Black version, which does not line up with the system's Black version.

I went into ~/.vim/ and manually deleted the black folder, then uninstalled the plugin again using :PlugClean, then reinstalled the plugin. Then I got this message when I ran :BlackVersion:

Please wait, one time setup for Black.
Creating a virtualenv in /home/kkovacs/.vim/black...
(this path can be customized in .vimrc by setting g:black_virtualenv)
The virtual environment was not created successfully because ensurepip is not
available.  On Debian/Ubuntu systems, you need to install the python3-venv
package using the following command.
    apt install python3.8-venv
You may need to use sudo with that command.  After installing the python3-venv
package, recreate your virtual environment.
Failing command: ['/home/kkovacs/.vim/black/bin/python3', '-Im', 'ensurepip', '--upgrade', '--default-pip']
Error detected while processing /home/kkovacs/.vim/plugged/black/autoload/black.vim:
line  204:
E880: Can't handle SystemExit of python exception in vim
Error detected while processing function black#BlackVersion:
line    1:
Traceback (most recent call last):
  File "<string>", line 1, in <module>
NameError: name 'BlackVersion' is not defined
Press ENTER or type command to continue

Then I did

sudo apt-get install python3.8-venv
sudo apt-get install python3.10-venv

and re-did the whole process of uninstalling and removing, then reinstalling. Finally, I got this message from :BlackVersion:

Please wait, one time setup for Black.
Creating a virtualenv in /home/kkovacs/.vim/black...
(this path can be customized in .vimrc by setting g:black_virtualenv)
Installing Black with pip...
DONE! You are all set, thanks for waiting ✨ 🍰 ✨
Pro-tip: to upgrade Black in the future, use the :BlackUpgrade command and restart Vim.
Black, version 22.10.0 on Python 3.8.10 (default, Jun 22 2022, 20:18:18)
[GCC 9.4.0].
Press ENTER or type command to continue

Now everything is working again. Not sure why I had to do this, seeing as Black used to work just fine on my machine, but at least now I know how to fix it next time :)

@nullromo
Copy link
Author

However, maybe it's worth mentioning that the message

DONE! You are all set, thanks for waiting ✨ 🍰 ✨

probably shouldn't print out when :BlackUpgrade fails.

@ichard26
Copy link
Collaborator

Feel free to open a new issue or rename & update the description of this issue (and reopen it).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C: vim Vim plugin T: user support OP looking for assistance or answers to a question
Projects
None yet
Development

No branches or pull requests

5 participants