Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/celpy/celtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,9 @@ def __new__(
def __repr__(self) -> str:
return f"{self.__class__.__name__}({super().__repr__()})"

def contains(self, item: Value) -> BoolType:
return BoolType(cast(StringType, item) in self)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This isn't passing tests (for me, or in CI) Perhaps we should do this instead?

Suggested change
return BoolType(cast(StringType, item) in self)
return BoolType(BytesType(item) in self)

Copy link
Collaborator

@hudlow hudlow Sep 2, 2025

Choose a reason for hiding this comment

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

Or maybe you meant for this to be cast(BytesType, item) but if so you'd want to do assert b_0.contains(b"byte") below...

Copy link
Collaborator

Choose a reason for hiding this comment

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

Okay, did a bit more testing, and it seems like this is what's necessary to appease the type checker (and what's consistent with other examples of contains()):

Suggested change
return BoolType(cast(StringType, item) in self)
return BoolType(cast(bytes, item) in cast(bytes, self))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hm, I'm mostly basing this on the StringType impl; why do we need to cast to bytes instead?

Copy link
Collaborator

Choose a reason for hiding this comment

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

@stefanvanburen logically, a string is a sequence of Unicode code points and there is always an encoding choice to be made to represent it as bytes.



class DoubleType(float):
"""
Expand Down
1 change: 1 addition & 0 deletions tests/test_celtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ def test_bytes_type():
assert repr(b_0) == "BytesType(b'bytes')"
assert BytesType(None) == BytesType(b'')
assert BytesType(MessageType({"value": BytesType(b'42')})) == BytesType(b'42')
assert b_0.contains("byte")


def test_double_type():
Expand Down
Loading