Skip to content

Flaky test_collection.py::test_len #12275

Description

@crusaderky

This test fails on both pandas 2.3 and 3.0 CI with some frequency:

https://github.com/dask/dask/actions/runs/21682277456/job/62519785526?pr=12271

    def test_len(df, pdf):
        df2 = df[["x"]] + 1
        assert len(df2) == len(pdf)
    
        assert len(df[df.x > 5]) == len(pdf[pdf.x > 5])
    
        first = df2.partitions[0].compute()
>       assert len(df2.partitions[0]) == len(first)
E       AssertionError: assert 100 == 10
E        +  where 100 = len(Dask DataFrame Structure:\n                   x\nnpartitions=1       \n0              int64\n10               ...\nDask Name: partitions, 4 expressions\nExpr=Partitions(frame=df[['x']] + 1, partitions=[0]))
E        +  and   10 = len(    x\n0   1\n1   2\n2   3\n3   4\n4   5\n5   6\n6   7\n7   8\n8   9\n9  10)

dask/dataframe/dask_expr/tests/test_collection.py:1718: AssertionError

FAILED dask/dataframe/dask_expr/tests/test_collection.py::test_len - AssertionError: assert 100 == 10
 +  where 100 = len(Dask DataFrame Structure:\n                   x\nnpartitions=1       \n0              int64\n10               ...\nDask Name: partitions, 4 expressions\nExpr=Partitions(frame=df[['x']] + 1, partitions=[0]))
 +  and   10 = len(    x\n0   1\n1   2\n2   3\n3   4\n4   5\n5   6\n6   7\n7   8\n8   9\n9  10)

Activity

  1. added
    needs triage
    on Feb 4, 2026
  2. abslock128w commented on Feb 4, 2026

    @abslock128w
    Contributor

    i could take a look at this

  3. changed the title [-]Flaky[/-] [+]Flaky `test_collection.py::test_len`[/+] on Feb 5, 2026
  4. crusaderky commented on Feb 5, 2026

    @crusaderky
    CollaboratorAuthor

    @abslock128w there are no categoricals in this test.
    It fails sporadically on all versions of pandas (but I can't reproduce it locally).
    The above being said, feel free to open a PR if you believe you found the issue and know how to fix it.

  5. added
    tests
    and removed
    needs triage
    on Feb 5, 2026
  6. crusaderky commented on Feb 5, 2026

    @crusaderky
    CollaboratorAuthor

    @abslock128w did you mean to post on #12268?

  7. abslock128w commented on Feb 5, 2026

    @abslock128w
    Contributor

    @abslock128w did you mean to post on #12268?

    yeah my bad,
    will try to find the fix for this and create PR

  8. abslock128w commented on Feb 5, 2026

    @abslock128w
    Contributor

    @abslock128w there are no categoricals in this test. It fails sporadically on all versions of pandas (but I can't reproduce it locally). The above being said, feel free to open a PR if you believe you found the issue and know how to fix it.

    I looked thru the issue.. this looks like a true flaky test caused by a mismatch between Dask metadata
    and computed data.

    len(df2.partitions[0]) relies on partition metadata and does not trigger
    computation, while df2.partitions[0].compute() returns the actual pandas
    DataFrame. In some execution paths, the metadata overestimates the partition
    length (e.g. reporting 100 rows) while the computed result only contains 10
    rows.

    This explains the intermittent 100 == 10 failure. The test assumes that
    partition metadata length always matches the computed length, but this is not
    a guarantee Dask provides, especially after expression-based transformations
    like `df[["x"]] + 1`.

    I’m able to run the test locally and it passes, which is consistent with the
    non-deterministic nature of the failure observed in CI.

    will create a fix for this and check out if it still fails..

  9. crusaderky commented on Feb 5, 2026

    @crusaderky
    CollaboratorAuthor

    The test assumes that partition metadata length always matches the computed length, but this is not
    a guarantee Dask provides

    It definitely does provide this guarantee and many things would break if it didn't. metadata length must be either an integer that actually matches what will come out of the compute() or NaN/None/raise.

  10. abslock128w commented on Feb 5, 2026

    @abslock128w
    Contributor

    The test assumes that partition metadata length always matches the computed length, but this is not
    a guarantee Dask provides

    It definitely does provide this guarantee and many things would break if it didn't. metadata length must be either an integer that actually matches what will come out of the compute() or NaN/None/raise.

    I agree that if the partition metadata says a concrete length, then it really has to match the computed result, otherwise a lot of downstream logic will break.

    So this flakiness probably points more to a bug in how partition length metadata is inferred or passed around in dask_expr, especially after expression-based ops like df[["x"]] + 1, rather than a problem with the test itself.

    I’ll take a closer look at where the partition length is coming from in this case and try to figure out why it sometimes becomes wrong.

  11. abslock128w commented on Feb 13, 2026

    @abslock128w
    Contributor

    @crusaderky Could you take a look at this PR when you have time? It addresses the flaky test_len failure Thanks!

  12. crusaderky commented on May 14, 2026

    @crusaderky
    CollaboratorAuthor

    XFAIL'ed in #12396

    In an earlier iteration, I tried using @pytest.mark.repeat. It doesn't work: when it flakes, it flakes 10 times in a row.
    This makes me suspect that the issue may be caused by set ordering (which changes at every interpreter start).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    tests

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions