Skip to content
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

anyio/Trio compatibility #134

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ Our design goals were roughly:

* Utilize type hints extensively as both a documentation and design by contract
tool to provide an expressive, readable API.
* Embrace asyncio as a first class citizen.
* Embrace anyio/asyncio/trio as a first class citizen.
* Implement state machines as plain old Python objects with an easily understood
method and input/output external API surface. Callers shouldn't need to know
or care about FSM minutiae.
Expand Down
5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pydantic = "^1.7.1"

[tool.poetry.dev-dependencies]
pytest = "^7.2.0"
pytest-asyncio = "^0.20.0"
anyio = "^4.1.0"
devtools = "^0.6.1"
pytest-mock = "^3.10.0"
isort = "^5.8.0"
Expand All @@ -38,6 +38,7 @@ add-trailing-comma = "^2.1.0"
pyformat = "^0.7"
pylint = "^2.6.0"
flake8-quotes = "^3.2.0"
trio = "^0.23.0"

[build-system]
requires = ["poetry-core>=1.0.0"]
Expand Down Expand Up @@ -66,4 +67,4 @@ mccabe = ["+*"]
[tool.flakehell.exceptions."statesman_test.py"]
pycodestyle = ["-*"]
pyflakes = ["-*"]
flake8-docstrings = ["-*"]
flake8-docstrings = ["-*"]
33 changes: 19 additions & 14 deletions statesman.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Statesman is a modern state machine library."""
from __future__ import annotations

import asyncio
import anyio
import collections
import contextlib
import datetime
Expand Down Expand Up @@ -119,10 +119,10 @@ async def __call__(self, *args, **kwargs) -> Any:
"""Call the action with the matching parameters and return the
result."""
matched_args, matched_kwargs = _parameters_matching_signature(self.signature, *args, **kwargs)
if asyncio.iscoroutinefunction(self.callable):
return await self.callable(*matched_args, **matched_kwargs)
else:
return self.callable(*matched_args, **matched_kwargs)
result = self.callable(*matched_args, **matched_kwargs)
if inspect.iscoroutine(result):
result = await result
return result

class Config:
arbitrary_types_allowed = True
Expand Down Expand Up @@ -163,17 +163,22 @@ def _get_actions(self, type_: Action.Types) -> List[Action]:
return list(filter(lambda c: c.type == type_, self._actions))

async def _run_actions(self, type_: Action.Types, *args, concurrently: bool = True, **kwargs) -> List[Any]:
results = []
if concurrently:
return await asyncio.gather(*(action(*args, **kwargs) for action in self._get_actions(type_)))
async with anyio.create_task_group() as tg:
async def _run(n,p):
results[n] = await p(*args, **kwargs)
for i,action in enumerate(self._get_actions(type_)):
results.append(None)
tg.start_soon(_run,i,action)
else:
results = []
for action in self._get_actions(type_):
result = await action(*args, **kwargs)
results.append(result)
if result == False:
if result is False:
break

return results
return results


class State(BaseModel):
Expand Down Expand Up @@ -1359,10 +1364,10 @@ async def _call_with_matching_parameters(callable: Callable, *args, **kwargs) ->
matched_args, matched_kwargs = _parameters_matching_signature(
inspect.Signature.from_callable(callable), *args, **kwargs
)
if asyncio.iscoroutinefunction(callable):
return await callable(*matched_args, **matched_kwargs)
else:
return callable(*matched_args, **matched_kwargs)
result = callable(*matched_args, **matched_kwargs)
if inspect.iscoroutine(result):
result = await result
return result


class HistoryMixin(pydantic.BaseModel):
Expand Down Expand Up @@ -1488,4 +1493,4 @@ def _state_entry(
try:
yield obj
finally:
obj._config.state_entry = original
obj._config.state_entry = original
Loading