Skip to main content

Add type hints support to Falcon with Pydantic and Marshmallow integration

Project description

Falcon typing

Use type hints to specify request parameters with Marshmallow and Pydantic support.

Uses typedjson

Example

"""API."""
from typing import Union

import falcon

# Import Marshmallow
from marshmallow import Schema as MarhmallowSchema
from marshmallow import fields

# Import Pydantic
from pydantic import BaseModel as PydanticModel
from pydantic import ValidationError

from falcontyping import TypedAPI, TypedResource, TypingMiddleware

# Add typing support to Falcon
API = falcon.API(middleware=[TypingMiddleware()])
API = TypedAPI(API)


class UserPydantic(PydanticModel):

    username: str


class UserMarshmallow(MarhmallowSchema):

    username = fields.String()


class UserResource(TypedResource):

    # A rather contrived example
    def on_post(self, request, response,
                # Specify query parameter type hint
                _id : int,
                # Specify body parameter type hint
                user: Union[UserPydantic, UserMarshmallow]) -> Union[UserPydantic, UserMarshmallow]:

        return UserMarshmallow().load({'username': user.username})


API.add_route('/users/{_id}', UserResource())

How to install

pip install falcontyping

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

falcontyping-0.2.5.tar.gz (8.8 kB view hashes)

Uploaded Source

Built Distribution

falcontyping-0.2.5-py3-none-any.whl (14.3 kB view hashes)

Uploaded Python 3

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