diff options
author | Guido van Rossum <guido@dropbox.com> | 2016-08-15 22:08:11 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@dropbox.com> | 2016-08-15 22:08:11 (GMT) |
commit | b09b3f7ab9c69eaad09a9b495cdcdb108a5898ac (patch) | |
tree | d888d8b992ed7ae4843c4bed1fdbf83b6b02b8a5 /Doc/library/typing.rst | |
parent | fc560a84d2c27cfe2dda97618e2507e8a3f22fea (diff) | |
parent | aa9560c6331c136cbe1e9676a2c48a1e0080a32a (diff) | |
download | cpython-b09b3f7ab9c69eaad09a9b495cdcdb108a5898ac.zip cpython-b09b3f7ab9c69eaad09a9b495cdcdb108a5898ac.tar.gz cpython-b09b3f7ab9c69eaad09a9b495cdcdb108a5898ac.tar.bz2 |
Add docs for typing.AnyStr and typing.Text. By Michael Lee. (Merge 3.5->3.6)
Diffstat (limited to 'Doc/library/typing.rst')
-rw-r--r-- | Doc/library/typing.rst | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst index c83bf6b..3eaf166 100644 --- a/Doc/library/typing.rst +++ b/Doc/library/typing.rst @@ -652,6 +652,33 @@ The module defines the following classes, functions and decorators: yield start start += 1 +.. class:: AnyStr + + ``AnyStr`` is a type variable defined as + ``AnyStr = TypeVar('AnyStr', str, bytes)``. + + It is meant to be used for functions that may accept any kind of string + without allowing different kinds of strings to mix. For example:: + + def concat(a: AnyStr, b: AnyStr) -> AnyStr: + return a + b + + concat(u"foo", u"bar") # Ok, output has type 'unicode' + concat(b"foo", b"bar") # Ok, output has type 'bytes' + concat(u"foo", b"bar") # Error, cannot mix unicode and bytes + +.. class:: Text + + ``Text`` is an alias for ``str``. It is provided to supply a forward + compatible path for Python 2 code: in Python 2, ``Text`` is an alias for + ``unicode``. + + 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. |