Comments (2)
Yeah, we don't support implicit unions based on subclasses for a few reasons:
- It can be unclear what is expected. Does the decoder expect exactly the
TaggedBase
message schema, or any subclass of it? Does the union include the base class, or only its subclasses? What if new subclasses ofTaggedBase
are defined after aDecoder
is created? - Supporting implicit unions based on subclasses means that you can't look at a message schema and know if a field is a union or a single struct. I think being explicit is better here.
- From an implementation detail, explicit unions are a lot simpler to support using
msgspec
's internal data model.
From a usability perspective, I wonder what could be done here. Better documentation could certainly help. I don't see an easy way to infer this mistake based on usage to raise a nicer warning.
We could also provide a utility to autogenerate a Union
based on subclasses of a type. Something like:
import typing
import msgspec
T = typing.TypeVar("T")
def subclass_union(cls: typing.Type[T]) -> typing.Type[T]:
"""Returns a Union of all subclasses of `cls` (excluding `cls` itself)"""
classes = set()
def _add(cls):
for c in cls.__subclasses__():
_add(c)
classes.add(cls)
for c in cls.__subclasses__():
_add(c)
return typing.Union[tuple(classes)] # type: ignore
class Base(msgspec.Struct, tag=True):
pass
class Foo(Base):
x: int
class Bar(Base):
x: float
class Baz(Bar):
y: float
decoder = msgspec.json.Decoder(subclass_union(Base))
print(decoder.decode(b'{"type": "Foo", "x": 1}'))
out = decoder.decode(b'{"type": "Bar", "x": 2.0}')
print(out)
if typing.TYPE_CHECKING:
reveal_type(out)
$ python subclass.py
Foo(x=1)
Bar(x=2.0)
$ mypy subclass.py
subclass.py:41: note: Revealed type is "subclass.Base"
Success: no issues found in 1 source file
$ pyright subclass.py
No configuration file found.
No pyproject.toml file found.
stubPath /home/jcristharif/Code/msgspec/typings is not a valid directory.
Assuming Python platform Linux
Searching for source files
Found 1 source file
/home/jcristharif/Code/msgspec/subclass.py
/home/jcristharif/Code/msgspec/subclass.py:41:17 - information: Type of "out" is "Base"
0 errors, 0 warnings, 1 information
Completed in 0.728sec
For reasons that are unclear to me, both mypy
and pyright
type check properly if the output of subclass_union
is passed on to a Decoder
/decode
function, but only pyright
type checks properly if the output of subclass_union
is used to define a larger composite type. mypy
complains about a variable being used in a typing context. For example:
BaseUnion = subclass_union(Base)
class Message(msgspec.Struct):
data: BaseUnion
For this reason, I'm reluctant to add this utility to msgspec itself, but if it helps you you're welcome to copy-paste it into your project.
from msgspec.
Thanks for the context, appreciate it. I feel the stance to be explicit here fits nicely with the purpose of the library.
For my own codebase, I'll find a nice compromise. Closing!
from msgspec.
Related Issues (20)
- Inherit __init_subclass__ args to subclass HOT 1
- ormsgpack benchmark comparison HOT 1
- encode sort_keys argument HOT 16
- Allow more than one str-like type in unions HOT 5
- decode's strict=False does not cast floats to ints HOT 3
- Typed encoding HOT 1
- Validation on serialization HOT 5
- Make msgspec more aware of large data and other serialization protocols
- Recommendation for Efficiently Decoding BSON from PyMongo with msgspec
- leading underscores and `"camel"` rename strategy HOT 2
- Constraint check for typing.Optional field HOT 1
- Automatically support `functools.cached_property` without requiring setting `dict=True` HOT 2
- UnboundLocalError for `new_scope` HOT 3
- Sign release tags when making future releases? HOT 4
- How can I do data normalization on a frozen instance in `__post_init__`? HOT 1
- Cannot convert with `from_attributes` when using a rename convention HOT 3
- Cannot set `gc=False` on Generic structs? HOT 5
- Support types.MappingProxyType HOT 3
- Add either `init_omit_defaults` or `omit_none` HOT 5
- Consider making `DecodeError` and `ValidationError` inherit from `ValueError` HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from msgspec.