From 343db57459c13a7f513dcda29a61aaef805544d6 Mon Sep 17 00:00:00 2001 From: Viicos <65306057+Viicos@users.noreply.github.com> Date: Sat, 6 Jan 2024 13:12:48 +0100 Subject: [PATCH] Make `click.Context` generic over `obj` --- src/click/core.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/click/core.py b/src/click/core.py index 336413950..ae0bee7be 100644 --- a/src/click/core.py +++ b/src/click/core.py @@ -42,8 +42,14 @@ from .utils import PacifyFlushWrapper if t.TYPE_CHECKING: + import typing_extensions as te + from .shell_completion import CompletionItem + ObjT = te.TypeVar("ObjT", default=t.Any) +else: + ObjT = t.TypeVar("ObjT") + F = t.TypeVar("F", bound="t.Callable[..., t.Any]") V = t.TypeVar("V") @@ -156,7 +162,7 @@ class ParameterSource(enum.Enum): """Used a prompt to confirm a default or provide a value.""" -class Context: +class Context(t.Generic[ObjT]): """The context is a special internal object that holds state relevant for the script execution at every single level. It's normally invisible to commands unless they opt-in to getting access to it. @@ -265,7 +271,7 @@ def __init__( command: Command, parent: Context | None = None, info_name: str | None = None, - obj: t.Any | None = None, + obj: ObjT | None = None, auto_envvar_prefix: str | None = None, default_map: cabc.MutableMapping[str, t.Any] | None = None, terminal_width: int | None = None, @@ -302,7 +308,7 @@ def __init__( obj = parent.obj #: the user object stored. - self.obj: t.Any = obj + self.obj: ObjT | None = obj self._meta: dict[str, t.Any] = getattr(parent, "meta", {}) #: A dictionary (-like object) with defaults for parameters.