From a9bb4a7122df6d9d4a764de52244c6ec75789ead Mon Sep 17 00:00:00 2001 From: Paolo Melchiorre Date: Mon, 5 Dec 2022 18:38:37 +0100 Subject: Add pyupgrade with --py310-plus in pre-commit (#103) --- stator/graph.py | 23 ++++++++++++----------- stator/management/commands/runstator.py | 6 +++--- stator/models.py | 18 +++++++++--------- stator/runner.py | 5 ++--- 4 files changed, 26 insertions(+), 26 deletions(-) (limited to 'stator') diff --git a/stator/graph.py b/stator/graph.py index 5c71d4a..0ec5ee7 100644 --- a/stator/graph.py +++ b/stator/graph.py @@ -1,4 +1,5 @@ -from typing import Any, Callable, ClassVar, Dict, List, Optional, Set, Tuple, Type +from collections.abc import Callable +from typing import Any, ClassVar class StateGraph: @@ -7,11 +8,11 @@ class StateGraph: Does not support subclasses of existing graphs yet. """ - states: ClassVar[Dict[str, "State"]] - choices: ClassVar[List[Tuple[object, str]]] + states: ClassVar[dict[str, "State"]] + choices: ClassVar[list[tuple[object, str]]] initial_state: ClassVar["State"] - terminal_states: ClassVar[Set["State"]] - automatic_states: ClassVar[Set["State"]] + terminal_states: ClassVar[set["State"]] + automatic_states: ClassVar[set["State"]] def __init_subclass__(cls) -> None: # Collect state members @@ -84,8 +85,8 @@ class State: def __init__( self, - try_interval: Optional[float] = None, - handler_name: Optional[str] = None, + try_interval: float | None = None, + handler_name: str | None = None, externally_progressed: bool = False, attempt_immediately: bool = True, force_initial: bool = False, @@ -95,10 +96,10 @@ class State: self.externally_progressed = externally_progressed self.attempt_immediately = attempt_immediately self.force_initial = force_initial - self.parents: Set["State"] = set() - self.children: Set["State"] = set() + self.parents: set["State"] = set() + self.children: set["State"] = set() - def _add_to_graph(self, graph: Type[StateGraph], name: str): + def _add_to_graph(self, graph: type[StateGraph], name: str): self.graph = graph self.name = name self.graph.states[name] = self @@ -132,7 +133,7 @@ class State: return not self.children @property - def handler(self) -> Callable[[Any], Optional[str]]: + def handler(self) -> Callable[[Any], str | None]: # Retrieve it by name off the graph if self.handler_name is None: raise AttributeError("No handler defined") diff --git a/stator/management/commands/runstator.py b/stator/management/commands/runstator.py index 4d52520..bec88d6 100644 --- a/stator/management/commands/runstator.py +++ b/stator/management/commands/runstator.py @@ -1,4 +1,4 @@ -from typing import List, Type, cast +from typing import cast from asgiref.sync import async_to_sync from django.apps import apps @@ -44,7 +44,7 @@ class Command(BaseCommand): def handle( self, - model_labels: List[str], + model_labels: list[str], concurrency: int, liveness_file: str, schedule_interval: int, @@ -56,7 +56,7 @@ class Command(BaseCommand): Config.system = Config.load_system() # Resolve the models list into names models = cast( - List[Type[StatorModel]], + list[type[StatorModel]], [apps.get_model(label) for label in model_labels], ) if not models: diff --git a/stator/models.py b/stator/models.py index 5257ac9..261584c 100644 --- a/stator/models.py +++ b/stator/models.py @@ -1,7 +1,7 @@ import datetime import pprint import traceback -from typing import ClassVar, List, Optional, Type, Union, cast +from typing import ClassVar, cast from asgiref.sync import sync_to_async from django.db import models, transaction @@ -17,7 +17,7 @@ class StateField(models.CharField): A special field that automatically gets choices from a state graph """ - def __init__(self, graph: Type[StateGraph], **kwargs): + def __init__(self, graph: type[StateGraph], **kwargs): # Sensible default for state length kwargs.setdefault("max_length", 100) # Add choices and initial @@ -61,7 +61,7 @@ class StatorModel(models.Model): state_locked_until = models.DateTimeField(null=True, blank=True) # Collection of subclasses of us - subclasses: ClassVar[List[Type["StatorModel"]]] = [] + subclasses: ClassVar[list[type["StatorModel"]]] = [] class Meta: abstract = True @@ -71,7 +71,7 @@ class StatorModel(models.Model): cls.subclasses.append(cls) @classproperty - def state_graph(cls) -> Type[StateGraph]: + def state_graph(cls) -> type[StateGraph]: return cls._meta.get_field("state").graph @property @@ -104,7 +104,7 @@ class StatorModel(models.Model): @classmethod def transition_get_with_lock( cls, number: int, lock_expiry: datetime.datetime - ) -> List["StatorModel"]: + ) -> list["StatorModel"]: """ Returns up to `number` tasks for execution, having locked them. """ @@ -124,7 +124,7 @@ class StatorModel(models.Model): @classmethod async def atransition_get_with_lock( cls, number: int, lock_expiry: datetime.datetime - ) -> List["StatorModel"]: + ) -> list["StatorModel"]: return await sync_to_async(cls.transition_get_with_lock)(number, lock_expiry) @classmethod @@ -143,7 +143,7 @@ class StatorModel(models.Model): self.state_ready = True self.save() - async def atransition_attempt(self) -> Optional[State]: + async def atransition_attempt(self) -> State | None: """ Attempts to transition the current state by running its handler(s). """ @@ -180,7 +180,7 @@ class StatorModel(models.Model): ) return None - def transition_perform(self, state: Union[State, str]): + def transition_perform(self, state: State | str): """ Transitions the instance to the given state name, forcibly. """ @@ -237,7 +237,7 @@ class StatorError(models.Model): async def acreate_from_instance( cls, instance: StatorModel, - exception: Optional[BaseException] = None, + exception: BaseException | None = None, ): detail = traceback.format_exc() if exception and len(exception.args) > 1: diff --git a/stator/runner.py b/stator/runner.py index ecbaae6..0d8f9ea 100644 --- a/stator/runner.py +++ b/stator/runner.py @@ -3,7 +3,6 @@ import datetime import time import traceback import uuid -from typing import List, Optional, Type from django.utils import timezone @@ -20,10 +19,10 @@ class StatorRunner: def __init__( self, - models: List[Type[StatorModel]], + models: list[type[StatorModel]], concurrency: int = 50, concurrency_per_model: int = 10, - liveness_file: Optional[str] = None, + liveness_file: str | None = None, schedule_interval: int = 30, lock_expiry: int = 300, run_for: int = 0, -- cgit v1.2.3