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

Support slices as fields #242

Open
victora-openai opened this issue Sep 9, 2024 · 1 comment
Open

Support slices as fields #242

victora-openai opened this issue Sep 9, 2024 · 1 comment
Labels
good first issue Good for newcomers

Comments

@victora-openai
Copy link

Is your feature request related to a problem? Please describe.
It would be useful to support serialization and parsing of slice objects.

For example,

@dataclass
class Foo(DataClassMessagePackMixin):
    my_slice: slice
    slice_dict: dict[slice, int]  # Legal in Python 3.12

f = Foo(slice(0, 5), {slice(3,6,2): 4})
s = f.to_msgpack()
Foo.from_msgpack(s)

Describe the solution you'd like
Slices should be natively serializable and parsable. The snippet above should run.

Describe alternatives you've considered
It's possible to hand write a slice wrapper class. This is what I've been doing but as slice has become hashable in Python 3.12, it would be nice to not have to have a non-trivial wrapping and unwrapping layer.

Additional context
This could be a good first issue as slices can basically be represented as tuples.

@Fatal1ty Fatal1ty added the good first issue Good for newcomers label Sep 9, 2024
@Fatal1ty
Copy link
Owner

Fatal1ty commented Sep 9, 2024

Sounds good to me. A slice object can be represented as a list of [start, stop, step] values. Assuming, that these values can be either integer or None, this would be trivial to implement.

However, I will note that neither a list nor a tuple can be used as dictionary keys, so it cannot be a key in MessagePack format.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants