Skip to main content

No project description provided

Project description

trycast

Status: In development. Current limitations in mypy prevent the examples below from actually typechecking successfully, despite functioning correctly at runtime.

This module provides a single function trycast which can be used to parse a JSON-like value.

Here is an example of parsing a Point2D object defined as a TypedDict:

from bottle import HTTPResponse, request, route
from trycast import trycast
from typing import TypedDict

class Point2D(TypedDict):
    x: float
    y: float
    name: str

@route('/draw_point')
def draw_point_endpoint() -> None:
    request_json = request.json  # type: object
    if (point := trycast(Point2D, request_json)) is not None:
        draw_point(point)  # type is narrowed to Point2D
    else:
        return HTTPResponse(status=400)  # Bad Request

def draw_point(point: Point2D) -> None:
    # ...

In this example the trycast function is asked to parse a request_json into a Point2D object, returning the original object (with its type narrowed appropriately) if parsing was successful.

More complex types can be parsed as well, such as the Shape in the following example, which is a tagged union that can be either a Circle or Rect value:

from bottle import HTTPResponse, request, route
from trycast import trycast
from typing import Literal, TypedDict, Union

class Point2D(TypedDict):
    x: float
    y: float

class Circle(TypedDict):
    type: Literal['circle']
    center: Point2D  # a nested TypedDict!
    radius: float

class Rect(TypedDict):
    type: Literal['rect']
    x: float
    y: float
    width: float
    height: float

Shape = Union[Circle, Rect]  # a Tagged Union!

@route('/draw_shape')
def draw_shape_endpoint() -> None:
    request_json = request.json  # type: object
    if (shape := trycast(Shape, request_json)) is not None:
        draw_shape(shape)  # type is narrowed to Shape
    else:
        return HTTPResponse(status=400)  # Bad Request

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

trycast-0.0.2.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

trycast-0.0.2-py3-none-any.whl (3.3 kB view details)

Uploaded Python 3

File details

Details for the file trycast-0.0.2.tar.gz.

File metadata

  • Download URL: trycast-0.0.2.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.8.0 Darwin/18.7.0

File hashes

Hashes for trycast-0.0.2.tar.gz
Algorithm Hash digest
SHA256 3ed11a498d900a3fd7561b86f249be05a4c88f56821df505e5621749b8b94080
MD5 5e66ed21ddc215391bdd98630284714f
BLAKE2b-256 ee4e116abd472ca90d593bfb01c1490509f03a104fd254e98e42d5e001f1fb47

See more details on using hashes here.

File details

Details for the file trycast-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: trycast-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 3.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.8.0 Darwin/18.7.0

File hashes

Hashes for trycast-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 dde13782f390711cb2c065fe254d6a20b9d23e062f3925f530e1dcc4c122b382
MD5 26547108541aa8175608755c261f726c
BLAKE2b-256 d24d69f87c0724a196756a2db4d99c7e3ef57a63db6ef3e1dd5ced905075fe5d

See more details on using hashes here.

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