diff -r 61d7aeb6aa1b -r 1553d6fdfdad Doc/library/typing.rst --- a/Doc/library/typing.rst Tue Aug 09 17:45:15 2016 -0500 +++ b/Doc/library/typing.rst Tue Aug 09 18:26:53 2016 -0700 @@ -652,6 +652,32 @@ yield start start += 1 +.. class:: AnyStr + + ``AnyStr`` is a type alias meant to be used for functions that may accept + any kind of string without allowing different kinds of strings to mix:: + + def concat(a: AnyStr, b: AnyStr) -> AnyStr: + return a + b + + concat(u"foo", u"bar") # Ok, has type 'unicode' + concat(b"foo", b"bar") # Ok, has type 'bytes' + concat(u"foo", b"bar") # Error + + It is exactly equivalent to ``AnyStr = TypeVar('AnyStr', str, bytes)`` + in Python 3 and ``AnyStr = TypeVar('AnyStr', str, unicode)`` in Python 2. + +.. class:: Text + + ``Text`` is a type alias meant for strings containing textual data. + + ``Text`` is aliased to ``unicode`` in Python 2 and ``str`` in Python 3. + Use ``Text`` to indicate that a value must contain a unicode string in + a manner that is compatible with both Python 2 and Python 3:: + + def add_unicode_checkmark(text: Text) -> Text: + return text + u' \u2713' + .. class:: io Wrapper namespace for I/O stream types.