Fanatique
Fanatique

Reputation: 428

Getting missing required positional arguments if passing *args with None values to a namedtuple object

In the following case, when I pass the tuple *values to Query() I get the following error:

TypeError: __new__() missing 9 required positional arguments: 'col1', 'col2', 'col3', 'col4', 'col7', 'col9', 'col11', 'col12', and 'col14'
def map_value_to_column(values: tuple, columns: list) -> namedtuple:
    Query = namedtuple("Query", columns)
    return Query(*values)

Obviously this is happening because some of the values are None. The problem here is that values is an immutable tuple and I don't want to copy all of the values to a list so I can map None to something else and I haven't found any other way to avoid this error. Is there a way I can make Query(*values) work in this case even if I have None values?

Note: I'm absolutely sure that the amount of values equals the amount of columns. Only some of the values can be None.

Upvotes: 2

Views: 467

Answers (1)

Fanatique
Fanatique

Reputation: 428

The problem was in another part of my code.

I was passing a list of tuples instead of a single tuple to the function map_value_to_column.

I just needed to build a list and convert each tuple to a namedtuple inside:

def map_value_to_column(values: list, columns: list) -> namedtuple:
    Query = namedtuple("Query", columns)
    queries = []
    for value in values:
        queries.append(Query(*value))
    return queries

There is no problem with supplying None type arguments to namedtuple.

Upvotes: 1

Related Questions