Install & Compatibility
Where this runs
tested against v0.0.32 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.178s · 18.3MB
glibcpy 3.10–3.95 runs
installs and imports cleanly · install 1.7s · import 0.148s · 19MB
16MB installed
● package 16MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
ParameterSpecification
✓ from pyre_extensions import ParameterSpecification
none_throws
✓ from pyre_extensions import none_throws
safe_json
✓ from pyre_extensions import safe_json
This imports the module, functions are then accessed via `safe_json.load`, `safe_json.dumps`, etc.
This quickstart demonstrates the use of `ParameterSpecification` for typing decorators that preserve function signatures, and `none_throws` for asserting non-None values. It defines a decorator `unwrap` that extracts the first item from a list returned by a function, raising an error if the list is empty. Note that Pyre would perform static analysis to ensure type safety based on these annotations.
from typing import TypeVar, Callable, List
from pyre_extensions import ParameterSpecification, none_throws
TParams = ParameterSpecification("TParams")
TReturn = TypeVar("TReturn")
def unwrap(f: Callable[TParams, List[TReturn]]) -> Callable[TParams, TReturn]:
"""Example of a decorator using ParameterSpecification."""
def inner(*args: TParams.args, **kwargs: TParams.kwargs) -> TReturn:
result = f(*args, **kwargs)
# Using none_throws for explicit Optional handling
first_item = none_throws(result[0]) if result else None
if first_item is None:
raise ValueError("List must not be empty for unwrap to extract an item")
return first_item
return inner
@unwrap
def get_first_char_list(s: str, upper: bool = False) -> List[str]:
chars = [c.upper() for c in s] if upper else list(s)
return chars
@unwrap
def get_empty_list(x: int) -> List[int]:
return []
# Example usage
print(f"First char (upper): {get_first_char_list('hello', upper=True)}")
print(f"First char (lower): {get_first_char_list('world')}")
try:
get_empty_list(123)
except ValueError as e:
print(f"Caught expected error for empty list: {e}")
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'pyre_extensions'
The 'pyre-extensions' package has not been installed in the Python environment, or the environment is not activated.
fixpip install pyre-extensions
NameError: name 'ParameterSpecification' is not defined
The 'ParameterSpecification' type was used in code without being explicitly imported from the 'pyre_extensions' library.
fixfrom pyre_extensions import ParameterSpecification
ModuleNotFoundError: No module named 'pyre_extensions.safe_json'
Users attempting to use the 'safe_json' module are referencing it with an incorrect submodule name; the correct module is 'type_safe_json'.
fixfrom pyre_extensions import type_safe_json
NameError: name 'none_throws' is not defined
The 'none_throws' type alias was used in code without being explicitly imported from the 'pyre_extensions' library.
fixfrom pyre_extensions import none_throws
Upgrade
Version history
0.0.32latest on PyPI · released Nov 22, 2024
Audit
Dependencies
pyre-checkrequiredThis library provides extensions specifically for the Pyre type checker and its utility is realized when used in conjunction with 'pyre-check'.