pygx.contextual¶
Context-scoped value override for PyGX.
contextual
¶
Context-scoped value override for PyGX.
pygx.contextual provides a thread-local stack of variable bindings that
inner code can read via pg.contextual.get. Bindings
are installed by pg.contextual.override (a
context manager) and propagate across threads when a callable is wrapped with
pg.contextual.with_override. The module
sits between pg.topology (for MISSING_VALUE) and the
heavier pg.symbolic layer.
The symbols below are intentionally not re-exported at the top of pg.* —
access them as pg.contextual.<name>.
Override
dataclass
¶
Value marker for a contextual override of an attribute.
Placeholder
¶
Placeholder(
*,
allow_partial: bool = False,
sealed: bool | None = None,
root_path: KeyPath | None = None,
explicit_init: bool = False,
**kwargs: Any
)
Bases: ValueFromParentChain, Extension
Value marker for a contextual attribute on a ContextualObject.
Used as the default value of a field on a pg.ContextualObject
subclass. At access time, the attribute resolves by walking the
containing object chain and consulting any
pg.contextual.override bindings in scope.
Source code in pygx/symbolic/_object.py
2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 | |
all
¶
Returns all values provided from pg.contextual.override in scope.
Source code in pygx/contextual/_contextual.py
get
¶
Returns the value of a variable defined in pg.contextual.override.
Source code in pygx/contextual/_contextual.py
get_override
¶
get_override(var_name: str) -> Override | None
Returns the overridden contextual marker for var_name in scope.
override
¶
override(
*, cascade: bool = False, override_attrs: bool = False, **variables: Any
) -> ContextManager[dict[str, Override]]
Context manager to provide contextual values under a scope.
Contextual value overrides are per-thread. To propagate them to other
threads, wrap the user function with pg.contextual.with_override(func).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cascade
|
bool
|
If True, this override applies to both the current scope and nested scopes, taking precedence over all nested overrides for the listed variables. |
False
|
override_attrs
|
bool
|
If True, the override also applies to attributes that already have explicit values. Otherwise overridden variables are only consulted for contextual attributes whose values are not present. |
False
|
**variables
|
Any
|
Key/values as overrides for contextual attributes. |
{}
|
Returns:
| Type | Description |
|---|---|
ContextManager[dict[str, Override]]
|
A dict of attribute names to their |
Source code in pygx/contextual/_contextual.py
with_override
¶
Wraps a user function so it carries the current contextual overrides.
The wrapped function may be called from another thread; the overrides active at wrap-time will be re-installed for the duration of each call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[..., Any]
|
The user function to be wrapped. |
required |
Returns:
| Type | Description |
|---|---|
Callable[..., Any]
|
A wrapper function that re-installs the captured overrides on each |
Callable[..., Any]
|
invocation, suitable for handing off to another thread. |
Source code in pygx/contextual/_contextual.py
options: show_root_heading: false show_root_toc_entry: false members_order: source heading_level: 2