Skip to content

Clean up frozen-instance code #35

Closed

Description

@Technologicat

See dataclasses, new in Python 3.7. (This pretty much fills the same role as MacroPy's case classes.)

We could raise dataclasses.FrozenInstanceError where appropriate.

Also, e.g. unpythonic.llist.cons doesn't need the internal readonly flag; it's simpler to just call object.__setattr__ to init the read-only fields, so the custom __setattr__ can be simplified. (Should check also other datatypes that use this pattern, maybe something in unpythonic.collections?)

Also, must intercept also __delattr__ to properly emulate immutability. Not sure if we currently do this (probably not - hence the bug label, we need to check).

Activity

  1. added
    enhancement
    on Sep 25, 2019
  2. added this to the 0.15 milestone on Sep 25, 2019
  3. added
    bug
    on Sep 25, 2019
  4. modified the milestones: 0.15, 0.15.x on Apr 12, 2021
  5. modified the milestones: 1.x, 2.2.0 on Apr 24, 2026
  6. Technologicat commented on Apr 24, 2026

    @Technologicat
    OwnerAuthor

    Investigation (2026-04-24):

    Confirmed the __delattr__ gap is real. Demonstration:

    from unpythonic import cons
    c = cons(1, 2)
    c.car = 99   # blocked: TypeError "'cons' object does not support item assignment"
    del c.car    # succeeds silently — attribute removed from supposedly-immutable instance

    Current implementation at unpythonic/llist.py:218-225 intercepts only __setattr__. A subsequent del c.car slips through object.__delattr__.

    Survey of the rest of the codebase:

    • unpythonic/env.py already intercepts both __setattr__ and __delattr__ (env is mutable but with controlled mutation) — good model.
    • unpythonic/assignonce.py intercepts only __setattr__ and is by design allowed to set once; never rebinding means del is also semantically wrong there. Should likely also intercept __delattr__.
    • unpythonic/collections.py:388 is the shim proxy, not an immutable — not in scope.
    • frozendict is a dict subclass; __setitem__/__delitem__ are the right hooks (already handled).

    Plan for 2.2.0:

    • cons: intercept __delattr__ symmetrically; use object.__setattr__ at init time to drop the _immutable sentinel dance; raise dataclasses.FrozenInstanceError instead of bare TypeError for stdlib parity.
    • assignonce: intercept __delattr__ (raise AttributeError with an explanatory message, since "set once" implies "no unset").
    • Add regression tests covering del on both.

    — Claude (Opus 4.7)

  7. Technologicat commented on May 6, 2026

    @Technologicat
    OwnerAuthor

    Closed for 2.2.0. Five commits on master:

    • ee66ff7 llist: cons __delattr__ + simplify via object.__setattr__ — bug fix (was missing) plus simplification (drop _immutable sentinel, init via object.__setattr__).
    • 95c05fb assignonce: forbid del on defined names — closes the del e.foo; e.foo = new_value bypass that was inherited from env.
    • 6c79667 env: drop _direct_write whitelist — same object.__setattr__ cleanup pattern; closes the gap where client code could clobber _env / _finalized.
    • dae7c30 collections: clarify frozendict freezes mapping not attributes — docstring note (intentional, not a bug).
    • 2798d86 llist: FrozenAttributeError shim, raised by cons — multiply-inherits from TypeError (legacy) and dataclasses.FrozenInstanceError (stdlib convention since 3.7). Either except clause catches.

    The TypeError base of the shim is scheduled for removal in 3.0.0; tracked in #102.

    Tests: 3767/3767 pass locally; CI matrix green.

    — Claude (Opus 4.7)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugenhancement

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions