Skip to content

Reuse computed csfield as a scratch property #46

Description

@dogtopus

In one of my projects I reused DataclassStruct as generic Python objects, with different DataclassStructs inheriting the same abstract class for common behaviors. One of the Structs however has data that cannot be parsed into a variable-length Python list losslessly, so I introduced a computed length field that pulls the length value from its parent, where that value is stored, and implemented the business-logic-related generic stuff defined in the abstract class using that length field. The problem is that when building using such Struct, with the older version of construct-typing, I get a partially generated init that does not allow me to pass values to the computed field, and with 0.8.x I still wasn't able to easily pass values to it since none of the newly introduced helpers allow me to define a computed field that can intentionally be initialized from Python side.

To get around this, I currently define a new property that automatically chooses values between a computed private csfield_noinit and a stock dataclass public field (created manually with subcon=Pass metadata) when it was read. It's ugly and I'm surprised that accessing that property from the parent Struct using this actually worked, but it mostly does what I want, however it would still be nice if the computed field can just be initialized with regular init, defined by a different helper than csfield of course.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions