RobertCraigie / pyright-python

Python command line wrapper for pyright, a static type checker
https://pypi.org/project/pyright/
MIT License
178 stars 22 forks source link

Pre-commit hook imports fail with pyenv venv. #265

Open Spenhouet opened 6 months ago

Spenhouet commented 6 months ago

We are using pyenv to manage our virtual environments. I was unable to get the pre-commit hook working in a way that it could be added to our repositories. It always fails to resolve imports.

The docs directly address this here: https://github.com/RobertCraigie/pyright-python?tab=readme-ov-file#pre-commit There one is instructed to set the venvPath and venv settings. We do not have a pytoml in our projects so I tried to configure this using the pyrightconfig.json.

{
    "venvPath": "~/.pyenv/versions",
    "venv": "myproject"
}

This fails in constructing the venv path and concatenates the current dir with the venvPath /home/spe/myproject/~/.pyenv/versions. Entering a absolute path works, but then I can't commit this to our repos. Our team does have the envs all in the same place, as that is how pyenv puts them. So it would be great if the relative home dir path would work.

When I execute pyright via CLI I can simply provide the --pythonpath argument (https://github.com/microsoft/pyright/blob/main/docs/command-line.md), The JSON config does not have this option (https://github.com/microsoft/pyright/blob/main/docs/configuration.md#main-configuration-options).

Since the pre-commit plugin runs the CLI command, I tried adding the --pythonpath as args to the pre-commit hook definition (https://[pre-commit](https://pre-commit.com/#hooks-args).com/#hooks-args), but this seems to be ignored or not passed on.

I then switched to a local hook definition based on https://github.com/RobertCraigie/pyright-python/blob/main/.pre-commit-hooks.yaml. This doesn't work as it uses the python language, which as stated by the maintainer of pre-commit, creates an isolated virtual env for the python execution (https://stackoverflow.com/a/70780205/2230045), always missing the imports. I therefore switched to the system language, which didn't work either when the hook is executed through the VS Code source control commit dialog, as VS code does not seem to make use of the currently active venv for these executions.

What finally worked:

.pre-commit-config.yaml

repos:
  - repo: local
    hooks:
      - id: pyright
        name: pyright
        description: "Python command line wrapper for pyright, a static type checker"
        entry: bin/lint-pyright.sh
        language: script
        "types_or": [python, pyi]
        require_serial: true
        additional_dependencies: []
        minimum_pre_commit_version: "2.9.2"

bin/lint-pyright.sh

#!/bin/bash
VENV_NAME="myproject"

eval "$(pyenv init -)" # initialize pyenv for current shell
eval "$(pyenv virtualenv-init -)" # initialize pyenv-virtualenv for current shell

pyenv shell $VENV_NAME

python -m pyright "$@"

requirements-dev.txt

pyright==1.1.359

Maybe this helps someone search for a solution. But more importantly, maybe something could be improved:

cmclaughlin commented 1 week ago

@Spenhouet thank you very much for posting your solution!