MCPcopy
hub / github.com/pallets/werkzeug / __init__

Method __init__

src/werkzeug/local.py:478–532  ·  view source on GitHub ↗
(
        self,
        local: ContextVar[T] | Local | LocalStack[T] | t.Callable[[], T],
        name: str | None = None,
        *,
        unbound_message: str | None = None,
    )

Source from the content-addressed store, hash-verified

476 """
477
478 def __init__(
479 self,
480 local: ContextVar[T] | Local | LocalStack[T] | t.Callable[[], T],
481 name: str | None = None,
482 *,
483 unbound_message: str | None = None,
484 ) -> None:
485 if name is None:
486 get_name = _identity
487 else:
488 get_name = attrgetter(name) # type: ignore[assignment]
489
490 if unbound_message is None:
491 unbound_message = "object is not bound"
492
493 if isinstance(local, Local):
494 if name is None:
495 raise TypeError("'name' is required when proxying a 'Local' object.")
496
497 def _get_current_object() -> T:
498 try:
499 return get_name(local) # type: ignore[return-value]
500 except AttributeError:
501 raise RuntimeError(unbound_message) from None
502
503 elif isinstance(local, LocalStack):
504
505 def _get_current_object() -> T:
506 obj = local.top
507
508 if obj is None:
509 raise RuntimeError(unbound_message)
510
511 return get_name(obj)
512
513 elif isinstance(local, ContextVar):
514
515 def _get_current_object() -> T:
516 try:
517 obj = local.get()
518 except LookupError:
519 raise RuntimeError(unbound_message) from None
520
521 return get_name(obj)
522
523 elif callable(local):
524
525 def _get_current_object() -> T:
526 return get_name(local())
527
528 else:
529 raise TypeError(f"Don't know how to proxy '{type(local)}'.")
530
531 object.__setattr__(self, "_LocalProxy__wrapped", local)
532 object.__setattr__(self, "_get_current_object", _get_current_object)
533
534 __doc__ = _ProxyLookup(
535 class_value=__doc__, fallback=lambda self: type(self).__doc__, is_attr=True

Callers

nothing calls this directly

Calls 1

__setattr__Method · 0.45

Tested by

no test coverage detected