Antony D
Antony D

Reputation: 75

Error with data type of List in a list with str and int

I am having problems with the type of data of a function I am writing. The data being inputted is a list with a list of str and int. It looks like this:

list_example = [['a', 1],['b', 2],['c', 3]]

The function looks like this:

def inputting_incrementing(club: str, list: List[List[str, int]]) -> None:

The error is this:

TypeError: Parameters to generic types must be types. Got [class 'str', class 'int']

Anyone can shed some light on this problem? Anything helps.

Upvotes: 1

Views: 3120

Answers (2)

Anteino
Anteino

Reputation: 1136

I believe the proper way to create a tuple is by placing all the elements between parentheses. If you want to create a list of tuples you should do it like this:

[('a', 1), ('b', 1), ('c', 1)]

Upvotes: 0

ShadowRanger
ShadowRanger

Reputation: 155418

I can't reproduce your error. The error you get is what you'd get if you'd entered:

List[[str, int]]

Did you perhaps name a global variable List or Tuple in such a way that "indexing" it would result in code that evaluated to the above? I suggest this because you're clearly not being careful about variable names (you named the parameter list, shadowing the list constructor for the body of that function).

I suggest adding a print(List, Tuple) to ensure they're still the types from the typing module, and making sure you haven't accidentally doubled the brackets somewhere, as in my example above.

I'll also note that even if this works, the annotation doesn't match the input you're providing; you're providing a list of lists, not a list of tuples.

Upvotes: 1

Related Questions