Skip to content

Make click.Context generic for obj #2493

@Viicos

Description

@Viicos

To add some type safety to the obj attribute of the Context class, I was thinking maybe it could be made generic with respect to obj attribute. It could then be used this way (with a TypedDict but it could be any user defined object):

class ContextObj(TypedDict):
    attr: int

def subcommand(
    ctx: click.Context[ContextObj],
    ...
) -> None:
    reveal_type(ctx.obj["attr"])  # Revealed type is "int"

That might be a bit cumbersome, so I'd understand if this is rejected (but I'm open to alternatives). Otherwise, I will be happy to implement this

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions