opendp.typing module#

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:

AssertionError – 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_domain_atom(domain)[source]#
opendp.typing.get_domain_atom_or_infer(domain, example)[source]#
Parameters:

domain (RuntimeType) –

opendp.typing.get_first(value)[source]#