Skip to content

One family for the kinds of object ObjectState can hold (function, dataclass, plain class) #36

Open

Description

@trissim

All line numbers are at main 1fcdf12.

Problem

ObjectState's goal is to hold state for almost any object: a dataclass instance, a plain class instance, a function. Each of those is the same thing, a declaration of named, typed parameters with defaults. But there is no type for "a kind of declaration". ParameterOwner (parameter_owner.py) is the alias type | Callable[..., Any]; it names two cases and owns no behaviour.

So the question "what kind of thing am I holding" is answered again at each place that needs it. A search for is_dataclass(, callable(, FunctionType, is_function and inspect.is* finds 70 checks in src/objectstate: 28 in object_state.py, 16 in context_manager.py, 14 in lazy_factory.py. For example extraction branches on isinstance(obj, FunctionType) to choose where values come from (object_state.py:2507-2534) and reconstruction branches on it again (:2696).

Two consequences:

  • Supporting a new kind of object means finding and editing those sites.
  • "Callable" currently means a plain function. The check is FunctionType, so a functools.partial, a bound method, a builtin or an instance with __call__ takes the instance path, where its parameters are whatever is found on the object and not its call signature. (Read from the source; I have not run these cases.)

Wanted

One family, one subclass per kind of declaration (function, dataclass, plain class, and whichever other callables are meant to be supported). Each subclass owns:

  • where current values are read from (signature defaults or stored attributes),
  • where defaults come from,
  • how the object is rebuilt from edited values,
  • how nested declarations are found.

ObjectState asks the declaration for these and contains no kind checks. The decision of which subclass applies is made once, when the state is constructed.

Acceptance

Count the edits needed to support functools.partial before and after: after, it is one new subclass and no change to ObjectState. The 70 checks drop to the ones inside the family plus value-level checks that are about field values, not about the held object. Tests cover a function, a partial, a bound method, a dataclass and a plain class through the same assertions.

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions