annotate urllib3/util/util.py @ 16:dc2c003078e9 tip

planemo upload for repository https://toolrepo.galaxytrakr.org/view/jpayne/bioproject_to_srr_2/556cac4fb538
author jpayne
date Tue, 21 May 2024 01:09:25 -0400
parents 5eb2d5e3bf22
children
rev   line source
jpayne@7 1 from __future__ import annotations
jpayne@7 2
jpayne@7 3 import typing
jpayne@7 4 from types import TracebackType
jpayne@7 5
jpayne@7 6
jpayne@7 7 def to_bytes(
jpayne@7 8 x: str | bytes, encoding: str | None = None, errors: str | None = None
jpayne@7 9 ) -> bytes:
jpayne@7 10 if isinstance(x, bytes):
jpayne@7 11 return x
jpayne@7 12 elif not isinstance(x, str):
jpayne@7 13 raise TypeError(f"not expecting type {type(x).__name__}")
jpayne@7 14 if encoding or errors:
jpayne@7 15 return x.encode(encoding or "utf-8", errors=errors or "strict")
jpayne@7 16 return x.encode()
jpayne@7 17
jpayne@7 18
jpayne@7 19 def to_str(
jpayne@7 20 x: str | bytes, encoding: str | None = None, errors: str | None = None
jpayne@7 21 ) -> str:
jpayne@7 22 if isinstance(x, str):
jpayne@7 23 return x
jpayne@7 24 elif not isinstance(x, bytes):
jpayne@7 25 raise TypeError(f"not expecting type {type(x).__name__}")
jpayne@7 26 if encoding or errors:
jpayne@7 27 return x.decode(encoding or "utf-8", errors=errors or "strict")
jpayne@7 28 return x.decode()
jpayne@7 29
jpayne@7 30
jpayne@7 31 def reraise(
jpayne@7 32 tp: type[BaseException] | None,
jpayne@7 33 value: BaseException,
jpayne@7 34 tb: TracebackType | None = None,
jpayne@7 35 ) -> typing.NoReturn:
jpayne@7 36 try:
jpayne@7 37 if value.__traceback__ is not tb:
jpayne@7 38 raise value.with_traceback(tb)
jpayne@7 39 raise value
jpayne@7 40 finally:
jpayne@7 41 value = None # type: ignore[assignment]
jpayne@7 42 tb = None