quapp-perceval 0.0.1.dev1__tar.gz → 0.0.1.dev3__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {quapp_perceval-0.0.1.dev1 → quapp_perceval-0.0.1.dev3}/PKG-INFO +2 -2
- {quapp_perceval-0.0.1.dev1 → quapp_perceval-0.0.1.dev3}/pyproject.toml +2 -2
- quapp_perceval-0.0.1.dev3/quapp_perceval/__init__.py +5 -0
- quapp_perceval-0.0.1.dev3/quapp_perceval/error_rules.py +82 -0
- {quapp_perceval-0.0.1.dev1 → quapp_perceval-0.0.1.dev3}/quapp_perceval/model/device/perceval_device.py +46 -31
- {quapp_perceval-0.0.1.dev1 → quapp_perceval-0.0.1.dev3}/quapp_perceval/model/provider/quapp_perceval_provider.py +9 -5
- {quapp_perceval-0.0.1.dev1 → quapp_perceval-0.0.1.dev3}/quapp_perceval.egg-info/PKG-INFO +2 -2
- {quapp_perceval-0.0.1.dev1 → quapp_perceval-0.0.1.dev3}/quapp_perceval.egg-info/SOURCES.txt +3 -1
- {quapp_perceval-0.0.1.dev1 → quapp_perceval-0.0.1.dev3}/quapp_perceval.egg-info/requires.txt +1 -1
- quapp_perceval-0.0.1.dev3/tests/test_error_logging.py +304 -0
- quapp_perceval-0.0.1.dev1/quapp_perceval/__init__.py +0 -1
- {quapp_perceval-0.0.1.dev1 → quapp_perceval-0.0.1.dev3}/LICENSE +0 -0
- {quapp_perceval-0.0.1.dev1 → quapp_perceval-0.0.1.dev3}/README.md +0 -0
- {quapp_perceval-0.0.1.dev1 → quapp_perceval-0.0.1.dev3}/quapp_perceval/async_tasks/__init__.py +0 -0
- {quapp_perceval-0.0.1.dev1 → quapp_perceval-0.0.1.dev3}/quapp_perceval/async_tasks/perceval_circuit_export_task.py +0 -0
- {quapp_perceval-0.0.1.dev1 → quapp_perceval-0.0.1.dev3}/quapp_perceval/component/__init__.py +0 -0
- {quapp_perceval-0.0.1.dev1 → quapp_perceval-0.0.1.dev3}/quapp_perceval/component/backend/__init__.py +0 -0
- {quapp_perceval-0.0.1.dev1 → quapp_perceval-0.0.1.dev3}/quapp_perceval/component/backend/perceval_invocation.py +0 -0
- {quapp_perceval-0.0.1.dev1 → quapp_perceval-0.0.1.dev3}/quapp_perceval/factory/__init__.py +0 -0
- {quapp_perceval-0.0.1.dev1 → quapp_perceval-0.0.1.dev3}/quapp_perceval/factory/perceval_device_factory.py +0 -0
- {quapp_perceval-0.0.1.dev1 → quapp_perceval-0.0.1.dev3}/quapp_perceval/factory/perceval_handler_factory.py +0 -0
- {quapp_perceval-0.0.1.dev1 → quapp_perceval-0.0.1.dev3}/quapp_perceval/factory/perceval_provider_factory.py +0 -0
- {quapp_perceval-0.0.1.dev1 → quapp_perceval-0.0.1.dev3}/quapp_perceval/handler/__init__.py +0 -0
- {quapp_perceval-0.0.1.dev1 → quapp_perceval-0.0.1.dev3}/quapp_perceval/handler/invocation_handler.py +0 -0
- {quapp_perceval-0.0.1.dev1 → quapp_perceval-0.0.1.dev3}/quapp_perceval/model/__init__.py +0 -0
- {quapp_perceval-0.0.1.dev1 → quapp_perceval-0.0.1.dev3}/quapp_perceval/model/device/__init__.py +0 -0
- {quapp_perceval-0.0.1.dev1 → quapp_perceval-0.0.1.dev3}/quapp_perceval/model/device/quapp_perceval_device.py +0 -0
- {quapp_perceval-0.0.1.dev1 → quapp_perceval-0.0.1.dev3}/quapp_perceval/model/provider/__init__.py +0 -0
- {quapp_perceval-0.0.1.dev1 → quapp_perceval-0.0.1.dev3}/quapp_perceval.egg-info/dependency_links.txt +0 -0
- {quapp_perceval-0.0.1.dev1 → quapp_perceval-0.0.1.dev3}/quapp_perceval.egg-info/top_level.txt +0 -0
- {quapp_perceval-0.0.1.dev1 → quapp_perceval-0.0.1.dev3}/setup.cfg +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: quapp-perceval
|
|
3
|
-
Version: 0.0.1.
|
|
3
|
+
Version: 0.0.1.dev3
|
|
4
4
|
Summary: Quapp common library supporting Quapp Platform for Quantum Computing
|
|
5
5
|
Author-email: "CITYNOW Co. Ltd. " <corp@citynow.vn>
|
|
6
6
|
License: The MIT License (MIT)
|
|
@@ -19,7 +19,7 @@ Classifier: Programming Language :: Python :: 3
|
|
|
19
19
|
Requires-Python: <3.11,>=3.9
|
|
20
20
|
Description-Content-Type: text/markdown
|
|
21
21
|
License-File: LICENSE
|
|
22
|
-
Requires-Dist: quapp-common==0.0.
|
|
22
|
+
Requires-Dist: quapp-common==0.0.14.dev10
|
|
23
23
|
Requires-Dist: perceval-quandela==1.2.1
|
|
24
24
|
Provides-Extra: dev
|
|
25
25
|
Requires-Dist: black; extra == "dev"
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "quapp-perceval"
|
|
7
|
-
version = "0.0.1.
|
|
7
|
+
version = "0.0.1.dev3"
|
|
8
8
|
description = "Quapp common library supporting Quapp Platform for Quantum Computing"
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
authors = [{ name = "CITYNOW Co. Ltd. ", email = "corp@citynow.vn" }]
|
|
@@ -16,7 +16,7 @@ classifiers = [
|
|
|
16
16
|
]
|
|
17
17
|
keywords = ["quapp", "quapp-perceval", "quantum"]
|
|
18
18
|
dependencies = [
|
|
19
|
-
"quapp-common==0.0.
|
|
19
|
+
"quapp-common==0.0.14.dev10",
|
|
20
20
|
"perceval-quandela==1.2.1"
|
|
21
21
|
]
|
|
22
22
|
requires-python = ">=3.9,<3.11"
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# Quapp Platform Project
|
|
2
|
+
# error_rules.py
|
|
3
|
+
# Copyright © CITYNOW Co. Ltd. All rights reserved.
|
|
4
|
+
|
|
5
|
+
"""Perceval-specific error classification rules.
|
|
6
|
+
|
|
7
|
+
Registered into ``quapp_common.util.error_classifier`` when ``quapp_perceval`` is
|
|
8
|
+
imported, so ``quapp-common`` never has to depend on Perceval.
|
|
9
|
+
|
|
10
|
+
Matching is done on the class **NAME as a string**, never ``isinstance``, and
|
|
11
|
+
nothing from ``perceval`` is imported. Same reason as the Qiskit version:
|
|
12
|
+
``import quapp_perceval`` must not demand the vendor SDK, and the classifier has to
|
|
13
|
+
work even when the SDK is absent.
|
|
14
|
+
|
|
15
|
+
Without any rule here the system still runs: the classifier falls back on the
|
|
16
|
+
phase, so the user just gets a generic ``EXECUTION_FAILED`` /
|
|
17
|
+
``PROVIDER_INVOCATION_FAILED`` instead of a specific code.
|
|
18
|
+
|
|
19
|
+
The first two rules key off messages **this lib itself** raises -- checked
|
|
20
|
+
against the code, not guessed:
|
|
21
|
+
|
|
22
|
+
quapp_perceval/factory/perceval_provider_factory.py:32
|
|
23
|
+
Exception("Unsupported provider!")
|
|
24
|
+
quapp_perceval/factory/perceval_device_factory.py:37
|
|
25
|
+
Exception("Unsupported device!")
|
|
26
|
+
quapp_perceval/model/provider/quapp_perceval_provider.py:40
|
|
27
|
+
Exception('Unsupported device')
|
|
28
|
+
|
|
29
|
+
The last rule matches Perceval exception names. If upstream renames them the rule
|
|
30
|
+
becomes inert rather than broken -- the classifier just falls back on the phase.
|
|
31
|
+
"""
|
|
32
|
+
|
|
33
|
+
from quapp_common.enum.error_category import ErrorCategory
|
|
34
|
+
from quapp_common.util.error_classifier import Rule, register_rules
|
|
35
|
+
|
|
36
|
+
DOC_DEVICE = 'errors/perceval-device'
|
|
37
|
+
DOC_CIRCUIT = 'errors/perceval-circuit'
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def _message(exception) -> str:
|
|
41
|
+
return str(exception).lower()
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def _name(exception) -> str:
|
|
45
|
+
return type(exception).__name__
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
register_rules(
|
|
49
|
+
# ------------------ raised by quapp-perceval itself (checked against code)
|
|
50
|
+
Rule(match=lambda e: 'unsupported device' in _message(e)
|
|
51
|
+
or 'unsupported provider' in _message(e),
|
|
52
|
+
category=ErrorCategory.PROVIDER_DEVICE,
|
|
53
|
+
code='PERCEVAL_DEVICE_UNSUPPORTED',
|
|
54
|
+
resolution="This device or provider is not supported by Quapp's "
|
|
55
|
+
'Perceval lib yet.',
|
|
56
|
+
steps=('Check the device selected for the job',
|
|
57
|
+
'quapp-perceval currently serves QUAO_QUANTUM_SIMULATOR'),
|
|
58
|
+
doc_slug=DOC_DEVICE),
|
|
59
|
+
|
|
60
|
+
Rule(match=lambda e: 'invalid circuit type' in _message(e),
|
|
61
|
+
category=ErrorCategory.USER_CODE,
|
|
62
|
+
code='PERCEVAL_INVALID_CIRCUIT_TYPE',
|
|
63
|
+
resolution='The circuit preparation function did not return a Perceval '
|
|
64
|
+
'circuit.',
|
|
65
|
+
steps=('Check what your processing function returns',
|
|
66
|
+
'The circuit must be a perceval.Circuit'),
|
|
67
|
+
doc_slug=DOC_CIRCUIT),
|
|
68
|
+
|
|
69
|
+
# ------------------------------------------------- Perceval's own exceptions
|
|
70
|
+
# Placed after the rules above: many Perceval exceptions inherit
|
|
71
|
+
# PercevalException, but matching is by NAME so only these exact names land
|
|
72
|
+
# here.
|
|
73
|
+
Rule(match=lambda e: _name(e) in {'PercevalError', 'CircuitError',
|
|
74
|
+
'ProcessorError', 'RPCError'},
|
|
75
|
+
category=ErrorCategory.SDK_RUNTIME,
|
|
76
|
+
code='PERCEVAL_SDK_ERROR',
|
|
77
|
+
resolution='Perceval failed while building or running the circuit. See '
|
|
78
|
+
'the stack trace below.',
|
|
79
|
+
steps=('Read the rootCause to see which step Perceval rejected',
|
|
80
|
+
'Check the mode count and processor configuration Perceval '
|
|
81
|
+
'supports')),
|
|
82
|
+
)
|
|
@@ -8,14 +8,14 @@ from quapp_common.model.device.custom_device import CustomDevice
|
|
|
8
8
|
from quapp_common.data.response.job_response import JobResponse
|
|
9
9
|
|
|
10
10
|
from quapp_common.component.callback.update_job_metadata import update_job_metadata
|
|
11
|
-
from quapp_common.enum.status.job_status import JobStatus
|
|
12
|
-
from quapp_common.enum.media_type import MediaType
|
|
13
11
|
from quapp_common.enum.status.status_code import StatusCode
|
|
14
12
|
from quapp_common.data.response.authentication import Authentication
|
|
15
13
|
from quapp_common.data.response.custom_header import CustomHeader
|
|
16
14
|
from quapp_common.data.device.circuit_running_option import CircuitRunningOption
|
|
17
15
|
from quapp_common.enum.invocation_step import InvocationStep
|
|
18
16
|
|
|
17
|
+
from quapp_common.config.log_context import phase
|
|
18
|
+
from quapp_common.enum.runtime_phase import RuntimePhase
|
|
19
19
|
from quapp_common.util.response_utils import build_error_job_response
|
|
20
20
|
|
|
21
21
|
from quapp_common.data.callback.callback_url import CallbackUrl
|
|
@@ -66,13 +66,20 @@ class PercevalDevice(CustomDevice, ABC):
|
|
|
66
66
|
update_job_metadata(job_response=job_response,
|
|
67
67
|
callback_url=execution_callback.on_start)
|
|
68
68
|
try:
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
69
|
+
# A copy of Device.run_circuit that lost every `with phase(...)`, so
|
|
70
|
+
# until now no perceval log line carried a phase at all. COMPILATION
|
|
71
|
+
# stays separate from EXECUTION: a wrong circuit type and a provider
|
|
72
|
+
# rejecting the job are two different failures.
|
|
73
|
+
with phase(RuntimePhase.COMPILATION):
|
|
74
|
+
job = self._create_job(circuit=circuit, options=options)
|
|
72
75
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
+
with phase(RuntimePhase.EXECUTION):
|
|
77
|
+
job_response.provider_job_id = self._get_provider_job_id(job)
|
|
78
|
+
job_response.job_status = self._get_job_status(job)
|
|
79
|
+
|
|
80
|
+
job_result = self._get_job_result(job)
|
|
81
|
+
job_response.job_result = job_result
|
|
82
|
+
original_job_result = job_result
|
|
76
83
|
|
|
77
84
|
update_job_metadata(job_response=job_response,
|
|
78
85
|
callback_url=execution_callback.on_done)
|
|
@@ -80,13 +87,17 @@ class PercevalDevice(CustomDevice, ABC):
|
|
|
80
87
|
return original_job_result, job_response
|
|
81
88
|
|
|
82
89
|
except Exception as exception:
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
job_response
|
|
89
|
-
|
|
90
|
+
# This used to be `{"error": str(exception)}`: no category, no phase,
|
|
91
|
+
# no rootCause -- exactly what left the Job Log without a readable
|
|
92
|
+
# reason. build_error_job_response fills the whole envelope and logs
|
|
93
|
+
# once itself, so the logger.debug that used to sit here is gone too,
|
|
94
|
+
# to avoid logging the same error twice.
|
|
95
|
+
job_response = build_error_job_response(
|
|
96
|
+
exception, job_response,
|
|
97
|
+
message='Error when executing job',
|
|
98
|
+
stage='while running the circuit on the device',
|
|
99
|
+
phase=RuntimePhase.EXECUTION,
|
|
100
|
+
context=self._error_context())
|
|
90
101
|
|
|
91
102
|
update_job_metadata(job_response=job_response,
|
|
92
103
|
callback_url=execution_callback.on_error)
|
|
@@ -143,19 +154,20 @@ class PercevalDevice(CustomDevice, ABC):
|
|
|
143
154
|
update_job_metadata(job_response, analysis_callback.on_start)
|
|
144
155
|
|
|
145
156
|
try:
|
|
146
|
-
|
|
147
|
-
|
|
157
|
+
with phase(RuntimePhase.ANALYSIS):
|
|
158
|
+
self.logger.debug(
|
|
159
|
+
"Producing histogram data from original job result")
|
|
148
160
|
|
|
149
|
-
|
|
150
|
-
|
|
161
|
+
job_response.job_histogram = self._produce_histogram_data(
|
|
162
|
+
original_job_result)
|
|
151
163
|
|
|
164
|
+
self.logger.debug(
|
|
165
|
+
"Calculating execution time from parsed results")
|
|
166
|
+
self._calculate_execution_time(original_job_result)
|
|
167
|
+
job_response.execution_time = self.execution_time
|
|
152
168
|
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
job_response.execution_time = self.execution_time
|
|
156
|
-
|
|
157
|
-
self.logger.debug("Retrieving shot count from job result")
|
|
158
|
-
job_response.shots = self._get_shots(original_job_result)
|
|
169
|
+
self.logger.debug("Retrieving shot count from job result")
|
|
170
|
+
job_response.shots = self._get_shots(original_job_result)
|
|
159
171
|
|
|
160
172
|
update_job_metadata(job_response, analysis_callback.on_done)
|
|
161
173
|
|
|
@@ -163,12 +175,15 @@ class PercevalDevice(CustomDevice, ABC):
|
|
|
163
175
|
return job_response
|
|
164
176
|
|
|
165
177
|
except Exception as exception:
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
178
|
+
# A logger.exception() here would emit a second ERROR line for the same
|
|
179
|
+
# failure: build_error_job_response already logs the full envelope.
|
|
180
|
+
# Two lines skew every metric that counts job_failed.
|
|
181
|
+
job_response = build_error_job_response(
|
|
182
|
+
exception, job_response,
|
|
183
|
+
message='Error when analyzing job result',
|
|
184
|
+
stage='while analysing the job result',
|
|
185
|
+
phase=RuntimePhase.ANALYSIS,
|
|
186
|
+
context=self._error_context())
|
|
172
187
|
|
|
173
188
|
update_job_metadata(job_response, analysis_callback.on_error)
|
|
174
189
|
return None
|
|
@@ -5,11 +5,11 @@
|
|
|
5
5
|
# quapp_perceval_provider.py
|
|
6
6
|
# Copyright © CITYNOW Co. Ltd. All rights reserved.
|
|
7
7
|
|
|
8
|
-
from quapp_common.config.logging_config import
|
|
8
|
+
from quapp_common.config.logging_config import logger
|
|
9
9
|
from quapp_common.enum.provider_tag import ProviderTag
|
|
10
10
|
from quapp_common.model.provider.provider import Provider
|
|
11
11
|
|
|
12
|
-
logger =
|
|
12
|
+
logger = logger.bind(context='QuappPercevalProvider')
|
|
13
13
|
|
|
14
14
|
_PROVIDER_TAG = (
|
|
15
15
|
getattr(ProviderTag, "QUANDELA_CLOUD", None)
|
|
@@ -35,6 +35,10 @@ class QuappPercevalProvider(Provider):
|
|
|
35
35
|
token = self._authentication.get("apiKey")
|
|
36
36
|
|
|
37
37
|
return RemoteProcessor(device_specification, token=token)
|
|
38
|
-
except Exception:
|
|
39
|
-
logger.exception(
|
|
40
|
-
|
|
38
|
+
except Exception as exception:
|
|
39
|
+
# A logger.exception() here would emit a second ERROR line: this error
|
|
40
|
+
# is logged again by build_error_job_response with the full envelope.
|
|
41
|
+
# `from` keeps the cause chain so the classifier can still read
|
|
42
|
+
# the SDK's original error.
|
|
43
|
+
raise Exception(
|
|
44
|
+
f'Unsupported device: {device_specification}') from exception
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: quapp-perceval
|
|
3
|
-
Version: 0.0.1.
|
|
3
|
+
Version: 0.0.1.dev3
|
|
4
4
|
Summary: Quapp common library supporting Quapp Platform for Quantum Computing
|
|
5
5
|
Author-email: "CITYNOW Co. Ltd. " <corp@citynow.vn>
|
|
6
6
|
License: The MIT License (MIT)
|
|
@@ -19,7 +19,7 @@ Classifier: Programming Language :: Python :: 3
|
|
|
19
19
|
Requires-Python: <3.11,>=3.9
|
|
20
20
|
Description-Content-Type: text/markdown
|
|
21
21
|
License-File: LICENSE
|
|
22
|
-
Requires-Dist: quapp-common==0.0.
|
|
22
|
+
Requires-Dist: quapp-common==0.0.14.dev10
|
|
23
23
|
Requires-Dist: perceval-quandela==1.2.1
|
|
24
24
|
Provides-Extra: dev
|
|
25
25
|
Requires-Dist: black; extra == "dev"
|
|
@@ -2,6 +2,7 @@ LICENSE
|
|
|
2
2
|
README.md
|
|
3
3
|
pyproject.toml
|
|
4
4
|
quapp_perceval/__init__.py
|
|
5
|
+
quapp_perceval/error_rules.py
|
|
5
6
|
quapp_perceval.egg-info/PKG-INFO
|
|
6
7
|
quapp_perceval.egg-info/SOURCES.txt
|
|
7
8
|
quapp_perceval.egg-info/dependency_links.txt
|
|
@@ -23,4 +24,5 @@ quapp_perceval/model/device/__init__.py
|
|
|
23
24
|
quapp_perceval/model/device/perceval_device.py
|
|
24
25
|
quapp_perceval/model/device/quapp_perceval_device.py
|
|
25
26
|
quapp_perceval/model/provider/__init__.py
|
|
26
|
-
quapp_perceval/model/provider/quapp_perceval_provider.py
|
|
27
|
+
quapp_perceval/model/provider/quapp_perceval_provider.py
|
|
28
|
+
tests/test_error_logging.py
|
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
# Quapp Platform Project
|
|
2
|
+
# test_error_logging.py
|
|
3
|
+
# Copyright © CITYNOW Co. Ltd. All rights reserved.
|
|
4
|
+
|
|
5
|
+
"""Locks four classes of error-logging bug, all found in real provider-lib code.
|
|
6
|
+
|
|
7
|
+
1. **A copied hook lost its phase.** Some devices copied ``run_circuit`` /
|
|
8
|
+
``_on_execution`` / ``_on_analysis`` wholesale from ``Device`` but dropped the
|
|
9
|
+
``with phase(...)`` blocks, so that lib's log had no ``phase`` field at any
|
|
10
|
+
stage. Same class of bug as ``CustomDevice._on_analysis`` in quapp-common.
|
|
11
|
+
|
|
12
|
+
2. **The error was flattened into a string.** ``job_response.job_result =
|
|
13
|
+
{"error": str(exception)}`` leaves the Job Log with a bare string: no
|
|
14
|
+
category, no phase, no rootCause -- exactly what the ticket complained about.
|
|
15
|
+
|
|
16
|
+
3. **The error was logged twice.** ``logger.exception()`` immediately before
|
|
17
|
+
``build_error_job_response`` emits a second ERROR line for the same failure,
|
|
18
|
+
skewing every metric that counts job_failed.
|
|
19
|
+
|
|
20
|
+
4. **A fake job_id.** ``job_logger('SomeClass')`` -- the first argument of
|
|
21
|
+
``job_logger()`` is the **job_id**. Passing a class name there sets a fake
|
|
22
|
+
job_id and it BEATS the real one (``_inject_job_context`` only fills keys that
|
|
23
|
+
are still None), so filtering by jobId in Grafana loses every line that
|
|
24
|
+
component logged.
|
|
25
|
+
|
|
26
|
+
The tests discover files inside the package, so this file works for any lib
|
|
27
|
+
without hardcoding names.
|
|
28
|
+
"""
|
|
29
|
+
|
|
30
|
+
import ast
|
|
31
|
+
import io
|
|
32
|
+
import pathlib
|
|
33
|
+
import re
|
|
34
|
+
import tokenize
|
|
35
|
+
|
|
36
|
+
import pytest
|
|
37
|
+
|
|
38
|
+
REPO_ROOT = pathlib.Path(__file__).resolve().parent.parent
|
|
39
|
+
|
|
40
|
+
# The first release carrying a phase for analysis/post-processing and one traceId
|
|
41
|
+
# per job.
|
|
42
|
+
MINIMUM_COMMON = '0.0.14.dev6'
|
|
43
|
+
|
|
44
|
+
# Hooks already instrumented in quapp-common: copying one means copying its
|
|
45
|
+
# phases too. _on_execution creates the job and then talks to the provider, which
|
|
46
|
+
# is why it needs both -- a failed transpile and a rejected job are different
|
|
47
|
+
# errors.
|
|
48
|
+
INSTRUMENTED = {'_on_execution': {'COMPILATION', 'EXECUTION'},
|
|
49
|
+
'_on_analysis' : {'ANALYSIS'}}
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def package_root() -> pathlib.Path:
|
|
53
|
+
"""The provider lib's package directory, inferred from the tree.
|
|
54
|
+
|
|
55
|
+
A few repos hold more than one package -- quapp-hpc also has
|
|
56
|
+
``quapp_slurm_hpc``, which only contains ``runner.py`` for the Slurm node and
|
|
57
|
+
is not the provider lib. Pick by structural evidence (it has ``model/`` or
|
|
58
|
+
``factory/``) rather than by name.
|
|
59
|
+
"""
|
|
60
|
+
candidates = [d for d in REPO_ROOT.iterdir()
|
|
61
|
+
if d.is_dir() and re.fullmatch(r'qu?app_\w+', d.name)
|
|
62
|
+
and (d / '__init__.py').is_file()]
|
|
63
|
+
assert candidates, 'found no package at all'
|
|
64
|
+
|
|
65
|
+
if len(candidates) > 1:
|
|
66
|
+
providers = [d for d in candidates
|
|
67
|
+
if (d / 'model').is_dir() or (d / 'factory').is_dir()]
|
|
68
|
+
assert len(providers) == 1, (
|
|
69
|
+
f'could not identify the provider package: '
|
|
70
|
+
f'{providers or candidates}')
|
|
71
|
+
return providers[0]
|
|
72
|
+
|
|
73
|
+
return candidates[0]
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
PACKAGE = package_root()
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def code_only(path) -> str:
|
|
80
|
+
"""Source with comments stripped out.
|
|
81
|
+
|
|
82
|
+
Needed because the explanatory comments *quote* the very code being banned
|
|
83
|
+
(``{"error": str(exception)}``, ``logger.exception(``). A raw scan would
|
|
84
|
+
report a false positive on the explanation itself.
|
|
85
|
+
"""
|
|
86
|
+
source = path.read_text(encoding='utf-8')
|
|
87
|
+
kept = [token for token in
|
|
88
|
+
tokenize.generate_tokens(io.StringIO(source).readline)
|
|
89
|
+
if token.type != tokenize.COMMENT]
|
|
90
|
+
return tokenize.untokenize(kept)
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def phases_opened_in(source: str) -> set:
|
|
94
|
+
"""The RuntimePhases opened with ``with phase(...)`` in a piece of code."""
|
|
95
|
+
tree = ast.parse(source.lstrip())
|
|
96
|
+
return {
|
|
97
|
+
argument.attr
|
|
98
|
+
for node in ast.walk(tree) if isinstance(node, ast.With)
|
|
99
|
+
for item in node.items if isinstance(item.context_expr, ast.Call)
|
|
100
|
+
if (getattr(item.context_expr.func, 'id', None)
|
|
101
|
+
or getattr(item.context_expr.func, 'attr', None)) == 'phase'
|
|
102
|
+
for argument in item.context_expr.args
|
|
103
|
+
if isinstance(argument, ast.Attribute)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def python_files():
|
|
108
|
+
return sorted(PACKAGE.rglob('*.py'))
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
# ------------------------------------------- (1) a copied hook keeps its phases
|
|
112
|
+
|
|
113
|
+
def test_copied_hooks_keep_their_phases():
|
|
114
|
+
"""Overriding an instrumented hook means copying its `with phase(...)` too."""
|
|
115
|
+
offenders = []
|
|
116
|
+
for path in python_files():
|
|
117
|
+
source = path.read_text(encoding='utf-8')
|
|
118
|
+
tree = ast.parse(source, filename=str(path))
|
|
119
|
+
for node in ast.walk(tree):
|
|
120
|
+
if not isinstance(node, ast.FunctionDef) \
|
|
121
|
+
or node.name not in INSTRUMENTED:
|
|
122
|
+
continue
|
|
123
|
+
opened = phases_opened_in(ast.get_source_segment(source, node))
|
|
124
|
+
missing = INSTRUMENTED[node.name] - opened
|
|
125
|
+
if missing:
|
|
126
|
+
offenders.append(
|
|
127
|
+
f'{path.name}::{node.name} is missing {sorted(missing)}')
|
|
128
|
+
|
|
129
|
+
assert not offenders, (
|
|
130
|
+
f'overridden hooks lost their phase: {offenders}. The original in '
|
|
131
|
+
f'quapp-common has `with phase(...)`; copying it without that leaves the '
|
|
132
|
+
f'log with no phase field.')
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def test_at_least_one_phase_is_opened_somewhere():
|
|
136
|
+
"""A lib that overrides no hook needs none; one that does needs a phase."""
|
|
137
|
+
overrides = any(
|
|
138
|
+
isinstance(node, ast.FunctionDef) and node.name in INSTRUMENTED
|
|
139
|
+
for path in python_files()
|
|
140
|
+
for node in ast.walk(ast.parse(path.read_text(encoding='utf-8')))
|
|
141
|
+
)
|
|
142
|
+
if not overrides:
|
|
143
|
+
pytest.skip('lib overrides no hook; phases come from quapp-common')
|
|
144
|
+
|
|
145
|
+
opened = set()
|
|
146
|
+
for path in python_files():
|
|
147
|
+
opened |= phases_opened_in(path.read_text(encoding='utf-8'))
|
|
148
|
+
assert opened, 'hooks are overridden but no phase is opened'
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
# --------------------------------- (2) no hand-rolled error string
|
|
152
|
+
|
|
153
|
+
@pytest.mark.parametrize('path', python_files(), ids=lambda p: p.name)
|
|
154
|
+
def test_no_hand_rolled_error_dict(path):
|
|
155
|
+
"""`{"error": str(exception)}` bypasses the entire error envelope."""
|
|
156
|
+
assert '{"error": str(exception)}' not in code_only(path), (
|
|
157
|
+
f'{path.name}: flattens the error into a string, losing '
|
|
158
|
+
f'category/phase/rootCause. Use '
|
|
159
|
+
f'build_error_job_response(..., phase=..., context=...)')
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def test_error_envelope_carries_phase_and_context():
|
|
163
|
+
"""Every build_error_job_response in the device needs phase= and context=."""
|
|
164
|
+
for path in python_files():
|
|
165
|
+
source = code_only(path)
|
|
166
|
+
calls = source.count('build_error_job_response(')
|
|
167
|
+
if not calls:
|
|
168
|
+
continue
|
|
169
|
+
assert source.count('phase=RuntimePhase.') >= calls, (
|
|
170
|
+
f'{path.name}: has {calls} error envelopes but is missing phase=, '
|
|
171
|
+
f'so the ErrorReport would come out with phase=None')
|
|
172
|
+
assert source.count('context=self._error_context()') >= calls, (
|
|
173
|
+
f'{path.name}: error envelope is missing context=, leaving support '
|
|
174
|
+
f'without the device/provider')
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
# ------------------------------------------ (3) one error is logged once
|
|
178
|
+
|
|
179
|
+
def test_no_duplicate_error_log_before_envelope():
|
|
180
|
+
"""logger.exception()/logger.error() right before the envelope = 2 ERRORs."""
|
|
181
|
+
offenders = []
|
|
182
|
+
for path in python_files():
|
|
183
|
+
source = code_only(path)
|
|
184
|
+
for match in re.finditer(r'logger\.(exception|error)\(', source):
|
|
185
|
+
if 'build_error_job_response(' in source[match.end():match.end() + 400]:
|
|
186
|
+
line = source[:match.start()].count('\n') + 1
|
|
187
|
+
offenders.append(f'{path.name}:{line}')
|
|
188
|
+
|
|
189
|
+
assert not offenders, (
|
|
190
|
+
f'duplicate ERROR log before build_error_job_response at {offenders}; '
|
|
191
|
+
f'the envelope already logs once')
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
# ------------------------------------------------- (4) no fake job_id
|
|
195
|
+
|
|
196
|
+
@pytest.mark.parametrize('path', python_files(), ids=lambda p: p.name)
|
|
197
|
+
def test_no_fake_job_logger_argument(path):
|
|
198
|
+
"""`job_logger()` takes a real job_id; a component name belongs in `context`.
|
|
199
|
+
|
|
200
|
+
Two shapes of fake id are caught: a string literal (`job_logger('Provider')`)
|
|
201
|
+
and `__name__`, which quietly makes the job_id the dotted module path and
|
|
202
|
+
blows up jobId cardinality in Grafana as well as losing the real job.
|
|
203
|
+
"""
|
|
204
|
+
tree = ast.parse(path.read_text(encoding='utf-8'), filename=str(path))
|
|
205
|
+
|
|
206
|
+
offenders = []
|
|
207
|
+
for node in ast.walk(tree):
|
|
208
|
+
if not isinstance(node, ast.Call) or not node.args:
|
|
209
|
+
continue
|
|
210
|
+
if (getattr(node.func, 'id', None)
|
|
211
|
+
or getattr(node.func, 'attr', None)) != 'job_logger':
|
|
212
|
+
continue
|
|
213
|
+
first = node.args[0]
|
|
214
|
+
if isinstance(first, ast.Constant) and isinstance(first.value, str):
|
|
215
|
+
offenders.append(repr(first.value))
|
|
216
|
+
elif isinstance(first, ast.Name) and first.id == '__name__':
|
|
217
|
+
offenders.append('__name__')
|
|
218
|
+
|
|
219
|
+
assert not offenders, (
|
|
220
|
+
f'{path.name}: job_logger() got {offenders} -- those are fake job_ids. '
|
|
221
|
+
f"Use logger.bind(context='...') for the component name")
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
def test_providers_bind_their_name_to_context():
|
|
225
|
+
"""A provider that logs must bind its own name, so lines say where they came
|
|
226
|
+
from."""
|
|
227
|
+
providers = sorted((PACKAGE / 'model' / 'provider').glob('*.py'))
|
|
228
|
+
if not providers:
|
|
229
|
+
pytest.skip('lib has no model/provider directory')
|
|
230
|
+
|
|
231
|
+
offenders = []
|
|
232
|
+
for path in providers:
|
|
233
|
+
if path.name == '__init__.py':
|
|
234
|
+
continue
|
|
235
|
+
# code_only: several providers keep their old logging commented out, and
|
|
236
|
+
# a raw scan would flag those as if they still logged.
|
|
237
|
+
source = code_only(path)
|
|
238
|
+
if 'logger' not in source:
|
|
239
|
+
continue
|
|
240
|
+
if not re.search(r"logger\.bind\(context='[A-Za-z]+'\)", source):
|
|
241
|
+
offenders.append(path.name)
|
|
242
|
+
|
|
243
|
+
assert not offenders, (
|
|
244
|
+
f'providers that log without binding context: {offenders}. The component '
|
|
245
|
+
f'name must be a field, not text inside the message, or Grafana cannot '
|
|
246
|
+
f'filter on it')
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
# ------------------------------------------ rules + the quapp-common pin
|
|
250
|
+
|
|
251
|
+
def test_error_rules_module_exists():
|
|
252
|
+
assert (PACKAGE / 'error_rules.py').is_file(), \
|
|
253
|
+
"no error_rules.py, so this SDK's errors never get classified"
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
def test_error_rules_are_registered_on_import():
|
|
257
|
+
init = (PACKAGE / '__init__.py').read_text(encoding='utf-8')
|
|
258
|
+
assert 'error_rules' in init, \
|
|
259
|
+
'__init__.py does not import error_rules, so the rules never register'
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
def test_error_rules_imports_no_vendor_sdk():
|
|
263
|
+
"""The classifier has to run even with the SDK absent, hence name matching."""
|
|
264
|
+
tree = ast.parse((PACKAGE / 'error_rules.py').read_text(encoding='utf-8'))
|
|
265
|
+
|
|
266
|
+
roots = set()
|
|
267
|
+
for node in ast.walk(tree):
|
|
268
|
+
if isinstance(node, ast.Import):
|
|
269
|
+
roots.update(alias.name.split('.')[0] for alias in node.names)
|
|
270
|
+
elif isinstance(node, ast.ImportFrom) and node.module and not node.level:
|
|
271
|
+
roots.add(node.module.split('.')[0])
|
|
272
|
+
|
|
273
|
+
assert roots <= {'quapp_common'}, \
|
|
274
|
+
f'error_rules.py may only import quapp_common, but imports: {sorted(roots)}'
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
def test_common_pin_is_exact_and_current():
|
|
278
|
+
version = pytest.importorskip('packaging.version')
|
|
279
|
+
|
|
280
|
+
text = (REPO_ROOT / 'pyproject.toml').read_text(encoding='utf-8')
|
|
281
|
+
assert not re.findall(r'"quapp-common\s*(>=|>|~=)', text), (
|
|
282
|
+
'quapp-common must be pinned with `==`: `>=` lets pip pick a higher '
|
|
283
|
+
'stable release that is missing the feature (which already happened '
|
|
284
|
+
'with quapp-qiskit 1.0.14)')
|
|
285
|
+
|
|
286
|
+
match = re.search(r'"quapp-common\s*==\s*([^"]+)"', text)
|
|
287
|
+
assert match, 'no quapp-common pin found'
|
|
288
|
+
assert version.Version(match.group(1)) >= version.Version(MINIMUM_COMMON), (
|
|
289
|
+
f'pin {match.group(1)} < {MINIMUM_COMMON}: that release has no phase for '
|
|
290
|
+
f'analysis/post-processing and still splits one job into two traceIds')
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
def test_registered_rules_are_reachable():
|
|
294
|
+
"""Runs for real: import the lib and read the registry. Skips without the SDK."""
|
|
295
|
+
package_name = PACKAGE.name
|
|
296
|
+
pytest.importorskip(package_name)
|
|
297
|
+
__import__(package_name)
|
|
298
|
+
from quapp_common.util.error_classifier import registered_rules
|
|
299
|
+
|
|
300
|
+
codes = {rule.code for rule in registered_rules()}
|
|
301
|
+
own = {code for code in codes if not code.startswith(
|
|
302
|
+
('INPUT_', 'DEPENDENCY_', 'PROVIDER_', 'HANDLER_', 'PLATFORM_',
|
|
303
|
+
'COMPILATION_', 'EXECUTION_', 'ANALYSIS_', 'POST_'))}
|
|
304
|
+
assert own, f'no lib-specific rule registered, only generic ones: {sorted(codes)}'
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
from .factory.perceval_handler_factory import PercevalHandlerFactory
|
|
File without changes
|
|
File without changes
|
{quapp_perceval-0.0.1.dev1 → quapp_perceval-0.0.1.dev3}/quapp_perceval/async_tasks/__init__.py
RENAMED
|
File without changes
|
|
File without changes
|
{quapp_perceval-0.0.1.dev1 → quapp_perceval-0.0.1.dev3}/quapp_perceval/component/__init__.py
RENAMED
|
File without changes
|
{quapp_perceval-0.0.1.dev1 → quapp_perceval-0.0.1.dev3}/quapp_perceval/component/backend/__init__.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{quapp_perceval-0.0.1.dev1 → quapp_perceval-0.0.1.dev3}/quapp_perceval/handler/invocation_handler.py
RENAMED
|
File without changes
|
|
File without changes
|
{quapp_perceval-0.0.1.dev1 → quapp_perceval-0.0.1.dev3}/quapp_perceval/model/device/__init__.py
RENAMED
|
File without changes
|
|
File without changes
|
{quapp_perceval-0.0.1.dev1 → quapp_perceval-0.0.1.dev3}/quapp_perceval/model/provider/__init__.py
RENAMED
|
File without changes
|
{quapp_perceval-0.0.1.dev1 → quapp_perceval-0.0.1.dev3}/quapp_perceval.egg-info/dependency_links.txt
RENAMED
|
File without changes
|
{quapp_perceval-0.0.1.dev1 → quapp_perceval-0.0.1.dev3}/quapp_perceval.egg-info/top_level.txt
RENAMED
|
File without changes
|
|
File without changes
|