Skip to content

Instantly share code, notes, and snippets.

@LucasRoesler
Last active June 28, 2024 20:16
Show Gist options
  • Save LucasRoesler/a16217be6c0f65d8c2b9a61c8387a6f0 to your computer and use it in GitHub Desktop.
Save LucasRoesler/a16217be6c0f65d8c2b9a61c8387a6f0 to your computer and use it in GitHub Desktop.
My default pyright and ruff configuration for new projects
FROM python:3.12.0-slim-bullseye AS build
RUN apt-get update && apt-get install -y gcc
ENV POETRY_VIRTUALENVS_IN_PROJECT=true \
POETRY_NO_INTERACTION=1 \
POETRY_VIRTUALENVS_OPTIONS_NO_SETUPTOOLS=1
RUN pip install "poetry>=1.8"
WORKDIR /app
# only install depednencies
COPY ./pyproject.toml ./poetry.lock ./
RUN poetry install --no-root --no-directory --only=main
# now add the project to the venv
COPY ./src /app/src
RUN poetry install --only-root
FROM python:3.12.0-slim-bullseye AS app
# This "enables" the virtualenv that we install in the build stage:
ENV PATH="/app/.venv/bin:$PATH"
COPY --from=build /app/.venv /app/.venv
COPY ./src /app/src
WORKDIR /app
CMD ["uvicorn", "--host", "0.0.0.0", "--port", "8080", "<REPLACE_WITH_PROJECT_NAME>.main:app"]
.data
*.pyc
.task
.env
.venv
.pytest_cache
.ruff_cache
__pycache__
tmp/
[tool.pyright]
# see https://microsoft.github.io/pyright/#/configuration?id=pyright-configuration
exclude = [
"**/node_modules",
"**/__pycache__",
"**/.venv",
]
reportMissingImports = true
typeCheckingMode = "basic"
[tool.ruff]
# See https://beta.ruff.rs/docs/configuration/
[tool.ruff.lint]
select = ["E", "F", "I",]
ignore = ["E501"]
extend-safe-fixes = ["F601"]
[tool.ruff.lint.isort]
combine-as-imports = true
version: 3
env:
POETRY_VIRTUALENVS_IN_PROJECT: "true"
POETRY_VIRTUALENVS_PREFER_ACTIVE_PYTHON: "true"
REPOSITORY: "<REPLACE>"
APPLICATION_NAME: "<REPLACE>"
DOCKER_IMAGE: "{{.REPOSITORY}}/{{.APPLICATION_NAME}}"
dotenv:
- .env
tasks:
check_poetry:
desc: Check if poetry is installed
silent: true
internal: true
vars:
poetry_version: "1.8.2"
installed_version:
sh: poetry --version 2>/dev/null | grep -o '[0-9]\+\.[0-9]\+\.[0-9]\+' || echo "0.0.0"
cmds:
- echo "you should install poetry version {{.poetry_version}}, you have {{.installed_version}}"
status:
- |
{{ if (lt .installed_version .poetry_version) }} exit 1 {{ else}} exit 0 {{ end }}
setup:
desc: Create the local development env
cmds:
- task: python-setup
python-setup:
desc: Create a python 3 virtualenv
deps:
- check_poetry
cmds:
- poetry install
sources:
- pyproject.toml
generates:
- ".venv/*"
lint:
desc: lint the python code
deps:
- setup
cmds:
- "poetry run ruff check ."
- "poetry run pyright ."
- "poetry run typos ."
format:
desc: format the python code
deps:
- setup
cmds:
- "poetry run ruff format ."
- "poetry run ruff check --fix-only ."
- "poetry run typos -w ."
test:
desc: Run the tests
deps:
- setup
cmds:
- "poetry run pytest . {{.CLI_ARGS}}"
clean:
desc: Clean the project
cmds:
- rm -rf tmp
- rm -rf .venv
- rm -rf .pytest_cache
- rm -rf .ruff_cache
build:
desc: Build the application
deps:
- setup
cmds:
- docker build -t {{.DOCKER_IMAGE}} .
sources:
- Dockerfile
- pyproject.toml
- src/**/*.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment