[Python-porting] builtins.str return type in py2

Justin Israel justinisrael at gmail.com
Sun Dec 16 04:56:38 EST 2018


Hi,

I've just been digging into future for one of my existing python2 libraries
that needs py3 support. One thing I am wondering is about the compatability
of the builtins.str type on python2. I have reviewed the API documentation (
http://python-future.org/str_object.html)

It seems like part of the documentation describes the backported py2 class
as wanting to remain compatable with py2, while part of it says it follows
the py3 functionality. I have a usage of str.translate in my library, and
they have different signatures:

py2
S.translate(table [,deletechars]) -> string

py3
S.translate(table) -> str

with builtins.str following the py3 signature.
So it makes me wonder what I should be expecting if I import the
builtins.str type and allow it to be returned to users of the library. Is
it expected that a user of a library providing py2/3 support via future
might return string types that aren't fully compatible with python2 code?
What if a user tries to call str.translate on the returned string, or some
other method that happens to follow the py3 signature?

Looking for a little extra guidance on the best practices around returning
these backported builtins to py2 users.

Thanks!
Justin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-porting/attachments/20181216/2ed60dfd/attachment.html>


More information about the Python-porting mailing list