Skip to content

[FR] Decorator to automate field aliasing #56

@NiklasRosenstein

Description

@NiklasRosenstein

The snake_case convention is common for Python code, but that makes modeling data structures using another convention more difficult. There are two approaches one could take:

  1. Name the dataclass fields exactly like in the data structure
  2. Annotate each field with the databind Alias() setting

Often these naming conventions can be converted to and from using a simple rule-set however. It would be nice to have a decorator that can be applied to a class that injects the Alias() settings for each field, deriving the alias by some specified rule.

Example

from dataclasses import dataclass
from databind.core import field_auto_aliasing

@dataclass
@field_auto_aliasing("camelCase")
class MyStruct:
    some_field: int  # Behaves as if we used Annotated[int, Alias("someField")]

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions