Skip to main content

Pait(π tool) - Python Modern API Tools, easier to use web frameworks/write API routing

Coverage

PyPI - Python Version PyPI

GitHub Workflow Status GitHub release (release name instead of tag name) Test

Support framework


Documentation: https://so1n.me/pait/

中文文档: https://so1n.me/pait-zh-doc/


pait

Pait is an api tool that can be used in any python web framework, the features provided are as follows:

  • Integrate into the Type Hints ecosystem to provide a safe and efficient API interface coding method.
  • Automatic verification and type conversion of request parameters (depends on Pydantic and inspect, currently supports Pydantic V1 and V2 versions).
  • Automatically generate openapi files and support UI components such as Swagger,Redoc,RapiDoc and Elements.
  • TestClient support, response result verification of test cases。
  • Plugin expansion, such as parameter relationship dependency verification, Mock response, etc.。
  • gRPC GateWay (After version 1.0, this feature has been migrated to grpc-gateway)
  • Automated API testing
  • WebSocket support
  • SSE support

Note:

  • mypy check 100%

  • python version >= 3.8 (support postponed annotations)

Installation

pip install pait

Simple Example

import uvicorn  # type: ignore
from starlette.applications import Starlette
from starlette.responses import JSONResponse
from starlette.routing import Route

from pait.app.starlette import pait
from pait.field import Body
from pait.openapi.doc_route import add_doc_route
from pydantic import BaseModel, Field


class ResponseModel(BaseModel):
    """demo response"""
    uid: int = Field()
    user_name: str = Field()


@pait(response_model_list=[ResponseModel])
async def demo_post(
    uid: int = Body.i(description="user id", gt=10, lt=1000),
    user_name: str = Body.i(description="user name", min_length=2, max_length=4)
) -> JSONResponse:
    return JSONResponse({'uid': uid, 'user_name': user_name})


app = Starlette(routes=[Route('/api', demo_post, methods=['POST'])])
add_doc_route(app)
uvicorn.run(app)

See documentation for more features

Support Web framework

Framework Description
Flask All features supported
Sanic All features supported
Starlette All features supported
Tornado All features supported
Django Coming soon

If the web framework is not supported(which you are using).

Can be modified sync web framework according to pait.app.flask

Can be modified async web framework according to pait.app.starlette

Performance

The main operating principle of Pait is to convert the function signature of the route function into Pydantic Model through the reflection mechanism when the program is started, and then verify and convert the request parameters through Pydantic Model when the request hits the route.

These two stages are all automatically handled internally by Pait. The first stage only slightly increases the startup time of the program, while the second stage increases the response time of the routing, but it only consumes 0.00005(s) more than manual processing. The specific benchmark data and subsequent optimization are described in #27.

Example

For more complete examples, please refer to example

Metadata

Release files for pait 1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pait 1.2
File Size Uploaded
pait-1.2.tar.gz 119.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pait 1.2
File Interpreter ABI Platform
pait-1.2-py3-none-any.whl Python 3 none any Details

Total release size: 292.5 kB

Release files / pait-1.2.tar.gz

Download URL pait-1.2.tar.gz
Size 119.1 kB
Tags Source
SHA-256 checksum
How to use checksums
52150f3483f96cac51bafa25c4742a66a965ed1525be38244c55df0603446ad7
BLAKE2b-256 checksum
How to use checksums
761f4daab74084c9cc7be6ad4b7f5a1dbc3e2a925e8eb2dbb293da9fce79e9cb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.12.13 Linux/6.6.138-amd64-desktop-hwe

Release files / pait-1.2-py3-none-any.whl

Download URL pait-1.2-py3-none-any.whl
Size 173.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2826c86d39684d64e08f35311afd300bcc0c0d040c7105a9f23676587e7a55e6
BLAKE2b-256 checksum
How to use checksums
50f7c4c685720a61fa7cf83d23f089f8407c06a9475b5b8eab8587d663467c40
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.12.13 Linux/6.6.138-amd64-desktop-hwe

Release history Release notifications | RSS feed

This release

1.2 This release

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.0

2 release files

0.8.0

2 release files

0.7.9

2 release files

0.7.8

2 release files

0.7.7

2 release files

0.7.6

2 release files

0.7.5

2 release files

0.7.4

2 release files

0.7.3

2 release files

0.7.2

2 release files

0.7.1

2 release files

0.7

2 release files

0.6.3

2 release files

0.6.2

2 release files

0.6.1

2 release files

0.6.0

2 release files

0.5.9

2 release files

0.5.8

2 release files

0.5.7

2 release files

0.5.6

2 release files

0.5.5

2 release files

0.5.4

2 release files

0.5.3

2 release files

0.5.2

2 release files

0.5.0

2 release files

0.4.1

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page