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


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()])

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.

Files for falcontyping, version 0.2.5
Filename, size File type Python version Upload date Hashes
Filename, size falcontyping-0.2.5-py3-none-any.whl (14.3 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size falcontyping-0.2.5.tar.gz (8.8 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page