Get the current value for the given context type. See the full :ref:`Use Context` docs for more information.
(context: Context[_Type])
| 229 | |
| 230 | |
| 231 | def use_context(context: Context[_Type]) -> _Type: |
| 232 | """Get the current value for the given context type. |
| 233 | |
| 234 | See the full :ref:`Use Context` docs for more information. |
| 235 | """ |
| 236 | hook = current_hook() |
| 237 | provider = hook.get_context_provider(context) |
| 238 | |
| 239 | if provider is None: |
| 240 | # same assertions but with normal exceptions |
| 241 | if not isinstance(context, FunctionType): |
| 242 | raise TypeError(f"{context} is not a Context") # nocov |
| 243 | if context.__kwdefaults__ is None: |
| 244 | raise TypeError(f"{context} has no 'value' kwarg") # nocov |
| 245 | if "value" not in context.__kwdefaults__: |
| 246 | raise TypeError(f"{context} has no 'value' kwarg") # nocov |
| 247 | return cast(_Type, context.__kwdefaults__["value"]) |
| 248 | |
| 249 | return provider.value |
| 250 | |
| 251 | |
| 252 | # backend implementations should establish this context at the root of an app |
no test coverage detected