Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added parametric control types * Demo of AbstractTerm change * Correct initial parametric control type implementation. * Parametric AbstractTerm initial implementation. * Update tests and fix hinting * Implement review comments. * Add parametric control check to integrator. * Update and test parametric control check * Introduce new LevyArea types * Updated Brownian path LevyArea types * Replace Union types in isinstance checks * Remove rogue comment * Revert _brownian_arch to single assignment * Revert _evaluate_leaf key splitting * Rename variables in test_term * Update isinstance and issubclass checks * Safer handling in _denormalise_bm_inc * Fix style in integrate control type check * Add draft vector_field typing * Add draft vector_field typing * Fix term test * Revert extemporaneous modifications in _tree * Rename TimeLevyArea to BrownianIncrement and simplify diff * Rename AbstractLevyReturn to AbstractBrownianReturn * Rename _LevyArea to _BrownianReturn * Enhance _term_compatiblity checks * Fix merge issues * Bump pre-commit and fix type hints * Clean up from self-review * Explicitly add typeguard to deps * Bump ruff config to new syntax * Parameterised terms: fixed term compatibility + spurious pyright errors Phew, this ended up being a complicated one! Let's start with the easy stuff: - Disabled spurious pyright errors due to incompatible between pyright and `eqx.AbstractVar`. - Now using ruff.lint and pinned exact typeguard version. Now on to the hard stuff: - Fixed term compatibibility missing some edge cases. Edge cases? What edge cases? Well, what we had before was basically predicated around doing ```python vf, contr = get_args(term_cls) ``` recalling that we may have e.g. `term_cls = AbstractTerm[SomeVectorField, SomeControl]`. So far so simple: get the arguments of a subscripted generic, no big deal. What this failed to account for is that we may also have subclasses of this generic, e.g. `term_cls = ODETerm[SomeVectorField]`, such that some of the type variables have already been filled in when defining it: ```python class ODETerm(AbstractTerm[_VF, RealScaleLike]): ... ``` so in this case, `get_args(term_cls)` simply returns a 1-tuple of `(SomeVectorField,)`. Oh no! Somehow we have to traverse both the filled-in type variables (to find that one of our type variables is `SomeVectorField` due to subscripting) *and* the type hierarchy (to figure out that the other type variable was filled in during the definition). Once again, for clarity: given a subscriptable base class `AbstractTerm[_VF, _Control]` and some arbitrary possible-subscripted subclass, we need to find the values of `_VF` and `_Control`, regardless of whehther they have been passed in via subscripting the final class (and are `get_args`-able) or have been filled in during subclassing (and require traversing pseudo-type-hierarchies of `__orig_bases__`). Any sane implementation would simply... not bother. There is no way that the hassle of figuring this out was going to be worth the small amount of type safety this brings... So anyway, after a few hours working on this *far* past the point I should be going to sleep, this problem this is now solved. This PR introduces a new `get_args_of` function, called as `get_args_of(superclass, subclass, error_msg_if_necessary)`. This acts analogous to `get_args`, but instead of looking up both parameters (the type variables we want filled in) and the arguments (the values those type variables have been filled in with) on the same class, it looks up the parameters on the superclass, and their filled-in-values on the subclass. Pure madness. (I'm also tagging @leycec here because this is exactly the kind of insane typing hackery that he seems to really enjoy.) Does anyone else remember the days when this was a package primarily concerned about solving differential equations? --------- Co-authored-by: Patrick Kidger <[email protected]>
- Loading branch information