3

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)

Benji
  • 549
  • 7
  • 22

2 Answers2

2

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')
Guy
  • 46,488
  • 10
  • 44
  • 88
1

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?

Robert Kossendey
  • 6,733
  • 2
  • 12
  • 42