Enhance Spina class typing and dynamic attribute method support.

Review Request #13222 — Created Aug. 17, 2023 and discarded — Latest diff uploaded

Information

Spina
master

Reviewers

This change improves typing support throughout Spina, helping to cement
some patterns, improve code maintenance, and correct typing issues in
derived classes.

SpinaClass is now a generic (SpinaClass<TBase>), which takes in the
class being typed, merges with it, and sets the prototype to it. This
replaces the SpinaClass & TBase we had internally in several places,
and gives us a proper up-front type to avoid some casting. Untyped, it
behaves like it did before.

__super__ is now mapped to any instead of object, avoiding type
errors when looking for attributes/functions on parents.

Backbone's _Result<T> is now exported in Spina as Result<T>, giving
us a non-private, stable type we can use when we need to reference a
type or callable returning the type.

And then, finally, we have a fix for defining methods for Result<T>,
without having to suppress errors. Subclasses that define methods for
one or more dynamic Result<T> attributes can now inherit from
DynamicAttrs(BaseClass, attr1, attr2, ...). At runtime, this is a
simple pass-through function that just returns BaseClass. During type
checking, it carefully omits the provided attributes from the parent and
allows the child to define them as methods, which will then be seen in
that class's typing.

This helps avoid all sorts of issues. By default, these attributes are
seen as non-methods, and must be treated as such. But when using
DynamicAttrs, they're seen as methods explicitly, and
subclasses/callers must treat them as such.

Unit tests pass.

Tested this with some in-progress work.

Commits

Files