5

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

def initialize_signals(
        self,
        command: InitializeCommand,
        initializers: Iterable[Union[
            Tuple[SignalNode],
            Tuple[SignalNode, Any, ...]
                  ]]):
    for x, *args in initializers:
        potential_update = command.create_potential_update(x, *args)

1 Answer 1

6

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I know that the refactoring you suggest is more logical. However, this code is called so much that it feels painful to add (,) all over the place. It does seem that this feature could theoretically (and without breaking anything) be added to python typing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.