Update force_bytes and force_unicode to support casting objects to strings.

Review Request #13033 — Created May 10, 2023 and submitted

Information

RBTools
release-5.x

Reviewers

force_bytes() and force_unicode() now take a strings_only boolean
argument, which defaults to True for backwards-compatibility. When set
to False, these functions can take any arbitrary object and try to
turn it into a string.

force_bytes() will look for a __bytes__() dunder method, using that to
get the result, and then falling back on __str__().

force_unicode() will just look for __str__(). Since every object has
this, it will technically work with any object.

Typing has been updated to provide overloads based on the strings_only
parameter, helping to ensure callers receive type warnings if they
provide something other than a string without opting into
strings_only=False.

Unit tests have been added to cover this module.

Unit tests pass.

Tested this with some upcoming fixes for command output rendering.

Summary ID
Update force_bytes and force_unicode to support casting objects to strings.
`force_bytes()` and `force_unicode()` now take a `strings_only` boolean argument, which defaults to `True` for backwards-compatibility. When set to `False`, these functions can take any arbitrary object and try to turn it into a string. `force_bytes()` will look for a `__bytes__()` dunder method, using that to get the result, and then falling back on `__str__()`. `force_unicode()` will just look for `__str__()`. Since every object has this, it will technically work with any object. Typing has been updated to provide overloads based on the `strings_only` parameter, helping to ensure callers receive type warnings if they provide something other than a string without opting into `strings_only=False`. Unit tests have been added to cover this module.
f0ec00cf06f36a3498cec8727d2243e72b235373
Description From Last Updated

Can we be specific here that this means "str or bytes"? For a minute I was thinking this was str …

daviddavid

Can we be specific here that this means "str or bytes"? For a minute I was thinking this was str …

daviddavid
david
  1. 
      
  2. rbtools/utils/encoding.py (Diff revision 1)
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     

    Neat!

  3. rbtools/utils/encoding.py (Diff revision 1)
     
     

    Can we be specific here that this means "str or bytes"? For a minute I was thinking this was str only, which was quite confusing.

  4. rbtools/utils/encoding.py (Diff revision 1)
     
     

    Can we be specific here that this means "str or bytes"? For a minute I was thinking this was str only, which was quite confusing.

  5. 
      
chipx86
david
  1. Ship It!
  2. 
      
chipx86
Review request changed

Status: Closed (submitted)

Change Summary:

Pushed to release-5.x (e0fe507)
chipx86
Review request changed

Status: Closed (submitted)

Change Summary:

Pushed to master (e0fe507)
Loading...