Skip to main content

Type system extensions for use with the pyre type checker

Project description

Pyre Extensions

This module defines extensions to the standard “typing” module that are supported by the Pyre typechecker.

none_throws

Function to make assumptions about Optionals explicit. The function will raise an assertion error if passed None and return the value otherwise.

ParameterSpecification

ParameterSpecifications are a special kind of type variable that captures callable parameter specifications (known as argspecs in the runtime and inspect library) instead of types, allowing the typing of decorators which transform the return type of the given callable. For example:

from typing import TypeVar, Callable, List
from pyre_extensions import ParameterSpecification
TParams = ParameterSpecification("TParams")
TReturn = TypeVar("TReturn")
def unwrap(f: Callable[TParams, List[TReturn]]) -> Callable[TParams, TReturn]:
    def inner(*args: TParams.args, **kwargs: TParams.kwargs) -> TReturn:
        return f(*args, **kwargs)[0]

    return inner
@unwrap
def foo(x: int, y: str, z: bool = False) -> List[int]:
    return [1, 2, 3]

decorates foo into a callable that returns int, but still has the same parameters, including their names and whether they are required.

These ParameterSpecification variables also have two special properties, args and kwargs, which correspond to the positional and keyword arguments for a specific call to the ParameterSpecification function. Because the division of parameters into these two argument collections can be different each invocation, these special annotations can only be used in one manner: together, in a function definition, as *args and **kwargs with no other parameters listed.

Safe JSON

The safe_json module provides a type-safe way to parse JSON. It is meant as a drop-in replacement for the builtin json module but instead of returning an object of undefined shape (i.e. Any) allows you to specify the shape of the JSON you're expecting. The parser will validate whether the input matches the expected type and raise an exception if it does not.

Examples

For trivial JSON structures you can use builtin types:

>>> from pyre_extensions import safe_json
>>> from typing import List, Dict
>>> safe_json.loads("[1, 2, 3]", List[int])
[1, 2, 3]
>>> safe_json.loads("[1, 2, 3]", List[str])
# Raises `pyre_extensions.safe_json.InvalidJson`
>>> safe_json.loads('{"key": "value"}', Dict[str, str])
{'key': 'value'}
>>> safe_json.loads('{"key": "value"}', Dict[str, int])
# Raises `pyre_extensions.safe_json.InvalidJson`

For more complicated, nested structures, typed dictionaries are the way to go:

>>> from typing import TypedDict
>>> class Movie(TypedDict):
...     name: str
...     year: int
...
>>> safe_json.loads('{"name": "Blade Runner", "year": 1982 }', Movie)
{'name': 'Blade Runner', 'year': 1982}
>>> safe_json.loads('{"name": "Blade Runner", "year": "1982" }', Movie)
# Raises `pyre_extensions.safe_json.InvalidJson`

Validate if data is expected type:

>>> from pyre_extensions import safe_json
>>> from typing import List, Dict
>>> data = {"foo": 23}
>>> safe_json.validate(data, Dict[str, str])
# Raises `pyre_extensions.safe_json.InvalidJson`
>>> safe_json.validate(data, Dict[str, int])
{"foo": 23}

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

pyre-extensions-0.0.24.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

pyre_extensions-0.0.24-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

Details for the file pyre-extensions-0.0.24.tar.gz.

File metadata

  • Download URL: pyre-extensions-0.0.24.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.8.6

File hashes

Hashes for pyre-extensions-0.0.24.tar.gz
Algorithm Hash digest
SHA256 9554134f630d5918697786d0e1bbba243114b3d20d9bac60d3382763873e72c8
MD5 3aa565d75296732aaa95031a87c51ead
BLAKE2b-256 19ac3ff08b45520fd5fe8d1008beca0123673e205534b972dd5a1c8ed0215251

See more details on using hashes here.

Provenance

File details

Details for the file pyre_extensions-0.0.24-py3-none-any.whl.

File metadata

  • Download URL: pyre_extensions-0.0.24-py3-none-any.whl
  • Upload date:
  • Size: 11.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.8.6

File hashes

Hashes for pyre_extensions-0.0.24-py3-none-any.whl
Algorithm Hash digest
SHA256 7229bbb8748d2f078f1647e5b4841ab86642c7188486a1d5fd17be8c0c0445b1
MD5 25c9a37583e57d5b6b2d93dc0f9945b0
BLAKE2b-256 b44fad5667b1d1008aeb16bf7b62b70ffb51b4b15677093bf68540c8fbbb4e54

See more details on using hashes here.

Provenance

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