opendp.typing module#

class opendp.typing.Carrier(origin, args=None)[source]#

Bases: RuntimeType

class opendp.typing.DatasetMetric(origin, args=None)[source]#

Bases: RuntimeType

All dataset metric RuntimeTypes inherit from DatasetMetric. Provides static type checking in user-code for dataset metrics.

class opendp.typing.Domain(origin, args=None)[source]#

Bases: RuntimeType

class opendp.typing.GenericType(origin, args=None)[source]#

Bases: RuntimeType

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.

class opendp.typing.RuntimeType(origin, args=None)[source]#

Bases: object

Utility for validating, manipulating, inferring and parsing/normalizing type information.

classmethod assert_is_similar(expected, inferred)[source]#

Assert that inferred is a member of the same equivalence class as parsed.

Parameters:
  • expected – the type that the data will be converted to

  • inferred – the type inferred from data

Raises:

TypeError – if expected type differs significantly from inferred type

classmethod infer(public_example)[source]#

Infer the normalized type from a public example.

Parameters:

public_example (Any) – data used to infer the type

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
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 (Union[RuntimeType, _GenericAlias, str, Type[Union[List, Tuple, int, float, str, bool]], tuple]) – type specifier

  • generics (Optional[List[str]]) – 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:

UnknownTypeError – if type_name fails to parse

Examples:

>>> from opendp.typing import RuntimeType, L1Distance
>>> assert RuntimeType.parse(int) == "i32"
>>> assert RuntimeType.parse("i32") == "i32"
>>> assert RuntimeType.parse(L1Distance[int]) == "L1Distance<i32>"
>>> assert 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 (Optional[Union[RuntimeType, _GenericAlias, str, Type[Union[List, Tuple, int, float, str, bool]], tuple]]) – type specifier. See RuntimeType.parse for documentation on valid inputs

  • public_example (Optional[Any]) – data used to infer the type

  • generics (Optional[List[str]]) – 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]#
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.

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, but still pass RuntimeType.assert_is_similar

opendp.typing.get_atom(type_name)[source]#
opendp.typing.get_atom_or_infer(type_name, example)[source]#
Parameters:

type_name (RuntimeType) –

opendp.typing.get_first(value)[source]#
opendp.typing.parse_or_infer(type_name, example)[source]#
Parameters:

type_name (RuntimeType) –

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) –

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, u128, usize, i8, i16, i32, i64, i128, isize]

Parameters:

T (Type Argument) –