Skip to content

refactor: implement active logic hooks #7

Merged
ymahlau merged 2 commits into
ymahlau:mainfrom
sIHURs:attribute-hooks-impl
Mar 30, 2026
Merged

refactor: implement active logic hooks #7
ymahlau merged 2 commits into
ymahlau:mainfrom
sIHURs:attribute-hooks-impl

Conversation

@sIHURs

@sIHURs sIHURs commented Mar 20, 2026

Copy link
Copy Markdown
Contributor

This PR implements on_setattr and on_getattr hook functionality for DataClass fields. Hooks and attribute changes are only allowed during the initialization stage. After the object is fully instantiated, static fields become strictly read-only (frozen).

In fdtdx/objects/object.py:

@autoinit
class SimulationObject(TreeClass, ABC):
    ...
    name: str = frozen_field(  # type: ignore
        default=None,
        on_setattr=[UniqueName()],
    )
    ...

and now adopt the drinx library:

class UniqueName(DataClass):
    def __call__(self, x: str | None) -> str:
        print("hello from uniquename")
        global _GLOBAL_COUNTER
        if x is None:
            name = f"Object_{_GLOBAL_COUNTER}"
            _GLOBAL_COUNTER += 1
            return name
        return x

class SimulationObject(DataClass, ABC):
    ...
    name: str = static_field(
        default=None,
        on_setattr=(UniqueName(),), 
    )
    ...

@ymahlau

ymahlau commented Mar 24, 2026

Copy link
Copy Markdown
Owner

@cubic-dev-ai review

@ymahlau

ymahlau commented Mar 24, 2026

Copy link
Copy Markdown
Owner

Hi @sIHURs, the code looks mostly good to me! Only the CI-pipeline is failing due to some small type errors, could you fix that please?

@ymahlau

ymahlau commented Mar 24, 2026

Copy link
Copy Markdown
Owner

One minor comment regarding the example you provided, I dont think it is necessary to have UniqueName(DataClass) be a subclass of DataClass. This could just be a simple function when you implement this feature in fdtdx.

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 68.18182% with 28 lines in your changes missing coverage. Please review.
✅ Project coverage is 86.47%. Comparing base (6cb3e8f) to head (9639106).

Files with missing lines Patch % Lines
src/drinx/base.py 65.85% 23 Missing and 5 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main       #7      +/-   ##
==========================================
- Coverage   91.03%   86.47%   -4.56%     
==========================================
  Files           6        6              
  Lines         368      451      +83     
  Branches       84       96      +12     
==========================================
+ Hits          335      390      +55     
- Misses         16       39      +23     
- Partials       17       22       +5     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ymahlau ymahlau merged commit fa3bc32 into ymahlau:main Mar 30, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants