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

Fix str() bug for tuple values #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

cfs-pure
Copy link

@cfs-pure cfs-pure commented Nov 1, 2023

Replaced the use of old-style formatting strings in str methods.

At first, that seems like a dated but harmless practice, but unfortunately

While dated and seemingly a harmless practice, OSlash actually uses it in an incorrect way.

If a program using OSlash deals with types other than strings, then the str methods for classes like Left, Right, and Either, OSlash directly writes:

return '%s' % value

where value is an arbitrary type.

Unfortunately, '%s' % value is only guaranteed to work if the value is a string, since Python will not call str() on it.

(Well, more precisely, '%s' % value is guaranteed to fail if the value happens to be a tuple of a certain kind, since Python will try to unpack the tuple into the string formatting operation)

The upshot is that such programs will get TypeErrors. Instead, use Python 3.8+ f-strings to guarantee that the value is converted to a string.

Replaced the use of old-style formatting strings in __str__ methods.

At first, that seems like a dated but harmless practice, but unfortunately

While dated and seemingly a harmless practice, OSlash actually uses it
in an incorrect way.

If a program using OSlash deals with types other than strings, then the
__str__ methods for classes like `Left`, `Right`, and `Either`, OSlash
directly writes:

    return '%s' % value

where `value` is an arbitrary type.

Unfortunately, '%s' % value is only guaranteed to work if the value
is a string, since Python will not call `str()` on it.

(Well, more precisely, `'%s' % value` is guaranteed to *fail* if the
value happens to be a tuple of a certain kind, since Python will try
to unpack the tuple into the string formatting operation)

The upshot is that such programs will get TypeErrors.  Instead, use
Python 3.8+ f-strings to guarantee that the value is converted to a
string.
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