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

Styling non-strings #40

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft

Conversation

keturn
Copy link
Contributor

@keturn keturn commented Jan 5, 2020

Here are a few test cases demonstrating failures when colorful makes ColorfulStrings out of non-string objects.

s.red(1) + s.red(2) case is particularly fun for generating quirky results that sneak by without raising exceptions.

It's sneaky because it gives different results when under NO_COLORS and
slips through without raising exceptions.
@keturn
Copy link
Contributor Author

keturn commented Jan 5, 2020

Not sure how you'd like to handle this.

The thing that seems most obvious to me is for ColorfulStyle.evaluate to str() everything that comes in, so that ColorfulString.orig_string is always a string.

Strictly speaking, that won't be 100% backwards compatible, because you'll be executing the input's __str__ earlier than you would have, and things like ColorfulString.__format__ will no longer call the original object's __format__.

but since style_string freezes its input as a string at that time anyway, it's probably the only thing that makes sense, to do them both at the same time.

@keturn
Copy link
Contributor Author

keturn commented Jan 5, 2020

And while I'm looking at ColorfulString, I notice __len__ uses orig_string but __iter__ uses styled_string.

Is that intentional? Should __iter__ at least switch on colormode == NO_COLORS?

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.

1 participant