Byte/unicode conversions
------------------------
- These functions are used extensively within Tornado itself,
- but should not be directly needed by most applications. Note that
- much of the complexity of these functions comes from the fact that
- Tornado supports both Python 2 and Python 3.
.. autofunction:: utf8
.. autofunction:: to_unicode
.. function:: native_str
+ .. function:: to_basestring
- Converts a byte or unicode string into type `str`. Equivalent to
- `utf8` on Python 2 and `to_unicode` on Python 3.
-
- .. autofunction:: to_basestring
+ Converts a byte or unicode string into type `str`. These functions
+ were used to help transition from Python 2 to Python 3 but are now
+ deprecated aliases for `to_unicode`.
.. autofunction:: recursive_unicode
import re
import urllib.parse
-from tornado.util import unicode_type, basestring_type
+from tornado.util import unicode_type
import typing
from typing import Union, Any, Optional, Dict, List, Callable
def json_decode(value: Union[str, bytes]) -> Any:
- """Returns Python objects for the given JSON string."""
+ """Returns Python objects for the given JSON string.
+
+ Supports both `str` and `bytes` inputs.
+ """
return json.loads(to_basestring(value))
# When dealing with the standard library across python 2 and 3 it is
# sometimes useful to have a direct conversion to the native string type
native_str = to_unicode
-
-_BASESTRING_TYPES = (basestring_type, type(None))
-
-
-@typing.overload
-def to_basestring(value: str) -> str:
- pass
-
-
-@typing.overload # noqa: F811
-def to_basestring(value: bytes) -> str:
- pass
-
-
-@typing.overload # noqa: F811
-def to_basestring(value: None) -> None:
- pass
-
-
-def to_basestring(value: Union[None, str, bytes]) -> Optional[str]: # noqa: F811
- """Converts a string argument to a subclass of basestring.
-
- In python2, byte and unicode strings are mostly interchangeable,
- so functions that deal with a user-supplied argument in combination
- with ascii string constants can use either and should return the type
- the user supplied. In python3, the two types are not interchangeable,
- so this method is needed to convert byte strings to unicode.
- """
- if isinstance(value, _BASESTRING_TYPES):
- return value
- if not isinstance(value, bytes):
- raise TypeError("Expected bytes, unicode, or None; got %r" % type(value))
- return value.decode("utf-8")
+to_basestring = to_unicode
def recursive_unicode(obj: Any) -> Any: