Skip to content
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

Parametric type hinting for control paths #359

Open
tttc3 opened this issue Jan 24, 2024 · 1 comment
Open

Parametric type hinting for control paths #359

tttc3 opened this issue Jan 24, 2024 · 1 comment
Labels
refactor Tidy things up

Comments

@tttc3
Copy link
Contributor

tttc3 commented Jan 24, 2024

I would like to type hint that my custom term expects a control which has a certain type of path. E.G. only VirtualBrownianTree not UnsafeBrownianPath.

Ideally the code would look something like this:

from diffrax import AbstractTerm, ODETerm, WeaklyDiagonalControlTerm

class CustomTerm(AbstractTerm)
    ode: ODETerm
    cde: WeaklyDiagonalControlTerm[VirtualBrownianPath]

I'm not sure if this is already possible, or would require a change to the code. My thought was to do something like the following:

class _ControlTerm[T: Union[AbstractPath, Callable]](AbstractTerm):
    vector_field: ...
    control: T = ...

class WeaklyDiagonalControlTerm[T](_ControlTerm[T]):
    ...

I'm not very experienced with the more advanced aspects of type hinting so perhaps there is a better way, or this is already possible? Any thought are much appreciated

@patrick-kidger
Copy link
Owner

patrick-kidger commented Jan 29, 2024

This seems reasonable to me! I'd be happy to take a PR adding this. Note that we don't yet insist on Python 3.12 and above, so I think we'd annotate this as something like

_Control = TypeVar("_Control", bound=AbstractPath)

class _ControlTerm(AbstractTerm, Generic[_Control]):
    control: _Control = ...

    def __init__(self, ..., control: Union[AbstractPath, Callable]):
        self.control = _callable_to_path(control)

@patrick-kidger patrick-kidger added the refactor Tidy things up label Jan 29, 2024
@patrick-kidger patrick-kidger mentioned this issue Jan 30, 2024
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
refactor Tidy things up
Projects
None yet
Development

No branches or pull requests

2 participants