Return the unicode representation of a bytes object ``text``. If ``text`` is already an unicode object, return it as-is.
(
text: str | bytes, encoding: str | None = None, errors: str = "strict"
)
| 70 | |
| 71 | |
| 72 | def to_unicode( |
| 73 | text: str | bytes, encoding: str | None = None, errors: str = "strict" |
| 74 | ) -> str: |
| 75 | """Return the unicode representation of a bytes object ``text``. If |
| 76 | ``text`` is already an unicode object, return it as-is.""" |
| 77 | if isinstance(text, str): |
| 78 | return text |
| 79 | if not isinstance(text, (bytes, str)): |
| 80 | raise TypeError( |
| 81 | f"to_unicode must receive a bytes or str object, got {type(text).__name__}" |
| 82 | ) |
| 83 | if encoding is None: |
| 84 | encoding = "utf-8" |
| 85 | return text.decode(encoding, errors) |
| 86 | |
| 87 | |
| 88 | def to_bytes( |
no outgoing calls