Skip to content

Commit

Permalink
Configure pyright to work with pyenv (#105)
Browse files Browse the repository at this point in the history
* Ignore pyrightconfig in gitignore

* Configure pyright to work with pyenv in makefile

* Add ruff config for trunk
  • Loading branch information
katybaulch authored Mar 25, 2024
1 parent dae2faf commit ce70848
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 48 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ ipython_config.py

# pyenv
.python-version
pyrightconfig.json

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
Expand Down
8 changes: 8 additions & 0 deletions .trunk/configs/ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Generic, formatter-friendly config.
select = ["B", "D3", "E", "F"]

# Never enforce `E501` (line length violations). This should be handled by formatters.
ignore = ["E501"]

[lint]
ignore-init-module-imports = true
33 changes: 33 additions & 0 deletions .trunk/configure-pyright-with-pyenv.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#! /usr/bin/env bash
set -e

# Get the name of the expected venv for this repo from the pyproject.toml file.
venv_name=$(grep -m 1 venv pyproject.toml | tr -s ' ' | tr -d '"' | tr -d "'" | cut -d' ' -f3) || true

# Check if pyrightconfig already exists.
if [[ ! -f pyrightconfig.json ]]; then
# Check if pyenv-pyright plugin is installed
if ! command -v pyenv &>/dev/null; then
echo "pyenv not installed. Please install pyenv..."
exit 1
fi

# Check if pyenv-pyright plugin is installed
if ! command -v pyenv pyright &>/dev/null; then
echo "pyenv-pyright not installed. Installing..."
pyenv_root=$(pyenv root)
git clone https://github.com/alefpereira/pyenv-pyright.git "${pyenv_root}"/plugins/pyenv-pyright
fi

# Generate the pyrightconfig.json file.
pyenv pyright "${venv_name}"
pyenv local "${venv_name}"

fi

# Check whether required keys are present in pyrightconfig.json.
if ! jq -r --arg venv_name "${venv_name}" '. | select(.venv != $venv_name) | select(.venvPath != null)' pyrightconfig.json; then
echo "Failed to configure pyright to use pyenv environment '${venv_name}' as interpreter. Please check pyrightconfig.json..."
exit 1
fi
exit 0
26 changes: 16 additions & 10 deletions .trunk/trunk.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
# To learn more about the format of this file, see https://docs.trunk.io/reference/trunk-yaml
version: 0.1
cli:
version: 1.20.1
version: 1.21.0

# Trunk provides extensibility via plugins.
# (https://docs.trunk.io/plugins)
plugins:
sources:
- id: trunk
ref: v1.4.4
ref: v1.4.5
uri: https://github.com/trunk-io/plugins

# Many linters and tools depend on runtimes - configure them here.
Expand All @@ -34,34 +34,40 @@ lint:
enabled:
- [email protected]
- [email protected]
- black@23.1.0
- [email protected].34
- black@24.3.0
- [email protected].44
- git-diff-check
- [email protected]
- [email protected]
- [email protected]
- osv-scanner@1.6.2
- osv-scanner@1.7.0
- [email protected]:
commands:
- end-of-file-fixer
- check-json
- detect-aws-credentials
- [email protected]
- [email protected].294
- [email protected].2
- [email protected].355
- [email protected].4
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- trivy@0.49.1
- trufflehog@3.69.0
- trivy@0.50.0
- trufflehog@3.70.3
- [email protected]

actions:
disabled:
- trunk-announce
- trunk-check-pre-push
- trunk-announce
enabled:
- trunk-check-pre-commit
- configure-pyright-with-pyenv
- trunk-fmt-pre-commit
- trunk-upgrade-available
definitions:
- id: configure-pyright-with-pyenv
run: source .trunk/configure-pyright-with-pyenv.sh
triggers:
- git_hooks: [pre-commit]
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ uninstall_trunk:
rm -ifr ${HOME}/.cache/trunk

git_hooks: install_trunk
trunk actions run configure-pyright-with-pyenv

check:
trunk fmt
trunk check

Expand Down
39 changes: 1 addition & 38 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,41 +72,4 @@ exclude = "^/alembic/versions/"
include = ["app", "unit_tests", "integration_tests"]
exclude = ["**/__pycache__"]
pythonVersion = "3.9"

[tool.ruff]
select = ["E", "F", "D"]
# Docstring Ignores:
# D100 - Missing docstring in public module
# D103 - Missing docstring in public function
# D104 - Missing docstring in public package
# D107 - Missing docstring in __init__
# D202 - No blank lines allowed after function docstring
# D203 - 1 blank line required before class docstring
# D213 - Multi-line docstring summary should start at the first line
# D400 - First line should end with a period
# D401 - First line should be in imperative mood
# D406 - Section name should end with a newline
# D407 - Missing dashed underline after section
# D413 - Missing blank line after last section
# D415 - First line should end with a period, question mark, or exclamation point
ignore = [
"D100",
"D103",
"D104",
"D107",
"D202",
"D203",
"D212",
"D400",
"D401",
"D406",
"D407",
"D413",
"D415",
]
line-length = 88

# Ignore `E402` (import violations) in all `__init__.py` files, and in `path/to/file.py`.
[tool.ruff.per-file-ignores]
"__init__.py" = ["F401"]
"tests/*" = ["E501"]
venv = "admin-backend"

0 comments on commit ce70848

Please sign in to comment.