Tuple declaration using square brackets

I sometimes see this Tuple declaration which is not common:

VARIABLE = Tuple[List[Tuple[str, float, Dict[str, Any]]], Dict[str, str]]

VAR = Tuple[…]
I was expecting Tuple with parentheses. Any help to understand?

Thank you!

Not too sure what you mean, but…

Tuple1 = ()  # this is a empty tuble object
Tuple2 = []  # this is a empty list object

… I’m simply making the point that naming an object, does not define it.

Thank you Rob for your response.

The “Tuple” is a type very similar to standard “tuple”, but this one is imported from typing and I am not able to understand it, as you can see below syntax:

VARIABLE = Tuple[ List[Tuple[str, float, Dict[str, Any]]], Dict[str, str] ]

I am not sure if my question is clear.

You’re welcome. Sorry I couldn’t be of help, but I’m sure someone here will be.

1 Like

This looks like a type docstring (documentation string embedded as comment in a program file).

Have you actually seen this as statements in runnable code, @rhafedh ?

1 Like

Is this upper-case Tuple object being imported from the typing module? If so, it’s a type intended to be used for type annotations (aka type hints), not to affect runtime code. See the docs on it here.

In this case it looks like it’s not being used directly in an annotation, but in a runtime assignment to create a “type alias” like VARIABLE or VAR.

6 Likes

Yes, it looks as a custom type:

First the types are declared as following in a separate .py file:

types.py

from typing import TypeVar, TYPE_CHECKING, Tuple

DataVar = Tuple[List[Tuple[str, float, Dict[str, Any]]], Dict[str, str]]

toolUpdate.py

class Updater(Generic[DataVar]):
def init(self: ‘Updater[CCT, UD, CD, BD]’): …


I hope this helps.

I think you nailed it, I will go through the shared link, thank you A. Reich for the explanation!