Skip to main content

A Pylint plugin that enforces FastAPI router decorator and model conventions

Project description

Pylint FastAPI Plugin

A Pylint plugin to enforce FastAPI router decorator parameters.

This plugin checks that FastAPI router decorators include required parameters:

  • response_model=BaseResponse
  • summary
  • operation_id
  • description

同时检查查询参数的命名规范和文档规范。

Installation

pip install pylint-fastapi-plugin

Usage

Add the following to your .pylintrc:

[MASTER]
load-plugins=pylint_fastapi_plugin

[MESSAGES CONTROL]
enable=fastapi-router-response-model,
       fastapi-router-summary,
       fastapi-router-operation-id,
       fastapi-router-description,
       fastapi-query-params-docs,
       fastapi-router-page-size-param

Testing

To run the tests:

cd tests
python run_tests.py

You can also test the plugin on a sample FastAPI application:

cd tests
python test_on_example.py

查看 TESTING.md 获取更多关于测试策略和结果的详细信息。

Rules

  • fastapi-router-response-model (W9001): Router decorator missing response_model=BaseResponse
  • fastapi-router-summary (W9002): Router decorator missing summary parameter
  • fastapi-router-operation-id (W9003): Router decorator missing operation_id parameter
  • fastapi-query-params-docs (W9004): Query parameter missing title or description
  • fastapi-router-description (W9005): Router decorator missing description parameter
  • fastapi-router-page-size-param (W9007): Query parameter should be renamed to 'page_size' with alias

PageSize Parameter Checker

This plugin also checks and fixes the naming convention for FastAPI query parameters related to page size:

  1. Parameter name should be in snake_case (page_size instead of pageSize)
  2. Query decorator should include alias="pageSize" to maintain API compatibility
  3. Query decorator should include documentation (title and description)

Example

Incorrect:

@router.get("/list")
async def list_users(pageSize: int = Query(10)):
    # ...

Correct:

@router.get("/list")
async def list_users(page_size: int = Query(10, alias="pageSize", title="每页数量", description="每页显示的记录数")):
    # ...

Configuration

Add the following to your .pylintrc:

[MASTER]
load-plugins=pylint_fastapi_plugin

[MESSAGES CONTROL]
enable=W9007

Auto-fix

The plugin automatically applies fixes when running pylint:

python -m pylint your_file.py

示例

查看 fixed_example_app.py 获取符合所有规则的示例代码。

贡献

欢迎提交 Pull Request 和 Issue 来改进这个插件。

许可证

MIT

Quickstart with Poetry

# 安装依赖并创建虚拟环境
poetry install

# 进入虚拟环境
poetry shell

# 本地开发模式安装到环境
poetry run pip install -e .

# 运行 pylint 验证插件生效
poetry run pylint --load-plugins=pylint_fastapi_plugin your_code_dir/

发布到 PyPI

# 构建
poetry build

# 发布(需先配置 PyPI token)
poetry publish --username __token__ --password <pypi-token>

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

pylint_fastapi_plugin-0.2.0.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pylint_fastapi_plugin-0.2.0-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file pylint_fastapi_plugin-0.2.0.tar.gz.

File metadata

  • Download URL: pylint_fastapi_plugin-0.2.0.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.11.11 Darwin/24.3.0

File hashes

Hashes for pylint_fastapi_plugin-0.2.0.tar.gz
Algorithm Hash digest
SHA256 5812b91b22a5ca014eba7bc3025004b14a45ed3133c4bc6a739d2a08a7765083
MD5 a9dd6450aee558eb38f9ca5ea27fcdbd
BLAKE2b-256 014cc5778bd7eb3df09146e8da82ba3ae516bccb74736059c6f7f533c61dab24

See more details on using hashes here.

File details

Details for the file pylint_fastapi_plugin-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pylint_fastapi_plugin-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 836c50623b9f0ad5570dcfcb30c931a2efe53fc07509507a89ad06946957f7ce
MD5 372e3f267b479c42eb385ae448ce6b61
BLAKE2b-256 12c3b72ab821d5d1c69b94043c0c2fae78aa25356f68e5aad635f72d24ded304

See more details on using hashes here.

Supported by

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