Skip to content

Extend dataclass #17

@Sau1707

Description

@Sau1707

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions