jcrist · GitHub

@jcrist

This improves the flexibility of `from_builtins` in the following ways:
- Arbitrary input types are now accepted, with the caveat that anything
  not part of the set of "builtin" types cannot be coerced except to
  another custom type. This means that you can decode e.g.
  `bson.ObjectId` objects to `bson.ObjectId`. You can also convert them
  to another custom type. But you can't decode them to `str`.
- Subclasses of `int` and `bytes` are now accepted, and can be coerced
  to their base classes (or in the case of int subclasses, other
  integer-like types like `IntEnum`). These changes are to support
  mapping `bson.Int64` and `bson.Binary` to `int` and `bytes`
  respectively. Since these types use `tp_flags` for fast subclass
  checks, there's little overhead to supporting subclasses like this as
  inputs. We hold off supporting arbitrary subclasses for now until a
  user with a valid use case asks for it.
With these changes, the `bson` library shipped as part of `pymongo` can
now be wrapped with `from_builtins` with no extra configuration needed:
```python
def decode(msg: bytes, type: type[T] = Any) -> T:
    return msgspec.from_builtins(bson.decode(msg), type)
```

Read the original on github.com ↗