python-statemachine
python-statemachine copied to clipboard
Development Bug: pyright and other language servers cannot identify unresolved references on subclasses of StateMachine
- Python State Machine version: 2.5.0
- Python version: 3.12
- Operating System: Windows 11
Description
As in the title, when StateMachine is subclassed, no unresolved references are found by pyright or similar language servers.
What I Did
In the following example code, the class without inheritance is flagged correctly as having an unresolved reference. The class with inheritance is not flagged correctly.
from statemachine import StateMachine
class MyStatemachine(StateMachine):
def __init__(self):
super().__init__()
self.this_method_does_not_exist()
class MyNotStatemachine:
def __init__(self):
self.this_method_does_not_exist()
And resolution of functions not in the subclass are correctly analyzed:
from statemachine import StateMachine
def a_function_from_an_outer_scope():
return True
class MyStatemachine(StateMachine):
def __init__(self):
super().__init__()
self.this_method_does_not_exist()
this_method_also_does_not_exist()
a_function_from_an_outer_scope()
class MyNotStatemachine:
def __init__(self):
self.this_method_does_not_exist()
this_method_also_does_not_exist()
a_function_from_an_outer_scope()
Hi @comalice , thanks for reporting this!
I think that this is the source of this issue: https://github.com/fgmacedo/python-statemachine/blob/995489040adcf1657d953eedefdd06c0a8552600/statemachine/statemachine.py#L118-L121
It was added to make mypy stop complaining about dynamic-created state machine attributes.
We need to figure out another and better way, or I will forever be trapped between choosing which linter error is displayed :(
I'll see if I can poke around and figure something out. There are mypy stub generators (used on peewee, for example) that might be useful here.
Hey, any idea how to solve this issue? Started using pyright recently and run into the same issue :)
Also there is a linting error about the name attribute of an event:
Also there is a linting error about the name attribute of an event:
Hi @mgineer85,
For this last case, you can make the linter happy by using explicit Event class when declaring an event.
For the first case, there's still no easy solution as the library uses dynamic properties of the language and the linters stuck to get it right because they don't run the code for real.
Oh, sorry, the error is about the TransitionList self.confirm. The event is actually fine.
I will ignore the whole file for now and might revisit this later.
Thank you anyways :)
How is the confirmdeclared?
Definition like this
idle = State(initial=True)
counting = State() # countdown before capture
capture = State() # capture from camera include postprocess single img postproc
multicapture = State() # capture from multicapture backend
record = State() # record from camera
approve_capture = State() # waiting state to approve. transition by confirm,reject or autoconfirm
captures_completed = State() # final postproc (mostly to create collage/gif)
present_capture = State() # final presentation of mediaitem
finished = State(final=True) # final state
## TRANSITIONS
start = idle.to(counting)
_counted_capture = counting.to(capture)
_counted_record = counting.to(record)
_counted_multicapture = counting.to(multicapture)
_captured = capture.to(approve_capture) | multicapture.to(approve_capture)
confirm = approve_capture.to(counting, unless="all_captures_done") | approve_capture.to(captures_completed, cond="all_captures_done")
reject = approve_capture.to(counting)
abort = approve_capture.to(finished)
stop_recording = record.to(captures_completed)
_present = captures_completed.to(present_capture)
_finish = present_capture.to(finished)
You can use explicit Event declaration, it wraps the transition list and makes the linter happy :)
from statemachine import Event
...
start = Event(idle.to(counting))
Thank you! Declaring the Events explicitly fixed both of the linting errors I had:
processing.py:487:32 - error: Cannot access attribute "name" for class "TransitionList"
Attribute "name" is unknown (reportAttributeAccessIssue)
processing.py:507:9 - error: Argument missing for parameter "f" (reportCallIssue)
Thank you!