Skip to content

sort() of a range() that does not land exactly on stop returns wrong values #3228

Closed

Description

@Osamaali313

🐛 Bug

sort() applied to a range() sweep returns the wrong elements whenever the range does not land exactly on stop (i.e. when the last element != stop - step).

_sort_sweep rebuilds the reversed range by deriving the new endpoints from sweep.stop, but stop is exclusive and frequently does not coincide with the last element, so the rebuilt range has wrong values (and sometimes a wrong element count).

To reproduce

from hydra._internal.grammar.grammar_functions import sort, range as r

list(sort(r(0, 5, 2), reverse=True).range())   # -> [3, 1, -1]   expected [4, 2, 0]
list(sort(r(4, -1, -2)).range())               # -> [1, 3, 5]     expected [0, 2, 4]
list(sort(r(7, 0, -3)).range())                # -> [3, 6, 9]     expected [1, 4, 7]

Expected behavior

sort() should return the original range's elements in sorted order, e.g. sort(range(0, 5, 2), reverse=True) should yield [4, 2, 0].

Notes

The existing tests only cover ranges that land exactly on stop (range(1, 10), range(1, 10, 3), range(0, 2, 0.5)), where the old formula coincidentally produces the correct answer — which is why this went unnoticed.

System information

  • Hydra version: 1.4.0.dev (main)

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions