Skip to content

Enhanced and explicit bytes support in BuiltIn, String and Collections #5548

Closed

Description

@pekkaklarck

Various validation keywords in BuiltIn currently work with bytes, but that support is not explicitly documented or tested. There's generally no bytes compatibility code, but bytes just has lot of the same methods as str:

>>> b'FOO'.lower().startswith(b'f')
True

Now that we are adding typing to BuiltIn (#5544), this bytes compatibility will be broken if we type arguments as str. That would be bad, because it is likely that these keywords are used with bytes even though the bytes support isn't advertised. A good example is #5054 that added bytes related enhancement to Should Contain. The problem described in this issue wouldn't occur with Should Contain, though, because it accepts also other values than strings (and bytes) and will likely be typed as object. Keywords like Should Start With that only make sense with strings (and bytes) are in bigger risk.

This is what we need to do:

  1. Make sure keywords that work with bytes get typing str | bytes instead of just str. This preserves the current functionality.
  2. Add tests. This makes sure current functionality isn't accidentally broken later.
  3. Fix possible API compatibility issues between bytes and str when feasible. For example, case-insensitivity is implemented with str.casefold, but bytes don't have that method and bytes.lower should be used instead. This particular problem is easy to fix with a helper method, in others we may just document that certain feature doesn't work with bytes.
  4. Consider enhancing bytes support when feasible. For example, Should Start With currently works if both the validated value and the expected start value are bytes, but validating bytes so that the expected value is str fails with a TypeError. The aforementioned Better support for bytes with Should Contain #5054 enhanced Should Contain in this regard and doing same enhancement with other bytes compatible keywords would be nice.
  5. Update documentation. Using str | bytes type hint already documents that bytes can be used, but adding some documentation about converting expected values could still be good. Concrete enhancements also require "New in RF 7.4." note.

Activity

  1. added this to the v7.4 milestone on Nov 17, 2025
  2. self-assigned this
    on Nov 17, 2025
  3. pekkaklarck commented on Nov 17, 2025

    @pekkaklarck
    MemberAuthor

    It probably makes sense to first add typing to BuiltIn (#5544) and afterwards implement this. The commit adding typing will be big even without these changes. We can use str as the type initially, but using str | bytes would be a bit better.

  4. pekkaklarck commented on Nov 20, 2025

    @pekkaklarck
    MemberAuthor

    Currently all keywords do case-normalization and space-normalization themselves which means that adding bytes normalization support this would require changing all these keywords. A much better solution is creating a reusable Normalizer class that can handle strings and bytes internally. Even a better idea is reusing the Normalizer class Collections got in PR #4343 and that has since been enhanced. It has a benefit that it handles collections recursively, which automatically brings that recursive support also to BuiltIn. I believe that should get its own issue.

  5. changed the title [-]Explicit and enhanced `bytes` support to BuiltIn keywords that support bytes and strings[/-] [+]Explicit and enhanced `bytes` support to BuiltIn and Collections keywords that support bytes and strings[/+] on Nov 20, 2025
  6. pekkaklarck commented on Nov 20, 2025

    @pekkaklarck
    MemberAuthor

    A benefit of reusing the Normalizer class Collections uses and then enhancing it to support bytes normalization is that then Collections keywords also get better bytes support. Let's cover that also as part of this issue.

    Another benefit is that because we need to enhance Normalizer to support space-normalization that BuiltIn supports, it would be easy to add that support also to Collections. That would require a separate issue and isn't, in my opinion, important enough for RF 7.4.

  7. pekkaklarck commented on Nov 21, 2025

    @pekkaklarck
    MemberAuthor

    I just realized that bytes auto conversion support that was added to Should Contain in #5054 handles both strings (with ordinals below 256) and integers (in range 0-255). There are two problems with this:

    1. It's odd to support integer values that only can create a single byte, but not lists of integers that allow creating byte sequences.
    2. Argument conversion when using arg: bytes only handles strings so it's inconsistent with this special auto conversion. That hasn't been a big deal when only one keyword used it, but now that we are going to add auto conversion to multiple keywords, the inconsistency is getting worse.

    This is how I believe we should fix the above issues:

    1. Enhance auto conversion to support also lists of integers. Probably should also support lists of items that can be converted to integers. This can be done as part of this issue.
    2. Enhance argument conversion with bytes (and bytearray) so that it supports integers and lists of integers as well. That will need a separate issue.
  8. changed the title [-]Explicit and enhanced `bytes` support to BuiltIn and Collections keywords that support bytes and strings[/-] [+]Enhanced and explicit `bytes` support in BuiltIn and Collections[/+] on Nov 21, 2025
  9. pekkaklarck commented on Nov 21, 2025

    @pekkaklarck
    MemberAuthor

    Ping @WisniewskiP, you may like this!

  10. added a commit that references this issue on Nov 21, 2025
    dbaa27a
  11. changed the title [-]Enhanced and explicit `bytes` support in BuiltIn and Collections[/-] [+]Enhanced and explicit `bytes` support in BuiltIn, String and Collections[/+] on Nov 28, 2025
  12. pekkaklarck commented on Nov 28, 2025

    @pekkaklarck
    MemberAuthor

    When adding typing to the String library (#5558), we noticed that many of its keywords have the exact same situation as various BuiltIn and Collections keywords had. I updated the title and reopen this issue.

    In general we'd like all String library keywords to work both with strings and bytes, but in practice that is likely not feasible at least in RF 7.4 scope. The key thing right now is making sure that no keyword that currently works with bytes isn't broken in that regard by using just str as the typing. If there are keywords that can be easily enhanced to work with bytes, that should be done similarly as we enhanced BuiltIn and Collections keywords. Keywords that cannot be enhanced to work with bytes should probably get a separate note about that.

    Can you @silentw0lf list the keywords in String that should still be looked at? I can then take care of updating them once #5554 has been merged.

  13. silentw0lf commented on Nov 30, 2025

    @silentw0lf
    Contributor

    Deeper analysis of String.py implementation/tests needed due to risk due to combinatorics of multiple paramters with different types (bytes vs string) is needed:
    (we can not only focus on named paramter string, ist als must work with other paramters in combination !):

    From Review (see also notes there)¨

    • convert_to_title_case
      • there are some tests indicating that it should only work with str/ not bytes (at least in the past) e.g. Should Be Title Case Does Not Work With ASCII Bytes)
      • also see note of Note of pekkaklarck
    • format_string
    • split_to_lines
      • does not work with bytes: ${{b'hello\nworld'}} -> Length of '["b'hello\nworld'"]' should be 2 but is 1.)
      • Note of pekkaklarck
    • get_line (does not work with bytes: ${{b'hello\nworld'}} -> Length of '["b'hello\nworld'"]' should be 2 but is 1.)
    • get_lines_containing_string
    • get_lines_matching_pattern
    • get_lines_matching_regexp
    • _get_matching_lines
    • get_regexp_matches
    • replace_string (e.g. b.replace(b"old", "new") -> TypeError)
    • replace_string_using_regexp
    • remove_string
    • remove_string_using_regexp
    • split_string
    • split_string_from_right
    • split_string_to_characters (does not work: Split String To Characters ${{b'ab 12'}} -> [97, 98, 32, 49, 50] int represtantion in robot hmmm, maybe tets not correct)
    • fetch_from_left
    • fetch_from_right
    • strip_string
    • should_be_title_case
  14. pekkaklarck commented on Dec 2, 2025

    @pekkaklarck
    MemberAuthor

    The commits above finalized this feature:

    • Should (Not) Match in BuiltIn was enhanced to work with bytes in 80b1919.
    • Part of the String keywords got bytes support in f60ac8f.
    • String library was finalized in 297889d.
  15. added a commit that references this issue on Dec 2, 2025
    297889d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions