How do I specify this kind of variable argument tuple with python typing?

前端 未结 1 1827
野性不改
野性不改 2021-01-22 10:43

I\'m trying to do this, but I\'m not sure how to specify the type signature:

def initialize_signals(
        self,
        command: InitializeCommand,
        in         


        
相关标签:
1条回答
  • 2021-01-22 10:54

    there currently isn't an annotation which can represent the addition of a fixed-length tuple with a variable length tuple.

    here's some code I used to determine how mypy's inference would handle something like this:

    from typing import Tuple
    
    x: Tuple[int, ...]
    y = ('hi', *x)
    z = (*x,)
    reveal_type(y)
    reveal_type(z)
    

    and output:

    $ mypy t.py
    t.py:6: error: Revealed type is 'builtins.tuple[builtins.object*]'
    t.py:7: error: Revealed type is 'builtins.tuple[builtins.int*]'
    

    despite knowing that it's a variable-length int tuple it decays to object.

    You may want to refactor your code to use Tuple[SignalNode, Tuple[Any, ...]] instead

    0 讨论(0)
提交回复
热议问题