I didn't understand the error message so I decided to figure it out. Some things I knew.
This creates a list by calling a function named list().
x = list('6048922', '334318')
We know it is a function call because it is a name followed by parenthesis.
This returns the first value from a list named "list":
x = list[0]
We know this does indexing because it is a name followed by square brackets and there is an integer (or int variable) in the brackets. As an aside, never use built-in function names as variable names.
This returns a slice that is the first three items in "list":
x = list[:3]
We know this is a slice because it looks like an index but there is a ":" and an integer between the brackets
This returns a Type Alias. A relatively new Python type that is used for type annotation:
x = list[int]
We know this is a Type Alias for a list of int because it is a sequence type name (list) followed by square brackets that contain a value type name (int).
From the docs:
https://docs.python.org/3/library/typing.html
Quote:A type alias is defined by assigning the type to the alias. In this example, Vector and list[float] will be treated as interchangeable synonyms:
Vector = list[float]
def scale(scalar: float, vector: Vector) -> Vector:
return [scalar * num for num in vector]
# typechecks; a list of floats qualifies as a Vector.
new_vector = scale(2.0, [1.0, -4.2, 5.4])
x = list['6048922', '334318'] cannot be indexing and it cannot be a slice, but theoretically it could be a type alias, so that is how Python evaluates this code. I did not know that was what your code was doing until I rewrote the example like this:
x = list['6048922', '334318']
print(x, type(x))
Output:
list['6048922', '334318'] <class 'types.GenericAlias'>
I don't think this is a valid GenericAlias, but we don't find that out until we try to use it. The "error" is not identified until you try to get the value types. From the documents:
https://docs.python.org/3/library/stdtyp...nericalias
Quote:The __getitem__() method of generics will raise an exception to disallow mistakes like dict[str][str]:
__getitem__() is what gets called when you do indexing. I am not sure exactly what causes the error because even a valid genericalias like x = list[int] throws an error when I try to index.
Output:
There are no type variables left in list[int]
I found some mention of this in a discussion of TypeVar, but it is still beyond me. I would appreciate any information on how __getitem__ would be used with a generic alias.