Skip to main content

Add type hints support to Falcon with Pydantic and Marshmallow integration

Project description

Falcon typing

PyPI - Python Version PyPI codecov Build Status License: MIT

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

**Uses typedjson


from typing import Optional, Union

from pydantic import BaseModel as PydanticModel
from falcontyping import TypedAPI, TypedResource

class UserV1(PydanticModel):

    username: str

class UserV2(PydanticModel):

    username: str
    balance: float

class UsersResource(TypedResource):

    def on_post(self, request, response, user: Union[UserV2, UserV1]) -> Union[UserV2, UserV1]:
        if isinstance(user, UserV2):
            return UserV2(username=user.username, balance=user.balance)

            return UserV1(username=user.username)

class UserDetailsResource(TypedResource):

    def on_get(self, request, response, user_id: int) -> Optional[Union[UserV2, UserV1]]:
        if user_id == 2:
            return UserV2(username='user', balance=0.0)

        if user_id == 1:
            return UserV1(username='user')

        return None

API = TypedAPI()
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.

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

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page