Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make click.Context generic over obj #2663

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/click/core.py
Expand Up @@ -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")

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand Down