From 58aa70784d819e467a9e8b057e481b59ee9547d9 Mon Sep 17 00:00:00 2001 From: Sergiu Bivol Date: Mon, 19 Feb 2024 21:16:17 +0000 Subject: [PATCH 1/8] Make the pyver argument optional --- autoload/black.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autoload/black.vim b/autoload/black.vim index 051fea05c3b..f10214afcfb 100644 --- a/autoload/black.vim +++ b/autoload/black.vim @@ -34,7 +34,7 @@ FLAGS = [ ] -def _get_python_binary(exec_prefix, pyver): +def _get_python_binary(exec_prefix, pyver=sys.version_info[:3]): try: default = vim.eval("g:pymode_python").strip() except vim.error: From 81bb5b7b0c3b6c2ab371180d24e58af6775387ee Mon Sep 17 00:00:00 2001 From: Sergiu Bivol Date: Mon, 19 Feb 2024 21:35:57 +0000 Subject: [PATCH 2/8] Print the import path when importing black fails --- autoload/black.vim | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/autoload/black.vim b/autoload/black.vim index f10214afcfb..2d42ac1eaf6 100644 --- a/autoload/black.vim +++ b/autoload/black.vim @@ -120,7 +120,12 @@ def _initialize_black_env(upgrade=False): return True if _initialize_black_env(): - import black + # TODO: Better handling of the case when import succeeds but Black doesn't + # work, such as when it has been uninstalled from the virtualenv + try: + import black + except ImportError: + print(f"Could not import black from any of: {', '.join(sys.path)}.") import time def get_target_version(tv): From f6bdef41cdeec22065259d684f73bee05fb6892f Mon Sep 17 00:00:00 2001 From: Sergiu Bivol Date: Mon, 19 Feb 2024 23:40:43 +0000 Subject: [PATCH 3/8] Return site-packages for the venv Python version --- autoload/black.vim | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/autoload/black.vim b/autoload/black.vim index 2d42ac1eaf6..405e96b6b36 100644 --- a/autoload/black.vim +++ b/autoload/black.vim @@ -53,15 +53,22 @@ def _get_python_binary(exec_prefix, pyver=sys.version_info[:3]): return exec_path raise ValueError("python executable not found") +def _get_python_version(exec_path): + import subprocess + version_proc = subprocess.run([exec_path, "--version"], stdout=subprocess.PIPE, text=True) + version = tuple(map(int, version_proc.stdout.split(" ")[1].strip().split("."))) + return version + def _get_pip(venv_path): if sys.platform[:3] == "win": return venv_path / 'Scripts' / 'pip.exe' return venv_path / 'bin' / 'pip' -def _get_virtualenv_site_packages(venv_path, pyver): +def _get_virtualenv_site_packages(venv_path): if sys.platform[:3] == "win": return venv_path / 'Lib' / 'site-packages' - return venv_path / 'lib' / f'python{pyver[0]}.{pyver[1]}' / 'site-packages' + venv_python_version = _get_python_version(_get_python_binary(venv_path)) + return venv_path / 'lib' / f'python{venv_python_version[0]}.{venv_python_version[1]}' / 'site-packages' def _initialize_black_env(upgrade=False): if vim.eval("g:black_use_virtualenv ? 'true' : 'false'") == "false": @@ -83,7 +90,7 @@ def _initialize_black_env(upgrade=False): import subprocess import venv virtualenv_path = Path(vim.eval("g:black_virtualenv")).expanduser() - virtualenv_site_packages = str(_get_virtualenv_site_packages(virtualenv_path, pyver)) + virtualenv_site_packages = str(_get_virtualenv_site_packages(virtualenv_path)) first_install = False if not virtualenv_path.is_dir(): print('Please wait, one time setup for Black.') From 273ea5979666a09f52b0528f72dbc96fe25cb2a8 Mon Sep 17 00:00:00 2001 From: Sergiu Bivol Date: Tue, 20 Feb 2024 00:30:25 +0000 Subject: [PATCH 4/8] Add site-packages to sys.path when black is in a non-default virtualenv --- autoload/black.vim | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/autoload/black.vim b/autoload/black.vim index 405e96b6b36..2da2f00d812 100644 --- a/autoload/black.vim +++ b/autoload/black.vim @@ -4,6 +4,8 @@ import os import sys import vim +from pathlib import Path + def strtobool(text): if text.lower() in ['y', 'yes', 't', 'true', 'on', '1']: return True @@ -71,6 +73,8 @@ def _get_virtualenv_site_packages(venv_path): return venv_path / 'lib' / f'python{venv_python_version[0]}.{venv_python_version[1]}' / 'site-packages' def _initialize_black_env(upgrade=False): + virtualenv_path = Path(vim.eval("g:black_virtualenv")).expanduser() + virtualenv_site_packages = str(_get_virtualenv_site_packages(virtualenv_path)) if vim.eval("g:black_use_virtualenv ? 'true' : 'false'") == "false": if upgrade: print("Upgrade disabled due to g:black_use_virtualenv being disabled.") @@ -78,7 +82,8 @@ def _initialize_black_env(upgrade=False): print("or modify your vimrc to have 'let g:black_use_virtualenv = 1'.") return False else: - # Nothing needed to be done. + if virtualenv_site_packages not in sys.path: + sys.path.insert(0, virtualenv_site_packages) return True pyver = sys.version_info[:3] @@ -86,11 +91,8 @@ def _initialize_black_env(upgrade=False): print("Sorry, Black requires Python 3.8+ to run.") return False - from pathlib import Path import subprocess import venv - virtualenv_path = Path(vim.eval("g:black_virtualenv")).expanduser() - virtualenv_site_packages = str(_get_virtualenv_site_packages(virtualenv_path)) first_install = False if not virtualenv_path.is_dir(): print('Please wait, one time setup for Black.') From c797df098e005b572e347767a780012abc86f441 Mon Sep 17 00:00:00 2001 From: Sergiu Bivol Date: Tue, 20 Feb 2024 00:42:10 +0000 Subject: [PATCH 5/8] Report Black's Python version instead of Vim's --- autoload/black.vim | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/autoload/black.vim b/autoload/black.vim index 2da2f00d812..853ccacb9b8 100644 --- a/autoload/black.vim +++ b/autoload/black.vim @@ -55,10 +55,12 @@ def _get_python_binary(exec_prefix, pyver=sys.version_info[:3]): return exec_path raise ValueError("python executable not found") -def _get_python_version(exec_path): +def _get_python_version(exec_path, as_string=False): import subprocess version_proc = subprocess.run([exec_path, "--version"], stdout=subprocess.PIPE, text=True) - version = tuple(map(int, version_proc.stdout.split(" ")[1].strip().split("."))) + version = version_proc.stdout.split(" ")[1].strip() + if not as_string: + version = tuple(map(int, version.split("."))) return version def _get_pip(venv_path): @@ -231,7 +233,9 @@ def BlackUpgrade(): _initialize_black_env(upgrade=True) def BlackVersion(): - print(f'Black, version {black.__version__} on Python {sys.version}.') + virtualenv_path = Path(vim.eval("g:black_virtualenv")).expanduser() + virtualenv_python_version = _get_python_version(_get_python_binary(virtualenv_path), as_string=True) + print(f'Black, version {black.__version__} on Python {virtualenv_python_version}.') EndPython3 From 7fef48698e1cfcc9c8a1760f393a0578c897da3f Mon Sep 17 00:00:00 2001 From: Sergiu Bivol Date: Tue, 20 Feb 2024 00:50:12 +0000 Subject: [PATCH 6/8] Add notes --- autoload/black.vim | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/autoload/black.vim b/autoload/black.vim index 853ccacb9b8..e81d605c009 100644 --- a/autoload/black.vim +++ b/autoload/black.vim @@ -53,6 +53,9 @@ def _get_python_binary(exec_prefix, pyver=sys.version_info[:3]): exec_path = (bin_path / f"python3").resolve() if exec_path.exists(): return exec_path + # TODO: Instead of failing, try to find black in PATH first and use its + # exec_prefix. This can happen when black is installed systemwide and + # g:black_virtualenv was not updated. raise ValueError("python executable not found") def _get_python_version(exec_path, as_string=False): @@ -88,6 +91,7 @@ def _initialize_black_env(upgrade=False): sys.path.insert(0, virtualenv_site_packages) return True + # TODO: Is this check about the Vim plugin, or about Black? pyver = sys.version_info[:3] if pyver < (3, 8): print("Sorry, Black requires Python 3.8+ to run.") From e028d8e08678a25cedfa7f19d1db029e73a5c39c Mon Sep 17 00:00:00 2001 From: Sergiu Bivol Date: Tue, 20 Feb 2024 01:33:07 +0000 Subject: [PATCH 7/8] Update changelog --- CHANGES.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index bcf6eb44fdb..a0d8dd948d5 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -40,7 +40,7 @@ ### Integrations - +- Allow Black to use a different Python version than Vim ### Documentation From bab3d73d89d9379ee1c5e2ab9633d44bda1f7a86 Mon Sep 17 00:00:00 2001 From: Sergiu Bivol Date: Tue, 20 Feb 2024 10:40:04 +0000 Subject: [PATCH 8/8] Add PR number --- CHANGES.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index a0d8dd948d5..cdb31bbed87 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -40,7 +40,7 @@ ### Integrations -- Allow Black to use a different Python version than Vim +- Allow Black to use a different Python version than Vim (#4245) ### Documentation