Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add various string conversion utilities #294

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

jhance
Copy link

@jhance jhance commented Jun 6, 2019

Currently six provides ensure_str, ensure_text, and ensure_binary. Typically these are used when you have a str object and want to convert to text or bytes, or the inverse.

I'd prefer to be explicit about which conversion I'm doing. For example, using six.ensure_binary should probably not be accepting text in py2 and should not be accepting binary in py3 (for most use cases). Effectively these equivalents make the code a little stricter about what types are actually expected.

They should also be more performant as they avoid an isinstance lookup on every conversion.

.. function:: str_to_binary(s, encoding='utf-8', errors='strict')

Encodes s to :data:`binary_type` in python 3 only. No-op in python 2.
*encoding*, *errors* are same as :meth:`py3.str.encode`

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For completeness, I'd mention the type of the argument (it's possible to infer it from the rest but it took me a while). And do this for all functions.

@benjaminp
Copy link
Owner

The main thing that makes me queasy about these is that str should usually be avoided when porting. Decide on either bytes or text. Maybe the cat is out of the bag with ensure_str, but this feels like a lot more str surface area.

@gvanrossum
Copy link

IIUC @jhance found a lot of situations where these would be really handy. Maybe he can quote some here? Or you two can discuss in person. :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants