delvtech / agent0

Analysis & simulation repo for Delv
https://agent0.readthedocs.io/en/latest/
Apache License 2.0
49 stars 21 forks source link

type narrowing for agents and policies using generics #1443

Open wakamex opened 4 months ago

wakamex commented 4 months ago

implement type narrowing all the way down to the lowest agent and policy level, through the use of generics right now policies are typed as only a very high-level NoPolicy | BasePolicy which isn't useful at all

dpaiton commented 4 months ago

here's an example of where the type narrowing is not correct image This type is incorrect, which means I can't auto-complete any of the random_hold_policy functions or discover the arguments.

and another for config: image

these should be random hold policy & random hold policy config, which would have correct autocomplete.

I have a TODO in base.py:

# TODO: We add this as a helper property so that subclasses can access the config without
# overwriting the __init__ function. The downside is that users of this member variable
# can't be type checked. There's probably a way to do this with generics instead of Any.

This hints that the solution is to use Generics, which I still think is correct. We need to revamp IHyperdrive to also use generics for this all to work.

The meta issue is that the python type system is fine with you narrowing the type to a base class, which removes all the metadata and content. So while it's not returning any type errors, it is not the behavior we want.

dpaiton commented 3 months ago

Move to medium priority bc the policy attribute is intended to be private & not accessed by the user.