jcrist / msgspec

A fast serialization and validation library, with builtin support for JSON, MessagePack, YAML, and TOML
https://jcristharif.com/msgspec/
BSD 3-Clause "New" or "Revised" License
2.01k stars 59 forks source link

Allow conversion to collection from generator #641

Open floxay opened 5 months ago

floxay commented 5 months ago

Description

I often need to convert objects that are nested. I would like to skip creating a new list for this but this currently(?) does not seem possible.

Given this JSON:

[
    {
        "id": "Jf8K2ji09uG0d",
        "data": {
            "value1": 23,
            "value2": "http"
        }
    },
    ...
]

and this Struct

class Data(msgspec.Struct):
    value1: int
    value2: str

I would like to do this:

msgspec.convert((obj["data"] for obj in objs), type=list[Data])
jcrist commented 5 months ago

Sure, I think this is something we could support. This is unlikely to be much more efficient than if you passed in a list instead of a generator expression though - IMO the only real benefit here would be developer ergonomics not performance.

# This works today and is likely to be just as fast as if you used a generator expression
msgspec.convert([obj["data"] for obj in objs], type=list[Data])
floxay commented 5 months ago

Well, I do admit that I was hoping for some performance benefits by doing this. :P