44
55import itertools
66import typing as t
7+ from enum import Enum
8+
9+
10+ class VariableSource (str , Enum ):
11+ """Which mechanism supplied the variable metadata.
12+
13+ Used for human-readable display only; not dispatched on.
14+ """
15+
16+ DAP = "dap"
17+ FALLBACK = "fallback"
718
819
920class VariablesUnavailable (Exception ):
@@ -131,7 +142,7 @@ def list_variables(kernel, *, timeout: float = 5.0) -> dict[str, t.Any]:
131142 Returns:
132143 Dict with keys:
133144 - "variables": list of {"name", "type", "value", "variables_reference"}
134- - "source": "dap" or "fallback"
145+ - "source": VariableSource (DAP or FALLBACK)
135146
136147 Raises:
137148 VariablesUnavailable: if neither path succeeds.
@@ -151,14 +162,14 @@ def list_variables(kernel, *, timeout: float = 5.0) -> dict[str, t.Any]:
151162 wsc = kernel ._manager .client
152163 raw = _dap_inspect_variables (wsc , timeout = timeout )
153164 variables = [_normalise_dap_variable (v ) for v in raw ]
154- return {"variables" : variables , "source" : "dap" }
165+ return {"variables" : variables , "source" : VariableSource . DAP }
155166 except Exception :
156167 pass # fall through to fallback
157168
158169 # Shell-channel fallback
159170 try :
160171 variables = _fallback_list_variables (kernel )
161- return {"variables" : variables , "source" : "fallback" }
172+ return {"variables" : variables , "source" : VariableSource . FALLBACK }
162173 except ValueError as e :
163174 raise VariablesUnavailable (str (e )) from e
164175 except Exception as e :
@@ -184,7 +195,7 @@ def inspect_variable(
184195 Returns:
185196 Dict with keys:
186197 - "name", "type", "value", "variables_reference"
187- - "source": "dap" or "fallback"
198+ - "source": VariableSource (DAP or FALLBACK)
188199 - "data", "metadata" (only when rich=True and DAP succeeds)
189200
190201 Raises:
@@ -206,7 +217,7 @@ def inspect_variable(
206217 }
207218 result ["data" ] = body .get ("data" , {})
208219 result ["metadata" ] = body .get ("metadata" , {})
209- result ["source" ] = "dap"
220+ result ["source" ] = VariableSource . DAP
210221 return result
211222 else :
212223 raw_all = _dap_inspect_variables (wsc , timeout = timeout )
@@ -218,7 +229,7 @@ def inspect_variable(
218229 f"Variable '{ name } ' not found in kernel namespace"
219230 )
220231 result = _normalise_dap_variable (match )
221- result ["source" ] = "dap"
232+ result ["source" ] = VariableSource . DAP
222233 return result
223234 except VariablesUnavailable :
224235 raise
@@ -233,7 +244,7 @@ def inspect_variable(
233244 raise VariablesUnavailable (
234245 f"Variable '{ name } ' not found in kernel namespace"
235246 )
236- match ["source" ] = "fallback"
247+ match ["source" ] = VariableSource . FALLBACK
237248 return match
238249 except VariablesUnavailable :
239250 raise
0 commit comments