简体   繁体   中英

Test fails in Foundry when using asterisk (*) for unpacking when creating a dataframe

I want to create a DataFrame in a fixture using the following code:

@pytest.fixture
def my_fun(spark_session):
    return spark_session.createDataFrame(
        [
            (*['test', 'testy'])
        ],
        T.StructType([
            T.StructField('mytest', T.StringType()),
            T.StructField('mytest2', T.StringType()
        ])
    )
 
def test_something(my_fun):
    return

However, this fails with the following error:

TypeError: StructType can not accept object 'test' in type <class 'str'>

If I use ('test', 'testy') instead of (*['test', 'testy']) , it works. But shouldn't this be synonymous?

(I'm using Python 3.8.13, pytest-7.0.1)

They are not the same. The round brackets in your example are not a tuple , they are just round brackets around a list . To make it a tuple you need to add a comma

test = (*['test1', 'test2'],)
# ('test1', 'test2')

You are missing a comma.

If I run this basic example:

test = (*['test1', 'test2'])

It fails with:

File "file0.py", line 5
  test = (*['test1', 'test2'])
            ^
SyntaxError: can't use starred expression here

Try the following:

(*['test1', 'test2'],)

On another note: Is there a reason you want to spread the list instead of directly using a tuple?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM