-
-
Notifications
You must be signed in to change notification settings - Fork 86
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
Argument missing for parameter "f" Pylance [reportCallIssue] #511
Comments
The static code analyzer is tracing the Example code: from statemachine import State, StateMachine
class PylanceWarningStateMachine(StateMachine):
init = State(initial=True)
running = State()
completed = State(final=True)
run = (
init.to(running)
)
complete = (
running.to(completed)
)
if __name__ == "__main__":
sm = PylanceWarningStateMachine()
sm.run()
sm.complete()
class AddCallbacksMixin:
...
def __call__(self, f):
return self._add_callback(f, CallbackGroup.ON, is_event=True)
... I verified by renaming the Modified code: class AddCallbacksMixin:
...
def __call__(self, itsThisF):
return self._add_callback(itsThisF, CallbackGroup.ON, is_event=True)
... |
Hi @kler, thanks for taking the time to report this. I was able to reproduce the issue by just adding Context of the problemWhen a transition is declared, it can be assigned to a class-level attribute, which later will be used as an In your example (see the comments): from statemachine import State
from statemachine import StateMachine
class PylanceWarningStateMachine(StateMachine):
init = State(initial=True)
running = State()
completed = State(final=True)
run = init.to(running)
complete = running.to(completed)
# At this point, when the body of the `StateMachine` is being evaluated,
# the `run` and `complete` attributes are still instances of `TransitionList` returned by
# the `init.to(running)` and `running.to(completed)` calls.
print("Type of `run` at class body is ", type(run))
# At this point, the `PylanceWarningStateMachine` body was fully evaluated, and transformed
# by the `StateMachineMetaclass`, so now `run` and `complete` are instances of `Event`.
print("Type of `run` after the final class evaluation is ", type(PylanceWarningStateMachine.run))
if __name__ == "__main__":
sm = PylanceWarningStateMachine()
sm.run()
sm.complete() Outputs: ❯ uv run tests/examples/types_machine.py
Type of `run` at class body is <class 'statemachine.transition_list.TransitionList'>
Type of `run` after the final class evaluation is <class 'statemachine.event.Event'> We say So when the body of a The IDE and the Pyright are unable to follow this processing and still think that The run = (
init.to(running)
)
@run # <- "calling" a `TransitionList` allows registering action callbacks
def do_run(self):
print("running!") FixSince these tools don't "run" the code, AFAIK there's no way to teach them that the result is an from .i18n import _
def __call__(self, *args, **kwargs) -> Any:
if len(args) == 1 and callable(args[0]):
return self._add_callback(args[0], CallbackGroup.ON, is_event=True)
raise ValueError(
_("Unsupported call signature. Calling %s only to register callbacks").format(
self.__class__.__name__
)
) You can apply this monkey patch until a new release arrives. Please let me know if it worked. |
I've verified and it works beautifully! |
Nice! Another and maybe better approach could be to use the explicit from statemachine import State, StateMachine, Event
class PylanceWarningStateMachine(StateMachine):
init = State(initial=True)
running = State()
completed = State(final=True)
run = Event(
init.to(running)
)
complete = Event(
running.to(completed)
)
if __name__ == "__main__":
sm = PylanceWarningStateMachine()
sm.run()
sm.complete() |
That's a good suggestion as well, though I do like the raw beauty of run = ( ... ) 🤓 If we could get your first fix into the repo that would be a great 🎁 |
2.5.0
Python 3.13.1
(also reproduced inPython 3.10.12
)Version: 1.96.0 (Universal)
Commit: 138f619c86f1199955d53b4166bef66ef252935c
Date: 2024-12-11T02:29:09.626Z
Electron: 32.2.6
ElectronBuildId: 10629634
Chromium: 128.0.6613.186
Node.js: 20.18.1
V8: 12.8.374.38-electron.0
OS: Darwin arm64 24.1.0
Description
I apologize in advance if this is outside scope of this project.
Since
python-statemachine v2.5.0
VS Code displays a warning on lines executing transitions:The warning is:
I haven't figured out how Pylance gets to this conclusion. The
f
must come fromstatemachine/transition_mixin.py
, which is the only file referencing a parameter namedf
.What I also have noticed is that this warning doesn't appear in a VS Code vanilla configuration, but only after certain settings are made.
For instance adding this to the project's
pyproject.toml
will make VS Code display the warning:In fact, just add this line will make VS Code display the warning:
Again, perhaps this is a bug in Pylance / Pyright for what I know, but the warning did not appear in
v2.4.0
. So in case you have an idea how to mitigate it, by changing the code or IDE setup, please share your thoughts :)The text was updated successfully, but these errors were encountered: