Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python type hint Callable with one known positional type and then *args and **kwargs

I the below function foo, which has:

  • One positional arg with a known type
  • A variable number of positional and keyword args after that
from typing import Callable

def foo(bar: str, *args, **kwargs) -> None:
    """Some function with one positional arg and then *args and **kwargs."""

foo_: Callable[[str, ...], None] = foo  # error: Unexpected '...'

How can I type hint this?

Currently, mypy==0.812 throws the error: error: Unexpected '...' [misc]

like image 472
Intrastellar Explorer Avatar asked Oct 16 '25 16:10

Intrastellar Explorer


1 Answers

I'd probably use Protocols for this. They're generally a bit more flexible than Callables. It would look something like this

from typing import Protocol

class BarFunc(Protocol):
    def __call__(fakeself, bar: str, *args, **kwargs) -> None:
        # fakeself gets swallowed by the class method binding logic
        # so this will match functions that have bar and the free arguments.
        ...

def foo(bar: str, *args, **kwargs) -> None:
    """Some function with one positional arg and then *args and **kwargs."""

foo_: BarFunc = foo
like image 100
Josiah Avatar answered Oct 18 '25 11:10

Josiah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!