pytransitions / transitions

A lightweight, object-oriented finite state machine implementation in Python with many extensions
MIT License
5.49k stars 525 forks source link

mypy support? #574

Closed Henddher closed 1 year ago

Henddher commented 2 years ago

Hi, great library!!!!

Any plans to add support for mypy?

I'm using the library but missing types trigger this error

error: Skipping analyzing "transitions": module is installed, but missing library stubs or py.typed marker  [import]
jellyfish-tech commented 1 year ago

Same for us, but with asyncio extension. Really looking forward to mypy support

aleneum commented 1 year ago

transitions 0.9.0 has been published to PyPI and include typing stub files. Since transitions allows rather arbitrary argument types in functions and constructors there might be some false positive typing warnings. I personally would expect false negatives though. My goal for 0.9.0 was to satisfy mypy in 'common' usage scenarios without extensive typing of state and transition definitions. Since nested configurations are often not accurately typed by mypy I opted for rather generic fallback types. Depending on your IDE this might lead to a vast amount of suggested type options. I am looking forward to community feedback to adjust this in upcoming releases.

For now I hope this makes your work slightly easier.

Henddher commented 1 year ago

Thank you @aleneum

Will have to update to 0.9.0 and see how it plays out.

vaidik commented 8 months ago

I am using 0.9.0 but cant seem to have type definitions work for trigger methods that are generated at run time

aleneum commented 8 months ago

Hello @vaidik,

cant seem to have type definitions work for trigger methods that are generated at run time

as far as I know this cannot be done with a static code analysis and type annotation. DocMachine might be useful. Alternatively, you could define your trigger methods on your model in advance for IDEs and code checkers to pick up and use OverrideMachine to allow transitions to override these methods during runtime. If you have a more elegant solution to deal with type checking and dynamic methods/attributes let me know.