Skip to content

Instantly share code, notes, and snippets.

@panicoenlaxbox
Last active November 18, 2022 12:10
Show Gist options
  • Save panicoenlaxbox/3f2551f51a4d4402052d66bd6221d10f to your computer and use it in GitHub Desktop.
Save panicoenlaxbox/3f2551f51a4d4402052d66bd6221d10f to your computer and use it in GitHub Desktop.
FastAPI setup using poetry
$app = "web_app2"
$directory = "WebApp2"
poetry new --name $app --src $directory
cd $directory
poetry shell
poetry add -G dev mypy pytest black isort flake8 assertpy pre-commit flake8-class-attributes-order pytest-asyncio pytest-cov flake8-pyproject
echo "[tool.black]`
line-length = 120`
target-version = ['py310']`
`
[tool.isort]`
multi_line_output = 3`
include_trailing_comma = true`
force_grid_wrap = 0`
use_parentheses = true`
ensure_newline_before_comments = true`
line_length = 120`
`
[tool.flake8]`
max-line-length = 120`
max-doc-length = 140`
use_class_attributes_order_strict_mode = true`
`
[tool.mypy]`
show_error_codes = true`
pretty = true`
show_error_context = true`
show_column_numbers = true`
warn_unused_ignores = true`
disallow_untyped_defs = false`
`
[[tool.mypy.overrides]]`
module = ""assertpy""`
ignore_missing_imports = true`
`
[tool.pytest.ini_options]`
pythonpath = ""src/""`
asyncio_mode = ""auto""`
`
[tool.coverage.run]`
omit = [""tests/*""]`
`
[tool.coverage.report]`
exclude_lines = [""pass""]" >> pyproject.toml
echo "fail_fast: true`
repos:`
- repo: https://github.com/python-poetry/poetry`
rev: 1.2.2`
hooks:`
- id: poetry-check`
verbose: true`
- id: poetry-lock`
verbose: true`
- repo: https://github.com/PyCQA/isort`
rev: 5.10.1`
hooks:`
- id: isort`
verbose: true`
- repo: https://github.com/psf/black`
rev: 22.10.0`
hooks:`
- id: black`
verbose: true`
- repo: https://github.com/john-hen/Flake8-pyproject`
rev: 1.2.0`
hooks:`
- id: Flake8-pyproject`
verbose: true`
- repo: https://github.com/pre-commit/mirrors-mypy`
rev: v0.991`
hooks:`
- id: mypy`
verbose: true" > .pre-commit-config.yaml
Invoke-RestMethod -Method Get -Uri "https://www.toptal.com/developers/gitignore/api/python,pycharm,visualstudiocode" | Out-File .gitignore
git init
pre-commit install
poetry add fastapi[all]
echo "import uvicorn`
from fastapi import FastAPI`
`
app = FastAPI()`
`
`
@app.get("/")`
async def root():`
return ""Hello World!""`
`
`
if __name__ == ""__main__"":`
uvicorn.run(""app:app"", reload=True)" > src\app.py
echo "from starlette.testclient import TestClient`
`
from web_app2.app import app`
from http import HTTPStatus`
`
client = TestClient(app)`
`
`
def test_root():`
r = client.get(""/"")`
assert r.status_code == HTTPStatus.OK`
assert r.json() == ""Hello World!""" > tests\test_app.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment