Skip to content

Development Bug: pyright and other language servers cannot identify unresolved references on subclasses of StateMachine #515

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

Open
comalice opened this issue Jan 14, 2025 · 11 comments

Comments

@comalice
Copy link
Contributor

  • 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()

image

@comalice
Copy link
Contributor Author

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()

image

@fgmacedo
Copy link
Owner

Hi @comalice , thanks for reporting this!

I think that this is the source of this issue:

if TYPE_CHECKING:
"""Makes mypy happy with dynamic created attributes"""
def __getattr__(self, attribute: str) -> Any: ...

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 :(

@comalice
Copy link
Contributor Author

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.

@mgineer85
Copy link

Hey, any idea how to solve this issue? Started using pyright recently and run into the same issue :)

@mgineer85
Copy link

Also there is a linting error about the name attribute of an event:
Image

@fgmacedo
Copy link
Owner

Also there is a linting error about the name attribute of an event: Image

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.

@mgineer85
Copy link

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 :)

@fgmacedo
Copy link
Owner

How is the confirmdeclared?

@mgineer85
Copy link

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)

@fgmacedo
Copy link
Owner

fgmacedo commented Mar 1, 2025

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))

@mgineer85
Copy link

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants