File size: 1,226 Bytes
d06fa57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
"""Resolve expression inputs from value mappings or context objects."""

from __future__ import annotations

from collections.abc import Mapping
from typing import Any


def resolve_context(
    context: Mapping[str, Any] | Any | None,
    target: Any = None,
    num_nodes: int | None = None,
) -> tuple[Mapping[str, Any], Any, int | None]:
    """Resolve values, target observations, and node metadata from a context.

    A mapping is always interpreted directly as symbol values. Context objects
    must expose a mapping-valued ``data`` field and may expose ``target`` and
    ``num_nodes`` metadata.
    """
    if context is None:
        values: Mapping[str, Any] = {}
    elif isinstance(context, Mapping):
        values = context
    else:
        candidate = getattr(context, "data", None)
        if not isinstance(candidate, Mapping):
            raise TypeError("context must expose mapping-valued data")
        values = candidate

    if num_nodes is None:
        num_nodes = getattr(context, "num_nodes", None)

    if target is None:
        target = getattr(context, "target", None)
    if isinstance(target, str) and target in values:
        target = values[target]
    return values, target, num_nodes