Skip to content

Commit b9e8232

Browse files
chore: in JobStatus, messages only need to be stored in docstrings
1 parent 93461c7 commit b9e8232

File tree

2 files changed

+60
-9
lines changed

2 files changed

+60
-9
lines changed

mpqp/execution/job.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616

1717
from typing import TYPE_CHECKING, Optional
1818

19-
from aenum import Enum, NoAlias
19+
from aenum import Enum, NoAlias, auto
2020
from typeguard import typechecked
2121

22+
from mpqp.tools.generics import MessageEnum
23+
2224
# This is needed because for some reason pyright does not understand that Enum
2325
# is a class (probably because Enum does weird things to the Enum class)
2426
if TYPE_CHECKING:
@@ -33,20 +35,20 @@
3335
from .devices import ATOSDevice, AvailableDevice, AWSDevice, IBMDevice
3436

3537

36-
class JobStatus(Enum):
38+
class JobStatus(MessageEnum):
3739
"""Possible states of a Job."""
3840

39-
INIT = "initializing the job"
41+
INIT = auto()
4042
"""Initializing the job."""
41-
QUEUED = "the job is in the queue"
43+
QUEUED = auto()
4244
"""The job is in the queue."""
43-
RUNNING = "the job is currently running"
45+
RUNNING = auto()
4446
"""The job is currently running."""
45-
CANCELLED = "the job is cancelled"
47+
CANCELLED = auto()
4648
"""The job is cancelled."""
47-
ERROR = "an error occurred with the job"
49+
ERROR = auto()
4850
"""An error occurred with the job."""
49-
DONE = "the job is successfully done"
51+
DONE = auto()
5052
"""The job is successfully done."""
5153

5254

mpqp/tools/generics.py

+50-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,24 @@
1919

2020
import re
2121
from abc import ABCMeta
22-
from typing import Any, Callable, Iterable, Iterator, Sequence, TypeVar, Union
22+
from inspect import getsource
23+
from typing import (
24+
TYPE_CHECKING,
25+
Any,
26+
Callable,
27+
Iterable,
28+
Iterator,
29+
Sequence,
30+
TypeVar,
31+
Union,
32+
)
33+
34+
from aenum import Enum
35+
36+
# This is needed because for some reason pyright does not understand that Enum
37+
# is a class (probably because Enum does weird things to the Enum class)
38+
if TYPE_CHECKING:
39+
from enum import Enum
2340

2441
import numpy as np
2542
import numpy.typing as npt
@@ -215,3 +232,35 @@ def __init__(self, func: Callable[..., Any]):
215232

216233
def __get__(self, instance: object, owner: object):
217234
return self.fget(owner)
235+
236+
237+
def _get_doc(enum: type[Any], member: str):
238+
src = getsource(enum)
239+
member_pointer = src.find(member)
240+
docstr_start = member_pointer + src[member_pointer:].find('"""') + 3
241+
docstr_end = docstr_start + src[docstr_start:].find('"""')
242+
return src[docstr_start:docstr_end]
243+
244+
245+
class MessageEnum(Enum):
246+
"""Enum subclass allowing you to access the docstring of the members of your
247+
enum through the ``message`` property.
248+
249+
Example:
250+
>>> class A(MessageEnum):
251+
... '''an enum'''
252+
... VALUE1 = auto()
253+
... '''member VALUE1'''
254+
... VALUE2 = auto()
255+
... '''member VALUE2'''
256+
>>> A.VALUE1.message
257+
'member VALUE2'
258+
259+
"""
260+
261+
message: str
262+
263+
def __init__(self, *args: Any, **kwds: dict[str, Any]) -> None:
264+
super().__init__(*args, **kwds)
265+
for member in type(self).__members__:
266+
type(self).__members__[member].message = _get_doc(type(self), member)

0 commit comments

Comments
 (0)