python / python/typing

TypeVarTuple with Callable and default arguments

Open
#1,231 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic: feature
Dominant language
Python
Stars
1.8k
Forks
302
Avg merge
23h
Merged PRs (30d)
8

Description

Previous discussion: https://github.com/microsoft/pyright/issues/3775

I've been playing around with TypeVarTuples recently, in particular together with Callable and args.
https://peps.python.org/pep-0646/#type-variable-tuples-with-callable

One issue became obvious early on which isn't defined in the PEP itself.

How should default arguments be handled?

A common pattern, especially in async code, is to pass a callable and it's args to a function

from typing import Callable
from typing_extensions import TypeVarTuple

Ts = TypeVarTuple("Ts")

def call_later(cb: Callable[[*Ts], None], *args: *Ts) -> None: ...

That works well if all arguments are required

def func1(x: int, y: int, z: int) -> None: ...
call_later(func1, 0, 0, 0)  # ok

However, what should happen if y and z have default arguments?

def func2(x: int, y: int = 0, z: int = 0) -> None: ...
call_later(func2, 0)

Instinctively, I would think that it should work, too. As @erictraut did point out though, there are at least a few cases where the behavior would need to be further specified / or explicitly forbidden.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the previous discussion in issue 3775 and the PEP 646 section on TypeVarTuples with Callable. Compare the required-argument and default-argument examples, along with the edge cases raised in the discussion, and establish a specified or explicitly forbidden behavior for default arguments.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.