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):
Textis an alias forstr. It is provided to supply a forward compatible path for Python 2 code: in Python 2,Textis 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'
https://docs.python.org/3/library/typing.html#typing.Text
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With