Skip to main content

flask-msgspec

msgspec integration for Flask

This project was inspired by the flask-pydantic package created by bauerji and the Litestar framework, however while the validate decorator appears similar to the one found in flask-pydantic there are many differences.

Installation

pip install flask-msgspec

Usage

Consider this simple example:

class BodyStructWithConstraints(msgspec.Struct):
    foo: Annotated[int, msgspec.Meta(gt=0, le=100)]
    bar: Annotated[list[str | int], msgspec.Meta(min_length=1)]


@app.post(rule="/test/<uuid:uuid_value>")
@validate()
def test_handler(
    uuid_value: UUID,
    query1: float,
    body: BodyStructWithConstraints,
    optional_query: str = "default_value",
) -> dict:
    return locals()

Here we have a UUID path parameter, a required query parameter of float type, a body of type BodyStructWithConstraints, and an optional query parameter which is a string, the endpoint will return a dictionary of unknown types.

Currently there is only one reserved keyword; body. This tries to convert either request.data or request.form to the specified type.

Similar to how Litestar works, keywords that are neither path parameters or reserved keywords are considered query parameters.

The return type can be set either:

  • via the return_model keyword in validate decorator, or
  • by annotating the function return type. (return_model keyword takes priority)

Sequences/iterables can also be used for return type, e.g.; list[ResponseModel].

The successful response status code can also be changed in two ways:

  • via setting the status_code keyword in validate decorator, or
  • by using the standard Flask syntax of returning a tuple.

Returning a tuple with a status code will override the value set by the status_code keyword.

As you might have noticed mixing these together most likely will cause issues or just going to be annoying to annotate.
Avoid using the standard Flask tuple return syntax to change the status code if you are also using the function return type to annotate the return model. This will cause issues; first with a static type checker, then with the code handling return values and conversion.
In my opinion the response model should be set via annotating the return type of the function and if the status code needs to be changed use the status_code keyword. Additionally, if you need to set headers, then set both, the return type and status code using the keywords of the validate decorator.

You can use msgspec.Struct, dataclass, and most built-in types natively supported by msgspec.

More examples and others stuff will be added soon:tm:.

Release files for flask-msgspec 0.0.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 flask-msgspec 0.0.2
File Size Uploaded
flask_msgspec-0.0.2.tar.gz 5.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for flask-msgspec 0.0.2
File Interpreter ABI Platform
flask_msgspec-0.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 11.1 kB

Release files / flask_msgspec-0.0.2.tar.gz

Download URL flask_msgspec-0.0.2.tar.gz
Size 5.5 kB
Tags Source
SHA-256 checksum
How to use checksums
a30cec9d91c5d2f7da0408f06764f02665d5d8fdda285b40240ccbf16e4a9764
BLAKE2b-256 checksum
How to use checksums
e7c2d5fc0f5501120f994543f41e32902d3d6777ae00a880c3058733fa5b9eeb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.6.1 CPython/3.11.4 Windows/10

Release files / flask_msgspec-0.0.2-py3-none-any.whl

Download URL flask_msgspec-0.0.2-py3-none-any.whl
Size 5.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6c4851aa365e172477b62e01a6f23aa80d1971856a3dc1f7feeb29e4656c9ef1
BLAKE2b-256 checksum
How to use checksums
9b357a3d65dbc3806ad3529e596446558f8310d1dcb39ba022438e40b6ab82ea
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.6.1 CPython/3.11.4 Windows/10

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files

0.0.1

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