Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b09b3f7

Browse files
author
Guido van Rossum
committed
Add docs for typing.AnyStr and typing.Text. By Michael Lee. (Merge 3.5->3.6)
2 parents fc560a8 + aa9560c commit b09b3f7

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

Doc/library/typing.rst

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,33 @@ The module defines the following classes, functions and decorators:
652652
yield start
653653
start += 1
654654

655+
.. class:: AnyStr
656+
657+
``AnyStr`` is a type variable defined as
658+
``AnyStr = TypeVar('AnyStr', str, bytes)``.
659+
660+
It is meant to be used for functions that may accept any kind of string
661+
without allowing different kinds of strings to mix. For example::
662+
663+
def concat(a: AnyStr, b: AnyStr) -> AnyStr:
664+
return a + b
665+
666+
concat(u"foo", u"bar") # Ok, output has type 'unicode'
667+
concat(b"foo", b"bar") # Ok, output has type 'bytes'
668+
concat(u"foo", b"bar") # Error, cannot mix unicode and bytes
669+
670+
.. class:: Text
671+
672+
``Text`` is an alias for ``str``. It is provided to supply a forward
673+
compatible path for Python 2 code: in Python 2, ``Text`` is an alias for
674+
``unicode``.
675+
676+
Use ``Text`` to indicate that a value must contain a unicode string in
677+
a manner that is compatible with both Python 2 and Python 3::
678+
679+
def add_unicode_checkmark(text: Text) -> Text:
680+
return text + u' \u2713'
681+
655682
.. class:: io
656683

657684
Wrapper namespace for I/O stream types.

0 commit comments

Comments
 (0)