Type annotations for *args and **kwargs

后端 未结 4 1532
無奈伤痛
無奈伤痛 2020-12-04 09:05

I\'m trying out Python\'s type annotations with abstract base classes to write some interfaces. Is there a way to annotate the possible types of *args and

4条回答
  •  既然无缘
    2020-12-04 09:43

    As a short addition to the previous answer, if you're trying to use mypy on Python 2 files and need to use comments to add types instead of annotations, you need to prefix the types for args and kwargs with * and ** respectively:

    def foo(param, *args, **kwargs):
        # type: (bool, *str, **int) -> None
        pass
    

    This is treated by mypy as being the same as the below, Python 3.5 version of foo:

    def foo(param: bool, *args: str, **kwargs: int) -> None:
        pass
    

提交回复
热议问题