Skip to content

Fix Context.command.params type hinting for better IDE support #1257

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

ashupal86
Copy link

Fix IDE autocompletion for typer.Context

Fixes: #346

What was broken

When using typer.Context in callbacks, IDEs couldn't autocomplete parameter attributes:

def my_callback(ctx: typer.Context, param, value):
    params = ctx.command.params
    param.name  # ❌ No autocompletion

What's fixed

Now IDEs provide full autocompletion:

def my_callback(ctx: typer.Context, param, value):
    params = ctx.command.params
    param.name  # ✅ Full autocompletion!

How it works

Added proper type hints to the Context class:

# New in typer/models.py
class TypedCommand(click.Command):
    params: List[click.Parameter]

class Context(click.Context):
    command: TypedCommand

Files changed

  • typer/models.py - Added type hints
  • tests/test_ctx_params_typing.py - Added tests

ashupal86 and others added 2 commits July 20, 2025 21:12
- Add TypedCommand class with properly typed params: List[click.Parameter]
- Enhance Context class with improved type hints for command attribute
- Fixes GitHub issue fastapi#346: No autocompletion for typer.Context
- Add comprehensive tests for type hinting improvements
- Maintain full backward compatibility

Benefits:
- IDE autocompletion for ctx.command.params
- Type safety with mypy/type checkers
- Better developer experience with proper type annotations
- Safer refactoring with type-aware tools

Resolves: fastapi#346
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant