MCPcopy
hub / github.com/pallets/click / process_value

Method process_value

src/click/core.py:2536–2600  ·  src/click/core.py::Parameter.process_value

Process the value of this parameter: 1. Type cast the value using :meth:`type_cast_value`. 2. Check if the value is missing (see: :meth:`value_is_missing`), and raise :exc:`MissingParameter` if it is required. 3. If a :attr:`callback` is set, call it to have the v

(self, ctx: Context, value: t.Any)

Source from the content-addressed store, hash-verified

2534 return False
2535
2536 def process_value(self, ctx: Context, value: t.Any) -> t.Any:
2537 class="st">"""Process the value of this parameter:
2538
2539 1. Type cast the value using :meth:`type_cast_value`.
2540 2. Check if the value is missing (see: :meth:`value_is_missing`), and raise
2541 :exc:`MissingParameter` if it is required.
2542 3. If a :attr:`callback` is set, call it to have the value replaced by the
2543 result of the callback. If the value was not set, the callback receive
2544 ``None``. This keep the legacy behavior as it was before the introduction of
2545 the :attr:`UNSET` sentinel.
2546
2547 :meta private:
2548 class="st">"""
2549 class="cm"># shelter `type_cast_value` from ever seeing an `UNSET` value by handling the
2550 class="cm"># cases in which `UNSET` gets special treatment explicitly at this layer
2551 class="cm">#
2552 class="cm"># Refs:
2553 class="cm"># https://github.com/pallets/click/issues/3069
2554 if value is UNSET:
2555 if self.multiple or self.nargs == -1:
2556 value = ()
2557 else:
2558 value = self.type_cast_value(ctx, value)
2559
2560 if self.required and self.value_is_missing(value):
2561 raise MissingParameter(ctx=ctx, param=self)
2562
2563 if self.callback is not None:
2564 class="cm"># Legacy case: UNSET is not exposed directly to the callback, but converted
2565 class="cm"># to None.
2566 if value is UNSET:
2567 value = None
2568
2569 class="cm"># Search for parameters with UNSET values in the context.
2570 unset_keys = {k: None for k, v in ctx.params.items() if v is UNSET}
2571 class="cm"># No UNSET values, call the callback as usual.
2572 if not unset_keys:
2573 value = self.callback(ctx, self, value)
2574
2575 class="cm"># Legacy case: provide a temporarily manipulated context to the callback
2576 class="cm"># to hide UNSET values as None.
2577 class="cm">#
2578 class="cm"># Refs:
2579 class="cm"># https://github.com/pallets/click/issues/3136
2580 class="cm"># https://github.com/pallets/click/pull/3137
2581 else:
2582 class="cm"># Add another layer to the context stack to clearly hint that the
2583 class="cm"># context is temporarily modified.
2584 with ctx:
2585 class="cm"># Update the context parameters to replace UNSET with None.
2586 ctx.params.update(unset_keys)
2587 class="cm"># Feed these fake context parameters to the callback.
2588 value = self.callback(ctx, self, value)
2589 class="cm"># Restore the UNSET values in the context parameters.
2590 ctx.params.update(
2591 {
2592 k: UNSET
2593 for k in unset_keys

Callers 1

handle_parse_resultMethod · 0.95

Calls 4

type_cast_valueMethod · 0.95
value_is_missingMethod · 0.95
MissingParameterClass · 0.85
updateMethod · 0.80

Tested by

no test coverage detected