msgspec icon indicating copy to clipboard operation
msgspec copied to clipboard

The addition of varying fields

Open ApxMK opened this issue 4 months ago • 3 comments

Description

I have thought of a new feature which could really benefit developers. So it would be good to have a msgspec class which has a "varying" field type. Meaning that if two instances have the same field and type but differ by the amount/repetition of that field, then they should be considered an instance of the msgspec class and of the same type. This would be useful in modelling situation when you have instance 1: field 1 : type y field2[0] : type x field2[1] : type x field2[2] : type x

And instance 2: field 1 : type y field2[0] : type x field2[1] : type x field2[2] : type x field2[3] : type x field2[4] : type x

And where the msgspec fields could be something like: field 1 : type y field2[varying] : type x

ApxMK avatar Sep 02 '25 05:09 ApxMK

Why not use a simple list or tuple?

class Foo(msgspec.Struct):
    field1: TypeY
    field2: tuple[TypeX, ...]

Annotating tuples

floxay avatar Sep 02 '25 09:09 floxay

Why not use a simple list or tuple?

class Foo(msgspec.Struct): field1: TypeY field2: tuple[TypeX, ...] Annotating tuples

The types between the instances do not differ. But they differ by the number of a specific field of the same annotation type. I want msg spec to support such a case where there can be two instances, but one instance differs by the number of a specific field on the msg spec class. Where both instances are still considered an instance o the same msgspec class.

ApxMK avatar Sep 03 '25 03:09 ApxMK

I want msg spec to support such a case where there can be two instances, but one instance differs by the number of a specific field on the msg spec class. Where both instances are still considered an instance o the same msgspec class.

@ApxMK thats what @floxay suggested .. can you close the request / or add more details if the suggested solution do not work for you.

return42 avatar Oct 17 '25 06:10 return42