What should I use when type annotating a string, Text, or str. What is the difference when using either?
for ex:
from typing import Text
def spring(a: Text) -> Text:
return a.upper()
or
def spring(a: str) -> str:
return a.upper()
What should I use when type annotating a string, Text, or str. What is the difference when using either?
for ex:
from typing import Text
def spring(a: Text) -> Text:
return a.upper()
or
def spring(a: str) -> str:
return a.upper()
From the docs (As mentioned in Ians comment):
Text
is an alias forstr
. It is provided to supply a forward compatible path for Python 2 code: in Python 2,Text
is an alias forunicode
.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'
str
."