Skip to content
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

WIP - support pydantic #91

Merged
merged 5 commits into from
Feb 26, 2024
Merged

WIP - support pydantic #91

merged 5 commits into from
Feb 26, 2024

Conversation

PythonFZ
Copy link
Member

@PythonFZ PythonFZ commented Nov 22, 2023

This PR adds support for pydantic

Old

This PR adds tests for using ZnFlow with pydantic.
The main idea would be, to use class MyCls(BaseModel, znflow.Node) like I do it with zninit.ZnInit.
Alternative approaches are also possible, e.g. providing a znflow.pydantic.Node that already provides both.

The main incompatibility seems to be:

ZnFlow/znflow/node.py

Lines 117 to 132 in 3160d0b

def __new__(cls, *args, **kwargs):
try:
instance = super().__new__(cls, *args, **kwargs)
except TypeError:
# e.g. in dataclasses the arguments are passed to __new__
# but even dataclasses seem to have an __init__ afterwards.
# print("TypeError: ...")
instance = super().__new__(cls)
_mark_init_in_construction(cls)
instance.uuid = uuid.uuid4()
# Connect the Node to the Graph
graph = get_graph()
if graph is not empty_graph:
graph.add_node(instance)
return instance

But I can't tell for sure, if the __getattribute__ and __setattr__ are also affected.

One cause could be the usage of metaclass in pydantic which does not work nicely with overriding the __new__.
https://github.com/pydantic/pydantic/blob/9ab33eb82d78c55d1e66784cbd86107c1c41943d/pydantic/v1/main.py#L310

@PythonFZ PythonFZ marked this pull request as draft November 22, 2023 19:09
@PythonFZ PythonFZ added enhancement New feature or request help wanted Extra attention is needed labels Nov 22, 2023
@PythonFZ PythonFZ removed the help wanted Extra attention is needed label Feb 26, 2024
@PythonFZ PythonFZ marked this pull request as ready for review February 26, 2024 11:44
@PythonFZ PythonFZ merged commit 9724783 into main Feb 26, 2024
6 checks passed
@PythonFZ PythonFZ deleted the pydantic branch February 26, 2024 11:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant