If you'd like to extend the @dataclass decorator to allow to add more settings to the table class itself, for example move the "pk='name'" in the class decorator, you can do that by using dataclass_transform imported from the typing module, for instance
from dataclasses import dataclass
from typing import dataclass_transform
@dataclass_transform()
def useTable(pk: str):
def decorator(cls):
cls = dataclass(cls)
cls.__pk__ = pk
return cls
return decorator
@useTable("table_1")
class Example:
name: str
age: int
ex1 = Example("John", 30)
print(ex1)
# Output: Example(name='John', age=30)
Plus ex1 has all the type hints in the editor as the normal dataclass does.
I was plaing around with this a few months ago but never had time to finish, feel free to take a look and get inspired
https://github.com/Sau1707/ezstorage
If you'd like to extend the
@dataclass
decorator to allow to add more settings to the table class itself, for example move the "pk='name'" in the class decorator, you can do that by usingdataclass_transform
imported from thetyping
module, for instancePlus ex1 has all the type hints in the editor as the normal dataclass does.