Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

209
Visualizações
Type hint for Callable that takes kwargs

I want to do something like

from typing import Callable


def a(foo: Callable[[int], None]):
    foo(b=5)

This code works, but gives a warning Unexpected argument.

Defining as

def a(foo: Callable[[int], None]):
    foo(5)

works with no warnings as expected.


How can I pass in an expected argument as a kwarg into a function with the type checker not being angry at me?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

The Callable docs say

There is no syntax to indicate optional or keyword arguments; such function types are rarely used as callback types.

However they also say

Callable[..., ReturnType] (literal ellipsis) can be used to type hint a callable taking any number of arguments and returning ReturnType

Applying here, that would be

def a(foo: Callable[..., None]):

You will lose the int annotation, but it's either that, living with the warning or explicitly surpassing it.

over 4 years ago · Santiago Trujillo Relatório

0

You can use a "callable protocol" here, so:

import typing

class MyCallableType(typing.Protocol):
    def __call__(self, bar:int) -> None:
        ...

def a(foo: MyCallableType):
    foo(32)
    foo(bar=32)

Now, testing the above with mypy:

jarrivillaga$ mypy --version
mypy 0.910
jarrivillaga$ mypy test.py
Success: no issues found in 1 source file

Note, this allows mypy to catch all kinds of errors, e.g. a function with the wrong argument name, or if we want b to be a function that specifies a keyword-only bar argument:

import typing

class MyCallableType(typing.Protocol):
    def __call__(self, b:int) -> None:
        ...

def a(foo: MyCallableType):
    foo(32)
    foo(b=32)

def bar(b: int) -> None:
    pass

def baz(*, b: int) -> None:
    pass

def bing(x: int) -> None:
    pass

a(bar)
a(baz)
a(bing)

And mypy will complain with the following:

jarrivillaga$ mypy test.py
test.py:21: error: Argument 1 to "a" has incompatible type "Callable[[NamedArg(int, 'b')], None]"; expected "MyCallableType"
test.py:22: error: Argument 1 to "a" has incompatible type "Callable[[int], None]"; expected "MyCallableType"
Found 2 errors in 1 file (checked 1 source file)
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda