Skip to content

Commit

Permalink
Added more explcit typing to from_unicode and changed to have one return
Browse files Browse the repository at this point in the history
  • Loading branch information
capuanob committed Nov 5, 2023
1 parent db54100 commit c760122
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/icalendar/parser_tools.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
from typing import Any

SEQUENCE_TYPES = (list, tuple)
DEFAULT_ENCODING = 'utf-8'


def from_unicode(value, encoding='utf-8') -> bytes:
def from_unicode(value: Any, encoding='utf-8') -> bytes:
"""
Converts a value to bytes, even if it already is bytes
:param value: The value to convert
:param encoding: The encoding to use in the conversion
:return: The bytes representation of the value
"""
if isinstance(value, bytes):
return value
value = value
elif isinstance(value, str):
try:
return value.encode(encoding)
value = value.encode(encoding)
except UnicodeEncodeError:
value = value.encode('utf-8', 'replace')
return value
Expand Down

0 comments on commit c760122

Please sign in to comment.