opendp.typing module#
The typing
module provides utilities that bridge between Python and Rust types.
OpenDP relies on precise descriptions of data types to make its security guarantees:
These are more natural in Rust with its fine-grained type system,
but they may feel out of place in Python. These utilities try to fill that gap.
- class opendp.typing.Carrier(origin, args=None)[source]#
Bases:
RuntimeType
- Parameters:
origin (str) –
args (List[RuntimeType | str]) –
- class opendp.typing.DomainDescriptor(origin, args=None)[source]#
Bases:
RuntimeType
- Parameters:
origin (str) –
args (List[RuntimeType | str]) –
- class opendp.typing.GenericType(origin, args=None)[source]#
Bases:
RuntimeType
- Parameters:
origin (str) –
args (List[RuntimeType | str]) –
- class opendp.typing.PrivacyMeasure(origin, args=None)[source]#
Bases:
RuntimeType
All measure RuntimeTypes inherit from PrivacyMeasure. Provides static type checking in user-code for privacy measures and a getitem interface like stdlib typing.
- Parameters:
origin (str) –
args (List[RuntimeType | str]) –
- class opendp.typing.RuntimeType(origin, args=None)[source]#
Utility for validating, manipulating, inferring and parsing/normalizing type information.
- Parameters:
origin (str) –
args (List[RuntimeType | str]) –
- args: List[RuntimeType | str]#
- classmethod infer(public_example, py_object=False)[source]#
Infer the normalized type from a public example.
- Parameters:
public_example (Any) – data used to infer the type
py_object – return “ExtrinsicObject” when type not recognized, instead of error
- Returns:
Normalized type. If the type has subtypes, returns a RuntimeType, else a str.
- Return type:
Union[“RuntimeType”, str]
- Raises:
UnknownTypeException – if inference fails on public_example
- Examples:
>>> from opendp.typing import RuntimeType, L1Distance >>> assert RuntimeType.infer(23) == "i32" >>> assert RuntimeType.infer(12.) == "f64" >>> assert RuntimeType.infer(["A", "B"]) == "Vec<String>" >>> assert RuntimeType.infer((12., True, "A")) == "(f64, bool,String)" # eq doesn't care about whitespace
>>> print(RuntimeType.infer([])) Traceback (most recent call last): ... opendp.mod.UnknownTypeException: attempted to create a type_name with an unknown type: cannot infer atomic type when empty
- origin: str#
- classmethod parse(type_name, generics=None)[source]#
Parse type descriptor into a normalized Rust type.
Type descriptor may be expressed as:
Python type hints from std typing module
plaintext Rust type strings for setting specific bit depth
Python type class - one of {int, str, float, bool}
tuple of type information - for example: (float, float)
- Parameters:
type_name (RuntimeType | str | Type[List[Any] | Tuple[Any, Any] | int | float | str | bool] | Tuple[RuntimeTypeDescriptor, ...] | _GenericAlias | GenericAlias) – type specifier
generics (List[str] | None) – For internal use. List of type names to consider generic when parsing.
- Type:
List[str]
- Returns:
Normalized type. If the type has subtypes, returns a RuntimeType, else a str.
- Return type:
Union[“RuntimeType”, str]
- Raises:
UnknownTypeException – if type_name fails to parse
- Examples:
>>> from opendp.typing import RuntimeType, L1Distance >>> RuntimeType.parse(int) 'i32' >>> RuntimeType.parse("i32") 'i32' >>> print(RuntimeType.parse(L1Distance[int])) L1Distance<i32> >>> print(RuntimeType.parse(L1Distance["f32"])) L1Distance<f32>
- classmethod parse_or_infer(type_name=None, public_example=None, generics=None)[source]#
If type_name is supplied, normalize it. Otherwise, infer the normalized type from a public example.
- Parameters:
type_name (RuntimeType | str | Type[List[Any] | Tuple[Any, Any] | int | float | str | bool] | Tuple[RuntimeType | str | Type[List[Any] | Tuple[Any, Any] | int | float | str | bool] | Tuple[RuntimeTypeDescriptor, ...] | _GenericAlias | GenericAlias, ...] | _GenericAlias | GenericAlias | None) – type specifier. See RuntimeType.parse for documentation on valid inputs
public_example (Any | None) – data used to infer the type
generics (List[str] | None) – For internal use. List of type names to consider generic when parsing.
- Returns:
Normalized type. If the type has subtypes, returns a RuntimeType, else a str.
- Return type:
Union[“RuntimeType”, str]
- Type:
List[str]
- Raises:
ValueError – if type_name fails to parse
UnknownTypeException – if inference fails on public_example or no args are supplied
- substitute(**kwargs)[source]#
- Parameters:
self (RuntimeType | str) –
- class opendp.typing.SensitivityMetric(origin, args=None)[source]#
Bases:
RuntimeType
All sensitivity RuntimeTypes inherit from SensitivityMetric. Provides static type checking in user-code for sensitivity metrics and a getitem interface like stdlib typing.
- Parameters:
origin (str) –
args (List[RuntimeType | str]) –
- class opendp.typing.UnknownType(reason)[source]#
Bases:
RuntimeType
Indicator for a type that cannot be inferred. Typically the atomic type of an empty list. RuntimeTypes containing UnknownType cannot be used in FFI
- args: None#
- origin: None#
- opendp.typing.get_atom_or_infer(type_name, example)[source]#
- Parameters:
type_name (RuntimeType | str) –
- opendp.typing.get_carrier_type(value)[source]#
- Parameters:
value (Domain) –
- Return type:
RuntimeType | str
- opendp.typing.get_dependencies(value)[source]#
- Parameters:
value (Measurement | Transformation | Function) –
- Return type:
Any
- opendp.typing.get_dependencies_iterable(value)[source]#
- Parameters:
value (List[Measurement | Transformation | Function]) –
- Return type:
List[Any]
- opendp.typing.get_distance_type(value)[source]#
- Parameters:
- Return type:
RuntimeType | str
- opendp.typing.parse_or_infer(type_name, example)[source]#
- Parameters:
type_name (RuntimeType | str | Type[List[Any] | Tuple[Any, Any] | int | float | str | bool] | Tuple[RuntimeType | str | Type[List[Any] | Tuple[Any, Any] | int | float | str | bool] | Tuple[RuntimeTypeDescriptor, ...] | _GenericAlias | GenericAlias, ...] | _GenericAlias | GenericAlias | None) –
- Return type:
RuntimeType | str
- opendp.typing.set_default_float_type(T)[source]#
Set the default float type throughout the library. This function is particularly useful when building computation chains with constructors. When you build a computation chain, any unspecified float types default to this float type.
The default float type is f64.
- Params T:
must be one of [f32, f64]
- Parameters:
T (Type Argument) –
- Return type:
None
- opendp.typing.set_default_int_type(T)[source]#
Set the default integer type throughout the library. This function is particularly useful when building computation chains with constructors. When you build a computation chain, any unspecified integer types default to this int type.
The default int type is i32.
- Params T:
must be one of [u8, u16, u32, u64, usize, i8, i16, i32, i64]
- Parameters:
T (Type Argument) –
- Return type:
None