Improvements to the recent unicode changes #137
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
#135 enabled unicode support, but it was quite limited for py2 users:
This change handles unicode much more uniformly, and only converts back to str at
__str__
and raising exceptions. As a general rule, I've gone for "unicode in unicode out, str in str out". That essentially means thattype(unit.origin)
will be the same as the original type that you constructed the Unit with (unless it was an integer, in which case you will get str).The error handling is now far superior also:
One of the main changes here is the introduction of the dunder
__unicode__
method -__str__
must always return a string but we can choose to implement__unicode__
which can then be encoded to astr
afterwards.I'll be honest and say that this definitely has not been a fun experience. It has taken many hours longer than I'd hoped, and I've ended up finding some ugly warts in py2's str implementation (I knew they were there, but I hadn't realised how bad they were until I'd seen the py3 light 🌞 ).
This whole thing will be avoided on the day we drop py2 support. A decision is definitely needed in that regard. Perhaps we should follow suit from SciTools/cartopy#1186.