This rule raises an issue when a function-scoped test uses the tmp_path_factory fixture instead of the tmp_path fixture.

Why is this an issue?

tmp_path provides an isolated directory for one test. tmp_path_factory is for session, module, or class-scoped fixtures that share directories across tests.

Using the factory in a function-scoped test adds an unnecessary mktemp() call and obscures intent.

What is the potential impact?

Choosing the session-scoped factory in function-scoped tests raises the cost of onboarding developers to the fixture graph.

How to fix it

Replace tmp_path_factory with tmp_path in function-scoped tests.

Code examples

Noncompliant code example

def test_file_operation(tmp_path_factory):  # Noncompliant
    # Unnecessary factory for function scope
    temp_dir = tmp_path_factory.mktemp('data')
    test_file = temp_dir / 'test.txt'
    test_file.write_text('content')
    assert test_file.read_text() == 'content'

Compliant solution

def test_file_operation(tmp_path):
    # Direct tmp_path for function scope
    test_file = tmp_path / 'test.txt'
    test_file.write_text('content')
    assert test_file.read_text() == 'content'

Resources

Documentation