Skip to main content

flake8 plugin that checks FastAPI code against opiniated style rules 🤓

Project description

flake8-fastapi

Latest Commit
Package version

A flake8 plugin that helps you avoid simple FastAPI mistakes.

Installation

First, install the package:

pip install flake8-fastapi

Then, check if the plugin is installed using flake8:

$ flake8 --version
3.9.2 (flake8-fastapi: 0.2.0, flake8-pytest-style: 1.4.2, mccabe: 0.6.1, pycodestyle: 2.7.0, pyflakes: 2.3.1) CPython 3.8.11 on Linux

Rules

Route Decorator Error (CF001)

Developers that were used to flask can be persuaded or want to use the same pattern in FastAPI:

from fastapi import FastAPI

app = FastAPI()


@app.route("/", methods=["GET"])
def home():
    return "Hello world!"

But on FastAPI, we have a simpler way to define this (and is the most known way to create endpoints):

from fastapi import FastAPI

app = FastAPI()


@app.get("/")
def home():
    return "Hello world!"

Route Prefix Error (CF002)

On old FastAPI versions, we were able to add a prefix only on the include_router method:

from fastapi import APIRouter, FastAPI

router = APIRouter()


@router.get("/")
def home():
    ...


app = FastAPI()
app.include_router(router, prefix="/prefix")

Now, it's possible to add in the Router initialization:

from fastapi import APIRouter, FastAPI

router = APIRouter(prefix="/prefix")


@router.get("/")
def home():
    ...


app = FastAPI()
app.include_router(router)

CORSMiddleware Order (CF008)

There's a tricky issue about CORSMiddleware that people are usually unaware. Which is that this middleware should be the last one on the middleware stack. You can read more about it here.

Let's see an example of what doesn't work:

from fastapi import FastAPI

app = FastAPI()

app.add_middleware(
    CORSMiddleware,
    allow_origins=['*'],
    allow_credentials=True,
    allow_methods=['*'],
    allow_headers=['*']
)
app.add_middleware(GZipMiddleware)

As you see, the last middleware added is not CORSMiddleware, so it will not work as expected. On the other hand, if you change the order, it will:

from fastapi import FastAPI

app = FastAPI()

app.add_middleware(GZipMiddleware)
app.add_middleware(
    CORSMiddleware,
    allow_origins=['*'],
    allow_credentials=True,
    allow_methods=['*'],
    allow_headers=['*']
)

License

This project is licensed under the terms of the MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flake8-fastapi-0.3.0.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

flake8_fastapi-0.3.0-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file flake8-fastapi-0.3.0.tar.gz.

File metadata

  • Download URL: flake8-fastapi-0.3.0.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.7 CPython/3.8.11 Linux/5.8.0-1036-azure

File hashes

Hashes for flake8-fastapi-0.3.0.tar.gz
Algorithm Hash digest
SHA256 48edc5268520b7b715b2a4bbb0e6ed0064f9b6fbe6ffa11fa047f25f2f1ba6d3
MD5 9ff278ae1bc4f4d391f333cd0fafdb6e
BLAKE2b-256 63aae25c627cf4ac8d81095212201939e42e3eaaaf15e92c1105151b8681cac6

See more details on using hashes here.

File details

Details for the file flake8_fastapi-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: flake8_fastapi-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.7 CPython/3.8.11 Linux/5.8.0-1036-azure

File hashes

Hashes for flake8_fastapi-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 885801325c61739baf6bd03568121b4704fa7500ab51ec27603ef25b3c547270
MD5 0263c3ee96b93a224920447af25a69d8
BLAKE2b-256 870c5ab752b3d825db6c8710532805892c6aa8aeaaf3fea6da87a49faada24f6

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page