Skip to content

trailing comma at end of a parametrized test's arguments list changes test semantics #13489

@MichaReiser

Description

@MichaReiser

Hi. I'm working on Ruff and we received a report that Ruff's formatter breaks pytests by adding a trailing comma at the end of a parametrized test's argument list (astral-sh/ruff#18437).

There test before formatting is:

class TestMonitoring:

    model_id = "test_model"
    run_date = datetime(2024, 5, 20)
    prediction_date = "2024-02-20"

    @pytest.mark.parametrize(
        "prod_run",
        [
            True,
            False,
        ]
    )
    def test_get_prediction_results(self, monkeypatch, spark, prod_run):
        database = "app_ard368_output" if prod_run else "discovery_ard368_output"

        prediction_df = spark.createDataFrame(TestDataMonitoring.prediction_data, TestDataMonitoring.prediction_schema)
        expected_df = spark.createDataFrame(TestDataMonitoring.prediction_data_specific_date, TestDataMonitoring.prediction_schema)

        monkeypatch.setattr(spark, "table", get_spark_tables({f"{database}.{self.model_id}_output": prediction_df}))

        monitoring = Monitoring(spark, self.model_id, self.run_date, prod_run)

        result_df = monitoring.get_prediction_results(self.prediction_date)

        assert_df_equality(result_df, expected_df, ignore_nullable=True)

Ruff's formatter then inserts a trailing comma after the [ True, False ] array. The formatted test is:

    ... 

    @pytest.mark.parametrize(
        "prod_run",
        [
            True,
            False,
        ],
    )
    def test_get_prediction_results(self, monkeypatch, spark, prod_run):
        ... # unchanged

Now, their test fails with FAILED tests/monitoring/test_unit_monitoring.py::TestMonitoring::test_get_prediction_results[True] - AttributeError: 'tuple' object has no attribute 'schema'.

Adding the trailing comma doesn't change semantics according to Python, but it seems to do so for Pytest. Is this intentional or a bug in pytest?

I tried to narrow this to a smaller repro but failed.

They use: pytest-8.2.2 pytest-metadata-3.1.1 pytest_html-4.1.1 pytest_mock-3.14.0 and run pytest with .venv/bin/python -m coverage run --source=src -m pytest -o log_cli=true --log-level=INFO --show-capture=log -W ignore:DeprecationWarning -vv

CC: @keita00 as you reported this issue with Ruff. What could help us repro this issue is if you could tell us for which object the .schema access fails. Is it the prod_run argument or is the access on the spark object? If so, how's the spark fixture defined?

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: needs informationreporter needs to provide more information; can be closed after 2 or more weeks of inactivity

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions