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 type checking to strings/bytes for Python/Ruby #1597

Merged
merged 1 commit into from
Jun 19, 2023

Conversation

heinrich5991
Copy link
Contributor

Also fixes that bytes had to be valid UTF-8 due to #1595.

Also fixes that bytes had to be valid UTF-8 due to mozilla#1595.
@heinrich5991 heinrich5991 requested a review from a team as a code owner June 15, 2023 09:30
@heinrich5991 heinrich5991 requested review from tarikeshaq and removed request for a team June 15, 2023 09:30
@badboy badboy self-requested a review June 15, 2023 09:50
@heinrich5991
Copy link
Contributor Author

Test failure is the unrelated #1553.

Copy link
Member

@mhammond mhammond left a comment

Choose a reason for hiding this comment

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

I like the idea of better test coverage, but I'm not quite clear on some of the decisions made (and sorry if I missed a discussion elsewhere, please point me to it!)

@@ -8,5 +8,9 @@ def read(buf):

@staticmethod
def write(value, buf):
try:
Copy link
Member

Choose a reason for hiding this comment

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

Why not isinstance() here? I assume it's because you want to support anything exposing the buffer protocol - so why do bytes get "anything bytes-like" but strings aren't "anything string-like"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

assume it's because you want to support anything exposing the buffer protocol

Yes, basically that.

I assume it's because you want to support anything exposing the buffer protocol - so why do bytes get "anything bytes-like" but strings aren't "anything string-like"?

Because I'm trying to mirror Python's native semantics here. Take e.g. the lambda x: str(x, encoding="utf8") function. It allows x to be anything adhering to the buffer protocol. But lambda x: bytes(x, encoding="utf8") only allows x to be str, no other classes pretending to be str. This also seems to be the case for other functions in Python that take bytes or str: Functions taking bytes usually take anything implementing the buffer protocol, functions taking str only allow str.

self.assertRaises(TypeError, lambda: take_string(0.0))
self.assertRaises(TypeError, lambda: take_string(b""))

class A:
Copy link
Member

Choose a reason for hiding this comment

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

It's not clear to me what the current semantics are - does this work without this patch? If so, what's the motivation for preventing this from working?

Copy link
Member

@mhammond mhammond Jun 15, 2023

Choose a reason for hiding this comment

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

(I mean, I guess "does the object allow str(ob)?" isn't really a great indicator of "this object wants to be seen as string like", but my question is really more about "do we want to exclude objects which want to be string like?"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's not clear to me what the current semantics are - does this work without this patch?

I didn't change the current semantics except for throwing TypeError instead of AttributeError. So no, it does not work without this patch.

In this way, this test is trying to test for the current behavior.

@heinrich5991
Copy link
Contributor Author

but I'm not quite clear on some of the decisions made (and sorry if I missed a discussion elsewhere, please point me to it!)

You haven't missed a discussion on this. I probably should have expanded on the choices I made in the commit message.

@mhammond
Copy link
Member

Thanks for the explanation. I'm fine with this, but I think you should wait for @badboy to say he is :)

@badboy badboy modified the milestones: v0.24.1+, v0.24.0 Jun 19, 2023
@badboy badboy merged commit 73caaf7 into mozilla:main Jun 19, 2023
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