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=BaseResponsesummaryoperation_iddescription
同时检查查询参数的命名规范和文档规范。
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=BaseResponsefastapi-router-summary(W9002): Router decorator missing summary parameterfastapi-router-operation-id(W9003): Router decorator missing operation_id parameterfastapi-query-params-docs(W9004): Query parameter missing title or descriptionfastapi-router-description(W9005): Router decorator missing description parameterfastapi-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:
- Parameter name should be in snake_case (
page_sizeinstead ofpageSize) - Query decorator should include
alias="pageSize"to maintain API compatibility - Query decorator should include documentation (
titleanddescription)
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5812b91b22a5ca014eba7bc3025004b14a45ed3133c4bc6a739d2a08a7765083
|
|
| MD5 |
a9dd6450aee558eb38f9ca5ea27fcdbd
|
|
| BLAKE2b-256 |
014cc5778bd7eb3df09146e8da82ba3ae516bccb74736059c6f7f533c61dab24
|
File details
Details for the file pylint_fastapi_plugin-0.2.0-py3-none-any.whl.
File metadata
- Download URL: pylint_fastapi_plugin-0.2.0-py3-none-any.whl
- Upload date:
- Size: 6.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.5 CPython/3.11.11 Darwin/24.3.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
836c50623b9f0ad5570dcfcb30c931a2efe53fc07509507a89ad06946957f7ce
|
|
| MD5 |
372e3f267b479c42eb385ae448ce6b61
|
|
| BLAKE2b-256 |
12c3b72ab821d5d1c69b94043c0c2fae78aa25356f68e5aad635f72d24ded304
|