Skip to main content

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}

Release files for pyre-extensions 0.0.32

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyre-extensions 0.0.32
File Size Uploaded
pyre_extensions-0.0.32.tar.gz 10.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pyre-extensions 0.0.32
File Interpreter ABI Platform
pyre_extensions-0.0.32-py3-none-any.whl Python 3 none any Details

Total release size: 23.6 kB

Release files / pyre_extensions-0.0.32.tar.gz

Download URL pyre_extensions-0.0.32.tar.gz
Size 10.9 kB
Tags Source
SHA-256 checksum
How to use checksums
5396715f14ea56c4d5fd0a88c57ca7e44faa468f905909edd7de4ad90ed85e55
BLAKE2b-256 checksum
How to use checksums
a7535bc2532536e921c48366ad1047c1344ccef6afa5e84053f0f6e20a453767
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.8.6

Release files / pyre_extensions-0.0.32-py3-none-any.whl

Download URL pyre_extensions-0.0.32-py3-none-any.whl
Size 12.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a63ba6883ab02f4b1a9f372ed4eb4a2f4c6f3d74879aa2725186fdfcfe3e5c68
BLAKE2b-256 checksum
How to use checksums
a47a9812cb8be9828ab688203c5ac5f743c60652887f0c00995a6f6f19f912bd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.8.6

Release history Release notifications | RSS feed

This release

0.0.32 This release

2 release files

0.0.30

2 release files

0.0.29

2 release files

0.0.28

2 release files

0.0.27

2 release files

0.0.25

2 release files

0.0.24

2 release files

0.0.22

2 release files

0.0.18

2 release files

0.0.17

2 release files

0.0.16

2 release files

0.0.15

2 release files

0.0.14

2 release files

0.0.13

2 release files

0.0.12

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page