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

Method process_value

src/click/core.py:2536–2600  ·  view source on GitHub ↗

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 """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 """
2549 # shelter `type_cast_value` from ever seeing an `UNSET` value by handling the
2550 # cases in which `UNSET` gets special treatment explicitly at this layer
2551 #
2552 # Refs:
2553 # 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 # Legacy case: UNSET is not exposed directly to the callback, but converted
2565 # to None.
2566 if value is UNSET:
2567 value = None
2568
2569 # 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 # No UNSET values, call the callback as usual.
2572 if not unset_keys:
2573 value = self.callback(ctx, self, value)
2574
2575 # Legacy case: provide a temporarily manipulated context to the callback
2576 # to hide UNSET values as None.
2577 #
2578 # Refs:
2579 # https://github.com/pallets/click/issues/3136
2580 # https://github.com/pallets/click/pull/3137
2581 else:
2582 # Add another layer to the context stack to clearly hint that the
2583 # context is temporarily modified.
2584 with ctx:
2585 # Update the context parameters to replace UNSET with None.
2586 ctx.params.update(unset_keys)
2587 # Feed these fake context parameters to the callback.
2588 value = self.callback(ctx, self, value)
2589 # 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