You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Convert bytes to str sanely in argument conversion #5567
and use it with bytes like b"hello", automatic argument conversion converts the value to a string simply by calling str(value). This means that the keyword ends up getting a string "b'hello'". The conversion can be tested on the interpreter:
>>>str(b"hello")
"b'hello'"
This isn't very useful and can cause quite a lot of confusion. The keyword seems to work, or at least argument conversion doesn't fail, but the actual value is most likely not what expected.
A much better approach is mapping each bytes to a matching Unicode code point. This means that bytes like b"hello" is converted to a string "hello". This can be demonstrated as below:
>>>"".join(chr(o) foroinb"hello")
'hello'>>>b"hello".decode("latin-1") # simpler and faster than the above'hello'
A major benefit of this approach is that it is symmetric to how strings are converted to bytes if you have a keyword that accepts arg: bytes and call it with a string like "hello". Simply calling bytes("hello") wouldn't even work and argument conversion uses "hello".encode("latin-1") that yields b"hello".
Another possible approach would be explicitly rejecting bytes when an argument has str as its type. I can see some benefits in that, but it would be inconsistent with how argument conversion works in general and especially how it works in str to bytes conversion already now.
Notice that the key invariant is that bytes should be mapped directly to matching Unicode code points. We do this to the opposite direction already when an argument has type arg: bytes and the value is string. In practice what we want is this:
This isn't really about text encodings. The fact that we can use Latin-1 encoding instead of converting each character separately is mostly an implementation detail:
This looks very good in itself, but due to how argument conversion currently handles conversion to str the behavior changes. After the PR is merged, if the keyword is called with bytes like b"hello", there's no exception anymore. Instead the value is converted to string "b'hello'" by argument conversion and the keyword then converts it to bytes b"b'hello'". This is not desirable.
The keyword itself could handle this by using string: str | bytes as the typing instead of just string: str and then explicitly rejecting bytes with something like if isinstance(string, bytes): raise TypeError(...). It would work, but it would be rather strange if the typing advertised bytes support but the implementation would then reject. This also wouldn't fix the same issue with all other keywords that accept str and are used with bytes. I thus believe changing argument conversion makes much more sense.
I marked this issue backwards incompatible, because handling bytes changes. This only happens if bytes aren't explicitly accepted, though, and the result are rather confusing, so I believe doing the change in a non-major release is fine. This should nevertheless be explicitly mentioned in the release notes.
This is related to the enhanced bytes support tracked by #5548. Keywords that accept str now work better with bytes than earlier. Depending on the case, proper bytes support would require accepting str | bytes and returning corresponding value, though.
Activity
pekkaklarck commentedon Nov 30, 2025
Notice that the key invariant is that bytes should be mapped directly to matching Unicode code points. We do this to the opposite direction already when an argument has type
arg: bytesand the value is string. In practice what we want is this:This isn't really about text encodings. The fact that we can use Latin-1 encoding instead of converting each character separately is mostly an implementation detail:
pekkaklarck commentedon Nov 30, 2025
I noticed this issue when review PR #5554 that adds typing to the String library. The library has a keyword that currently looks like this:
This is a reasonable implementation. If you call the keyword with a string, the result is something like this (using
sfrom the previous comment):On the other hand. calling with bytes explicitly fails (but perhaps not in the nicest possible way):
In the PR the keyword is changed to this:
This looks very good in itself, but due to how argument conversion currently handles conversion to
strthe behavior changes. After the PR is merged, if the keyword is called with bytes likeb"hello", there's no exception anymore. Instead the value is converted to string"b'hello'"by argument conversion and the keyword then converts it to bytesb"b'hello'". This is not desirable.The keyword itself could handle this by using
string: str | bytesas the typing instead of juststring: strand then explicitly rejecting bytes with something likeif isinstance(string, bytes): raise TypeError(...). It would work, but it would be rather strange if the typing advertisedbytessupport but the implementation would then reject. This also wouldn't fix the same issue with all other keywords that acceptstrand are used withbytes. I thus believe changing argument conversion makes much more sense.pekkaklarck commentedon Nov 30, 2025
I marked this issue backwards incompatible, because handling bytes changes. This only happens if bytes aren't explicitly accepted, though, and the result are rather confusing, so I believe doing the change in a non-major release is fine. This should nevertheless be explicitly mentioned in the release notes.
pekkaklarck commentedon Nov 30, 2025
This is related to the enhanced bytes support tracked by #5548. Keywords that accept
strnow work better with bytes than earlier. Depending on the case, proper bytes support would require acceptingstr | bytesand returning corresponding value, though.Convert bytes to `str` sanely in argument conversion