black icon indicating copy to clipboard operation
black copied to clipboard

[WIP] Allow Black to use a different Python version than Vim

Open sbivol opened this issue 1 year ago • 2 comments

Description

This branch should fix https://github.com/psf/black/issues/2547

It is sometimes the case that Vim was built with a different Python version than the one used by Black. One such scenario is when the black virtualenv was created by an older version of Vim and then the system was upgraded in place. Another scenario is when g:black_use_virtualenv is disabled and the manually created environment in g:black_virtualenv uses a different Python version. Right now Ubuntu 24.04 builds Vim with Python 3.12 but the system uses Python 3.11 as default, making this scenario more likely to happen:

$ python3 --version
Python 3.11.8

$ vim --version | grep lpython
Linking: gcc -Wl,-Bsymbolic-functions -flto=auto -ffat-lto-objects -Wl,-z,relro -Wl,-z,now -Wl,--as-needed -o vim -lm -ltinfo -lselinux -lsodium -lacl -lattr -lgpm -L/usr/lib/python3.12/config-3.12-x86_64-linux-gnu -lpython3.12 -ldl -lm

Currently, :BlackVersion reports Vim's Python version instead of the one found in the virtualenv. This PR fixes that inconsistency as well.

Finally, I added a few TODO comments for issues that I encountered while testing various scenarios but didn't have the time to fix in this PR.

Checklist - did you ...

  • [x] Add an entry in CHANGES.md if necessary?
  • [x] Add / update tests if necessary?
  • [x] Add new / update outdated documentation?

sbivol avatar Feb 20 '24 01:02 sbivol

An alternative approach is to detect the Python version in the virtualenv, and if it doesn't match what Vim was built with, then go through these steps:

  • inform the user that Vim won't use that virtualenv
  • reset g:black_virtualenv to its default value
  • create the default virtualenv as usual

This approach wouldn't enable mixing different Python versions and would use a bit of extra space for a new ~/.vim/black virtualenv, but should still leave the user with a working Black and no chance of confusion.

sbivol avatar Feb 20 '24 10:02 sbivol

After giving this some more thought, and further discussion in https://github.com/pypa/pip/issues/11835, I'm now convinced that my approach isn't ideal. Mixing Python versions can have undesired side effects. I am going to rework this PR.

sbivol avatar Feb 20 '24 21:02 sbivol