Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
697 views
in Technique[技术] by (71.8m points)

python - How do I annotate a callable with *args and **kwargs?

I have a function which returns a function. I would like to find a proper type annotation. However, the returned function has *args and *kwargs. How is that annotated within Callable[[Parameters???], ReturnType]?

Example:

from typing import Callable
import io
import pandas as pd

def get_conversion_function(file_type: str) -> Callable[[io.BytesIO, TODO], pd.DataFrame]:
    def to_csv(bytes_, *args, **kwargs):
        return pd.read_csv(bytes_, **kwargs)
    if file_type == "csv":
        return to_csv
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

With the introduction of typing.Protocol, MyPy (and other compliant type checkers) support the full call syntax natively via the __call__ special method.

MyPy: Callback Protocols

Protocols can be used to define flexible callback types that are hard (or even impossible) to express using the Callable[...] syntax, such as variadic, overloaded, and complex generic callbacks. (...)

Simply define a Protocol whose __call__ method has the desired signature.

from typing import Protocol

class SomeCallable(Protocol):
      def __call__(self, a: str, b: int, *args: float, kwargs: str) -> bytes: ...

def my_callable(a: str, b: int, *args: float, kwargs: str) -> bytes:
    return b"Hello World"

def my_func(key: str) -> SomeCallable:
    return my_callable

Note that the Protocol's __call__ must include a self parameter. This parameter is ignored when comparing other signatures against the Protocol.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...