-
Notifications
You must be signed in to change notification settings - Fork 96
Msgspec Doesn't Decode Json Correctly when SQLAlchemy.Mapped is used. #820
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
Comments
Having the same issue here. It's basically forced me not to use msgspec.convert throughout my code base... :/
|
@timinou I have also tried other alternatives and I am willing to name them all including providing other things that I've tried.
|
Wow yeah @Vizonex you definitely thought about this. Thanks for the resources and the reflections! I looked at your SQLTable code and it led me to remove the
This is the code, I think mostly from SQLTable: def remove_mapping_annotations(cls):
"""Temporarly alters annotations so msgspec can understand
how to decode variables that are associated to `Mapped`"""
__previous_annotations__ = inspect.get_annotations(
cls, eval_str=True
)
for k, v in __previous_annotations__.items():
# using type(v) would just give me _GenericAlias this bypasses that...
orig = typing.get_origin(v)
if orig is Mapped:
cls.__annotations__[k] = typing.get_args(v)[0] I tried putting the remove_mapping_annotations in an Generally when I've wanted to do something to msgspec, I've ended up finding an elegant way to do something with it (thanks @jcrist for the exquisite library design!). So my thinking around this would be to have something like: from sqlalchemy.org import Mapped as _Mapped
from typing import Annotated
from msgspec import Meta
type Mapped[T] = Annotated[_Mapped[T], Meta(extra={"__table_field__": True}) If I implement it I'll let you all know. It doesn't solve the other issue msgspec has with encoding fields, where I have to deal with session persistence and relationship fields having to be awaited to get their values (with The lack of being able to omit fields that have Another avenue I'm considering is SQLActive. It's got a serialisation mixin here with a Hope it helps! |
@timinou The problem with using python-level things (At least for me) is that it kills the performance of many things. This is why I specifically tried researching into making a metaclass in Cython or Rust mainly so that maintenance would be a bit easier and so that the speed could be retained, it takes a lot longer to put together a CPython module than a Cython or Rust Module does. However you were right about everything
I'm already trying to lend the hand with finding the best solution. One Idea that I did not mention or forgot to was using cwpack in Cython and adding it in as either a wrapper or Mixin then handle all the
I do understand the C Implementation of this library but the hard part (at least for me that is) is figuring out where it could be fixed and then going in, recompiling it and testing it. There's a lot of ways to solve the problem but as it stands the only success I've had is sterilization. Let's not forget that its redundant to write a class twice which was what lead me down this large rabbit hole to start. |
The Reason why the json decoder and others are broken is because it calls for This is not a bad thing, the only other thing unrelated to this topic that I could really suggest is that more of the C Code could be separated into different C and Header files to spare any innocent developer or new maintainer trying to navigate through it's code in |
@timinou Unique update I may have figured out a way to fix Colums to prevent member stealing.
The Good NewsMsgspec is not at fault and SQLAlchemy is and that means that this bug has a better chance of being remedied as shown in my example. There might have been some black-magic involved with SQLAlchemy's code that makes that happen and thus subclassing it and experimenting with a Json Decoder with Hacked annotations has a chance of working as intended. |
Description
I am having a problem where when I want to write a Json Decoder for SQLAlchemy but then I bump into an issue where msgspec doesn't know how to handle these types.
I am completely stumped on how to fix this problem and I have gone through all hoops and hurtles to try and make the decoder for my upcoming python library
SQLTable
to decode json correctly.Other Things I've Tried
I have tried hacking the
Mapped[]
Object itself into an annotated object but then SQLAlchemy fails to find it...I am almost completely stumped on how this will get solved. But there are 2 fixes that I could think of that could be approched to solve this once and for all.
Annotated
types.typenode_collect_type
function where we could check if the class has a__table__
or another obscure type belonging to sqlalchemy and then try to filter outMapped[T]
types through that kind of Boolean/logic.The text was updated successfully, but these errors were encountered: