-
Notifications
You must be signed in to change notification settings - Fork 75
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
Class based actions: Mypy not happy with defined parameters in the run
method
#457
Comments
Thanks for the report! So this is an inherent mypy limitation/conscious decisiom. Mypy has no way of knowing that the fields are dynamically set by the def run(self,
state: State,
**kwargs) -> dict:
# can access the app_id from the __context
__context = kwargs["context"]
increment_by = kwargs["increment_by"]
print(__context.app_id, __context.partition_key, __context.sequence_id)
return {"counter": state["counter"] + increment_by} We chose not to have that as it's harder to read, and I'm less a believer in strict type-checking in python, but it's there if you want it! You may be able to do something with a typed dict/type-hinting kwargs as well -- haven't tested it out -- see https://peps.python.org/pep-0692/ |
Thanks for the response. For now have considered the options, I probably will just put |
OK, so I dug in a bit. This is not something we're going to be able to solve at a framework level but I think you have two good options:
from functools import wraps
from typing import Callable, Any
def adapt_types(func: Callable[..., Any]) -> Callable[..., Any]:
@wraps(func)
def wrapper(*args, **kwargs) -> Any:
return func(*args, **kwargs)
return wrapper
from burr.core import Action, State, ApplicationContext
class Counter(Action):
@property
def reads(self) -> list[str]:
return ["counter"]
@adapt_types
def run(self,
state: State,
increment_by: int,
# add __context here
__context: ApplicationContext) -> dict:
# can access the app_id from the __context
print(__context.app_id, __context.partition_key, __context.sequence_id)
return {"counter": state["counter"] + increment_by}
@property
def writes(self) -> list[str]:
return ["counter"]
def update(self, result: dict, state: State) -> State:
return state.update(**result)
@property
def inputs(self) -> list[str]:
# add __context here
return ["increment_by", "__context"] Happy to consider adding This does not work, it has the same error (understandibly so) https://peps.python.org/pep-0692/. |
For class-based actions, mypy is not happy with parameters to be defined in the method signature of the
run
method.Current behavior
I have copied the class-based action example from doc:
Then mypy is not happy with it with errors:
Library & System Information
The text was updated successfully, but these errors were encountered: