PyCharm: namedtuple: unexpected argument, unfilled parameter

435 Views Asked by At

In PyCharm, you can declare a named tuple.

from collections import namedtuple

InstTyp = namedtuple(
    typename='InstTyp',
    field_names='''
        instance_type
        memory
        num_cpus    
    '''
)

Code that uses the named tuple runs without error.

it = InstTyp(
    instance_type='tx',
    memory=64,
    num_cpus=8
)

However, PyCharm raises "Unexpected argument" and "unfilled parameter" inspection warnings.

1

There are 1 best solutions below

0
Brian Fitzgerald On

The field_names are a sequence of strings such as ['x', 'y']. Alternatively, field_names can be a single string with each fieldname separated by whitespace and/or commas, for example 'x y' or 'x, y'.

The python interpreter handles all whitespace, so the code runs. PyCharm does not handle newlines in whitespace, so PyCharm displays inspection warnings.

There are two solutions.

  1. Split field_names into an array:

     InstTyp = namedtuple(
         typename='InstTyp',
         field_names='''
             instance_type
             memory
             num_cpus    
         '''.split()
     )
    
  2. Put field_names on a single line.

     InstTyp = namedtuple(
         typename='InstTyp',
         field_names='instance_type memory num_cpus'
     )
    

The PyCharm inspection warnings are gone.