Skip to content

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

Closed

Description

@comalice
  • 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

Activity

  1. comalice commented on Jan 14, 2025

    @comalice
    ContributorAuthor

    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

  2. fgmacedo commented on Jan 16, 2025

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

  3. comalice commented on Jan 20, 2025

    @comalice
    ContributorAuthor

    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.

  4. mgineer85 commented on Feb 26, 2025

    @mgineer85

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

  5. mgineer85 commented on Feb 26, 2025

    @mgineer85

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

  6. fgmacedo commented on Feb 26, 2025

    @fgmacedo
    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.

  7. mgineer85 commented on Feb 26, 2025

    @mgineer85

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

  8. fgmacedo commented on Feb 26, 2025

    @fgmacedo
    Owner

    How is the confirmdeclared?

  9. mgineer85 commented on Feb 28, 2025

    @mgineer85

    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)
    
  10. fgmacedo commented on Mar 1, 2025

    @fgmacedo
    Owner

    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))
    
  11. mgineer85 commented on Mar 2, 2025

    @mgineer85

    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!

  12. added a commit that references this issue on Dec 26, 2025
    fd45edd
  13. rodrigobnogueira commented on Dec 26, 2025

    @rodrigobnogueira
    Contributor

    Hi! I'm a user of python-statemachine in some of my projects. I've submitted PR #545 that implements Generic[TModel] support to address this issue. The implementation adds type safety for the model attribute while maintaining full backward compatibility.
    I'd appreciate any feedback on the approach!

  14. added a commit that references this issue on Feb 15, 2026
    2e9bc86
  15. added a commit that references this issue on Feb 17, 2026
    a214e04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions