pyre-extensions


Namepyre-extensions JSON
Version 0.0.32 PyPI version JSON
download
home_pagehttps://pyre-check.org
SummaryType system extensions for use with the pyre type checker
upload_time2024-11-22 19:26:44
maintainerNone
docs_urlNone
authorFacebook
requires_pythonNone
licenseNone
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Pyre Extensions
This module defines extensions to the standard “typing” module that are supported by the [Pyre typechecker](https://pypi.org/project/pyre-check/).

## `none_throws`
Function to make assumptions about `Optional`s explicit. The function will raise an
assertion error if passed `None` and return the value otherwise.

## ParameterSpecification
`ParameterSpecification`s 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:

```python
>>> 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:
```python
>>> 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:
```python
>>> 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}
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://pyre-check.org",
    "name": "pyre-extensions",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": null,
    "author": "Facebook",
    "author_email": "pyre@fb.com",
    "download_url": "https://files.pythonhosted.org/packages/a7/53/5bc2532536e921c48366ad1047c1344ccef6afa5e84053f0f6e20a453767/pyre_extensions-0.0.32.tar.gz",
    "platform": null,
    "description": "# Pyre Extensions\nThis module defines extensions to the standard \u201ctyping\u201d module that are supported by the [Pyre typechecker](https://pypi.org/project/pyre-check/).\n\n## `none_throws`\nFunction to make assumptions about `Optional`s explicit. The function will raise an\nassertion error if passed `None` and return the value otherwise.\n\n## ParameterSpecification\n`ParameterSpecification`s are a special kind of type variable that captures callable parameter\nspecifications (known as argspecs in the runtime and inspect library) instead of types, allowing\nthe typing of decorators which transform the return type of the given callable.\nFor example:\n```\nfrom typing import TypeVar, Callable, List\nfrom pyre_extensions import ParameterSpecification\nTParams = ParameterSpecification(\"TParams\")\nTReturn = TypeVar(\"TReturn\")\ndef unwrap(f: Callable[TParams, List[TReturn]]) -> Callable[TParams, TReturn]:\n    def inner(*args: TParams.args, **kwargs: TParams.kwargs) -> TReturn:\n        return f(*args, **kwargs)[0]\n\n    return inner\n@unwrap\ndef foo(x: int, y: str, z: bool = False) -> List[int]:\n    return [1, 2, 3]\n```\ndecorates foo into a callable that returns int, but still has the same parameters, including their\nnames and whether they are required.\n\nThese `ParameterSpecification` variables also have two special properties, `args` and `kwargs`,\nwhich correspond to the positional and keyword arguments for a specific call to the\n`ParameterSpecification` function.  Because the division of parameters into these two argument\ncollections can be different each invocation, these special annotations can only be used in one\nmanner: together, in a function definition, as `*args` and `**kwargs` with no other parameters\nlisted.\n\n## Safe JSON\nThe `safe_json` module provides a type-safe way to parse JSON. It is meant as a drop-in replacement\nfor the builtin `json` module but instead of returning an object of undefined shape (i.e. `Any`)\nallows you to specify the shape of the JSON you're expecting. The parser will validate whether the\ninput matches the expected type and raise an exception if it does not.\n\n### Examples\nFor trivial JSON structures you can use builtin types:\n\n```python\n>>> from pyre_extensions import safe_json\n>>> from typing import List, Dict\n>>> safe_json.loads(\"[1, 2, 3]\", List[int])\n[1, 2, 3]\n>>> safe_json.loads(\"[1, 2, 3]\", List[str])\n# Raises `pyre_extensions.safe_json.InvalidJson`\n>>> safe_json.loads('{\"key\": \"value\"}', Dict[str, str])\n{'key': 'value'}\n>>> safe_json.loads('{\"key\": \"value\"}', Dict[str, int])\n# Raises `pyre_extensions.safe_json.InvalidJson`\n```\n\nFor more complicated, nested structures, typed dictionaries are the way to go:\n```python\n>>> from typing import TypedDict\n>>> class Movie(TypedDict):\n...     name: str\n...     year: int\n...\n>>> safe_json.loads('{\"name\": \"Blade Runner\", \"year\": 1982 }', Movie)\n{'name': 'Blade Runner', 'year': 1982}\n>>> safe_json.loads('{\"name\": \"Blade Runner\", \"year\": \"1982\" }', Movie)\n# Raises `pyre_extensions.safe_json.InvalidJson`\n```\n\nValidate if data is expected type:\n```python\n>>> from pyre_extensions import safe_json\n>>> from typing import List, Dict\n>>> data = {\"foo\": 23}\n>>> safe_json.validate(data, Dict[str, str])\n# Raises `pyre_extensions.safe_json.InvalidJson`\n>>> safe_json.validate(data, Dict[str, int])\n{\"foo\": 23}\n```\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Type system extensions for use with the pyre type checker",
    "version": "0.0.32",
    "project_urls": {
        "Homepage": "https://pyre-check.org"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a47a9812cb8be9828ab688203c5ac5f743c60652887f0c00995a6f6f19f912bd",
                "md5": "b32ce5c38806891a4ccb97c9a4cb16c2",
                "sha256": "a63ba6883ab02f4b1a9f372ed4eb4a2f4c6f3d74879aa2725186fdfcfe3e5c68"
            },
            "downloads": -1,
            "filename": "pyre_extensions-0.0.32-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "b32ce5c38806891a4ccb97c9a4cb16c2",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 12766,
            "upload_time": "2024-11-22T19:26:42",
            "upload_time_iso_8601": "2024-11-22T19:26:42.465359Z",
            "url": "https://files.pythonhosted.org/packages/a4/7a/9812cb8be9828ab688203c5ac5f743c60652887f0c00995a6f6f19f912bd/pyre_extensions-0.0.32-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a7535bc2532536e921c48366ad1047c1344ccef6afa5e84053f0f6e20a453767",
                "md5": "bc7b257ba8f94927fdce754c3a0b2c81",
                "sha256": "5396715f14ea56c4d5fd0a88c57ca7e44faa468f905909edd7de4ad90ed85e55"
            },
            "downloads": -1,
            "filename": "pyre_extensions-0.0.32.tar.gz",
            "has_sig": false,
            "md5_digest": "bc7b257ba8f94927fdce754c3a0b2c81",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 10852,
            "upload_time": "2024-11-22T19:26:44",
            "upload_time_iso_8601": "2024-11-22T19:26:44.152854Z",
            "url": "https://files.pythonhosted.org/packages/a7/53/5bc2532536e921c48366ad1047c1344ccef6afa5e84053f0f6e20a453767/pyre_extensions-0.0.32.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-11-22 19:26:44",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "pyre-extensions"
}
        
Elapsed time: 1.34903s