514-labs / moose

The developer framework for your data & analytics stack
https://www.moosejs.com
MIT License
20 stars 4 forks source link

Tim/514 918 python data models #1140

Open tg339 opened 3 weeks ago

linear[bot] commented 3 weeks ago

514-918 python data models

vercel[bot] commented 3 weeks ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
company-landing ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 2, 2024 11:20pm
framework-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 2, 2024 11:20pm
framework-landing ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 2, 2024 11:20pm
moose-product-analytics ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 2, 2024 11:20pm
phiSgr commented 6 days ago

Since the types are retained in Python runtime (just ignored by the interpreter), maybe we should, instead of interring the semantics from the syntax, import the user's file then do typing.get_type_hints.

E.g.

>>> import simple
>>> dir(simple)
['Enum', 'Key', 'List', 'MyModel', 'Optional', 'Status', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'dataclass', 'field']
>>> type(getattr(simple, 'Status'))
<class 'enum.EnumType'>
>>> [element for element in getattr(simple, 'Status')]
[<Status.OK: 'ok'>, <Status.ERROR: 'error'>]
>>> type(getattr(simple, 'MyModel'))
<class 'type'>
>>> import typing
>>> typing.get_type_hints(getattr(simple, 'MyModel'))
{'name': Key[str], 'age': <class 'int'>, 'flag': <class 'bool'>, 'status': <enum 'Status'>, 'test_key': <class 'str'>, 'arr': typing.List[str], 'opt': typing.Optional[str]}
>>>

We can do sth very similar to how we read the data model config in ts.