Skip to main content

Add type hints support to Falcon with Pydantic and Marshmallow integration

Project description

Falcon typing

PyPI - Python Version PyPI License: MIT

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

Uses typedjson

Example

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

from marshmallow import Schema as MarhmallowSchema
from marshmallow import fields
from pydantic import BaseModel as PydanticModel

# Create API
from falcontyping import TypedAPI, TypedResource
API = TypedAPI()


class UserV1(MarhmallowSchema):

    username = fields.String()


class UserV2(PydanticModel):

    username: str


class UserResource(TypedResource):

    def on_post(self, request, response, user: Union[UserV1, UserV2]) -> Union[UserV1, UserV2]:
        if isinstance(user, UserV1):
            return UserV1().load({'username': user.username})

        else:
            return UserV2(username=user.username)


class UserDetailsResource(TypedResource):

    def on_get(self, request, response, user_id: int) -> UserV2:
        return UserV2(username='user')


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

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.3.0.tar.gz (9.0 kB view hashes)

Uploaded Source

Built Distribution

falcontyping-0.3.0-py3-none-any.whl (14.4 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