-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Typing ModelStage
#23
Comments
When the return annotation
|
Typing of all the arguments is working now. Two things needed to be done:
For example, here's the updated Lines 168 to 203 in 0c6e824
Making this change for all of the Unfortunately, the use of |
Being uncertain about potential alternatives to my solution, I'm leaving this issue open for now. |
State operations ("stages") in an
AbstractStagedModel
are defined by the propertymodel_spec
which is anOrderedDict[str, ModelStage]
.For example, here's one entry from
SimpleFeedback.model_spec
:feedbax/feedbax/bodies.py
Lines 195 to 199 in 2ce8b1c
The
state
arguments in theselambdas
should be typed asSimpleFeedbackState
, so that the type checker recognizes thatstate.mechanics
is a valid reference:SimpleFeedbackState
has a fieldmechanics: MechanicsState
.Currently,
ModelStage
is a generic of the type variableStateT = TypeVar('StateT', AbstractState, Array)
, where all of the state PyTrees likeSimpleFeedbackState
inherit fromAbstractState
. Here is a slight simplification:However, nowhere do we subclass
ModelStage
and give an argument for this type variable.Throughout Feedbax, Pyright raises errors for only some of the references found in
lambdas
ofmodel_spec
properties. For example, it raises an error for thecallable
field of the"mechanics_step"
stage given at the start of this issue,but not for its
where_input
orwhere_state
fields. Checking the Pylance tooltips, theinput
arguments are typed asAbstractTaskInputs
, but thestate
fields areAny
, which is at odds with theStateT
annotation inModelStage
.I suspect it will be necessary (#24 ) to eliminate
AbstractState
. Then, the type arguments to final subclasses ofAbstractStagedModel
will need to be distinct subclasses ofequinox.Module
. Similarly, the type variable inModelStage
will need to be bound toequinox.Module
instead ofAbstractState
. This doesn't seem problematic asModelStage
will only be used inside anAbstractStagedModel
with which it shares a type argument. For example,ModelState[SimpleFeedbackState]
will be used inSimpleFeedback[SimpleFeedbackState]
.I've tried explicitly writing
"mechanics_step": ModelStage[SimpleFeedbackState](...)
in themodel_spec
entries. Writing type arguments in at the time of instantiation is not a syntax I've ever used before, and it doesn't resolve the type checker's errors. I wouldn't expect this to resolve the error raised for thecallable
field ofModelStage
, since the type argument won't specify thatself
should be of typeSimpleFeedback
.How should this be properly typed?
The text was updated successfully, but these errors were encountered: