File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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.
You can’t perform that action at this time.
0 commit comments