-
Notifications
You must be signed in to change notification settings - Fork 130
Add C-API Capsule #961
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
base: main
Are you sure you want to change the base?
Add C-API Capsule #961
Conversation
|
I'm going to try working on encoders today for a bit as well as planning out how users should subclass the encoder objects possibly using the C Structure and build around it as well as the writing in of bytes & bytearray types. |
|
It would be nice if you could provide some documentation on how to use this feature. I'm surely curious myself |
…r optionally compiling _testcapi (Should disable upon distribution)
@jacopoabramo I got around to making the |
|
I'm going to fix |
|
@jacopoabramo I added |
|
I'm a little bit stumped on figuring how how to properly subclass class TableMeta(StructMeta):
__struct_fields__: ClassVar[tuple[str, ...]]
__struct_config__: ClassVar[StructConfig]
__match_args__: ClassVar[tuple[str, ...]]
# Idea I had after working with SQLModel for a very long time...
__table__: ClassVar[str | None]
def __new__(cls, name: str, bases: tuple[type, ...], namespace: dict[str, Any], /, **kwds) -> None:
table = kwds.pop("table", None)
new_cls = StructMeta.__new__(cls, name, bases, namespace, **kwds)
if table is not None:
if isinstance(table, str):
new_cls.__table__ = table
elif isinstance(table, bool):
if table is True:
new_cls.__table__ = new_cls.__name__.lower()
else:
new_cls.__table__ = None # ignore as table is simply Abstract...
else:
new_cls.__table__ = None
return new_cls
class Table(metaclass=TableMeta):
pass
class MyTable(Table, table=True):
pass
class NamedTable(Table, table="My Named Table"):
pass |
|
Today I will attempt to resume this PR and I think I have an idea about how to approach adding new variables to StructMeta without it crashing involving the subclass of StructMixin perhaps? |
|
Did you manage to make it work? |
|
No I am having difficulties with subclassing the metaclass and making something with it in C without it crashing on me. If someone could figure out how to subclass StructMeta without the code crashing feel free to let me know. I have been mostly stumped at this point in time. |
|
@ofek Good News I found a suitable solution for my problem EDIT: (Bad news: Segfaults on linux on Windows on the otherhand it works) from msgspec import _testcapi
from msgspec import StructMeta, Struct
class Table(Struct, _testcapi.TableMixin, metaclass=_testcapi.TableMeta):
pass
class XY(Table, table=True):
x:int
y:str
t = XY(4, 'x')
print(t.__table_name__)
print(t.__abstract_table__)This code now runs successfully. |
|
@Vizonex could you run locally the pre-commit hooks? it would probably re-trigger the CI so it can evaluate if tests pass or not. it's a really could feature to have in the future this one |
@jacopoabramo After that I'll document all the functions I've added and update the documentation if I get any bit of extra downtime. |
Warning
This pull request is a still a being worked on and is based off previous work I did for a different library based off #958
I have not added a whole lot yet in order to be sure that this is what other maintainers had in mind otherwise I will simply wait for input. It should be noted that this concept I had was based off unfinished work I had previously done for another library but works nevertheless.
Important
It should also be noted that getting this implemented will take time and planning on what the best approach would be to take and to do it all slow and steady. I have not implemented more objects besides Fields and Factories because I wanted to ensure that what gets added makes sense and is in the best interest of the maintainers in order to not make the process anymore stressful than it may already be to maintain.
Edit: Here is the objects I am planning to add to the C-API
Checklist
StructMetaTypeSafely in C (Crashes for me upon attempting to bind atPyType_Type.tp_new(...))Field is named MsgspecFieldObjectinmsgspec.hso when someone compiles the code something Generic likeField(which could theoretically be used in a different C library or another Dev's implementation) it isn't being mixed and causing developers headaches and compilation issues.fixes #958