Skip to content

Collections: recursive case normalization can make dictionary keys unhashable #5861

Open

Description

@anticipate218

This is the separate container-normalization follow-up requested in #5858. I confirmed that #5859 fixes the original nested-key comparison. The concern is a change in observable comparison results, including an exception when comparing otherwise valid dictionaries.

Verified with Robot Framework 7.5.2.dev1 on Python 3.13.9, Windows 11 (10.0.26200), comparing master commit 429888ef4cba8e695aa033daefc46695a3f8da9f with its predecessor 8e6a23571ee22b8156859067ea4efd75ce93a06e.

VALUES mode turns unchanged hashable keys into lists

from collections import namedtuple
from robot.libraries.Collections import Collections

Pair = namedtuple("Pair", "first second")
key = Pair(1, 2)
Collections().dictionaries_should_be_equal(
    {key: "UPPER"}, {key: "upper"}, ignore_case="VALUES"
)

This passes at 8e6a235 and raises TypeError: unhashable type: 'list' at 429888ef. Replacing key with range(2) has the same result. Even comparing {key: "same"} with itself raises this error with ignore_case="VALUES". An ordinary (1, 2) tuple key still works.

Expected: only value case is ignored, and valid keys remain usable.

KEYS mode can also make unequal values compare equal

from robot.libraries.Collections import Collections

Collections().dictionaries_should_be_equal(
    {"item": range(2)}, {"item": [0, 1]}, ignore_case="KEYS"
)

The earlier commit raises the expected comparison assertion; the new commit passes. The keys are already identical, and no string value needs normalization. A Pair(1, 2) value compared with [1, 2] behaves the same way. In contrast, an ordinary (1, 2) tuple value remains distinct from [1, 2].

Cause and verification

The new recursion reaches normalize_list() for keys in VALUES mode and values in KEYS mode. _try_to_preserve_type() calls cls(value_list); Pair([1, 2]) and range([0, 1]) raise TypeError, so the fallback returns the list. For keys, the subsequent dictionary lookup fails; for values, both sides can become lists and compare equal.

I reproduced these differences both with the native Python Collections API and through the actual Robot runner. The API matrix and focused seven-case Robot suite also confirm that nested-key recursion is fixed, value case remains significant in KEYS mode, and ordinary tuple behavior is preserved. This is focused validation, not a full repository test run.

One possible direction is to separate traversing containers for nested-key normalization from reconstructing containers whose own contents did not change. A blanket early return for KEYS values would reintroduce #5858, and returning an already-consumed generator is unsafe. I do not yet have a fully validated combined fix.

AI assistance was used for investigation, reproduction, and this report.

Activity

  1. pekkaklarck commented on Nov 19, 2026

    @pekkaklarck
    Member

    Normalizing nested values in generic manner is a pretty hard problem. It can be handled pretty easily with lists and dicts, but other containers are trickier. Converting the normalized value back to the original type like type(orig)(normalized) works in simple cases, but there are various objects that don't support it.

    We could special case some objects like namedtuple (PR #5862 does that), but that's not a generic solution. With generators like range there's also a problem that iterating over values consumes the original value.

    I'm honestly not sure what we should do. Options include:

    1. Socument the behavior and instructing users not to normalize values if they use other types than dicts, lists and tuples. Such objects ought to be rare and users can create custom keywords for custom normalization needs.
    2. Fail if preserving the original type fails.
    3. Add an option to explicitly normalize container types to lists and dicts.
    4. Special case namedtuple etc.
    5. A combination of the above.
  2. anticipate218 commented on Nov 20, 2026

    @anticipate218
    Author

    Thanks for laying out the options. As a data point, I re-ran the seven cases from my report (as a small Robot suite using Collections) against #5862's head 95fdba3 on Python 3.13 / Windows: all seven pass. That covers the VALUES-mode namedtuple and range keys, the KEYS-mode range/namedtuple vs. list values, ordinary tuples staying distinct from lists, and the nested KEYS comparison from #5858. This is a focused check, not a full acceptance test run.

    On the options: the regression I reported only involves containers whose items don't change during normalization (unchanged keys in VALUES mode, unchanged values in KEYS mode), and before #5859 those were left as they were. Returning such sequences unchanged, as #5862 does, restores that behavior without needing a generic way to rebuild arbitrary types, and it doesn't try to return already-consumed iterators. For containers whose items really do change and can't be rebuilt, documenting that they may be compared as lists (option 1) seems less disruptive than failing (option 2), but I don't have a strong opinion there.

    AI assistance was used for this check and reply.

  3. pekkaklarck commented on Nov 20, 2026

    @pekkaklarck
    Member

    I only now realized how the fix for #5858 causes regressions. These certainly need to be resolved somehow while still preserving the recursive functionality added in #5858.

    I need to think about the alternatives, but before that we need to decide do we need RF 7.5.2 or not. If we want to create such a bug fix release, these issues either need to be resolved ASAP or we need to cut the release from a commit before the fix for #5858. If the next release is RF 7.6, then there's more time to think about this as well.

  4. pekkaklarck commented on Nov 20, 2026

    @pekkaklarck
    Member

    I looked at the PR #5862 again and start to like it. We may still want to discuss how to handle range objects. As I commented the PR, I believe we should not touch them at all.

    Does the test set you @anticipate218 mentioned contain tests that aren't covered by the tests added in #5862? If yes, can you create a separate PR adding them after #5862 is merged?

  5. anticipate218 commented on Nov 20, 2026

    @anticipate218
    Author

    Yes. Comparing the seven cases with #5862 at 95fdba3, the missing regression is a namedtuple used as a dictionary value versus an equivalent list with ignore_case=KEYS. The new range-value test does not exercise that namedtuple path.

    The namedtuple/range key cases and the range-value versus list case are already in #5862; the recursive KEYS case is already in the test file. My ordinary-tuple/list and VALUES-key-case checks were controls, so I will avoid duplicating coverage unnecessarily.

    After #5862 is merged, I will recheck its final tests and send a separate, focused acceptance-test PR for the remaining namedtuple-value case. That can stay independent of the decision about handling ranges.

    AI assistance was used for this source/test comparison.

  6. added a commit that references this issue on Nov 23, 2026
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