Search code examples
pythonpycharmpytest

Pytest parametrized indirect input arguments types: list vs tuple issue


I am writing a pytest unit test with a parametrized fixture using indirect to instantiate Design objects via a classmethod called 'get_design2'. There are three string arguments that I need to pass in for each test, so I was using a list of strings which worked like a champ. The return of get_design is a tuple with a string (input item 1) as the first item and an instance of Design (created from input items 2 and 3) as the second item.

If I change the parametrize lists to tuples as shown below (which makes more sense because each set of input parameters is static), then Pycharm complains that the second item returned from get_design is a string instead of an instance of Design. The test still runs without a problem.

Any idea why it is complaining about the type mismatch? I wouldn't think there would be any difference in passing the arguments as lists vs tuples because they are being consumed exactly the same way.

@pytest.fixture
def get_design2(request) -> tuple[str, Design]:
    """Return a single instantiated class that can be used for one or many tests."""
    print(f"Instantiating Design object for {request.param[0]}")
    this_design = Design.init_with_paths(request.param[1], request.param[2], request.param[0])
    return request.param[0], this_design


@pytest.mark.parametrize('get_design2',
                         [('foo', '', ''),
                          ('bar', '', ''),
                          ('baz', '', '')],
                         indirect=True)
def test_all_designs(get_design2):
    design_name = get_design2[0]
    this_design = get_design2[1]
    assert this_design.product_name == design_name. # Unresolved attribute reference 'product_name' for class 'str'

See screenshots of before/after code (showing IDE warning) linked below...

parametrize args as list

parametrize args as tuple with warning


Solution

  • Your code is OK. It is a PyCharm bug. IDE cannot handle indirect fixture parametrization type inference, see the relevant ticket in PyCharm bug tracker https://youtrack.jetbrains.com/issue/PY-36334 (and vote for it).