judgevet.testing._conformance_probes
Status: draft.
judgevet.testing._conformance_probes
Observers the provider conformance kit wraps around a provider.
ScopeRecorder wraps a provider factory and records every context it
constructs, so the kit observes entry and exit itself instead of trusting the
provider. TextOnlyView hides every method except system_one. MediaProbe
counts media dispatches so the kit can show that a refusal happened before the
provider ran. declared_evidence builds one image whose MIME type and size
the declared capabilities admit, and undeclared_evidence builds one whose
MIME type they do not admit. Each kit image is a valid one-pixel PNG, JPEG or
WebP file. signature_problem and provider_error_problem describe a broken
port shape or failure contract, so the synchronous and asynchronous kits share
one message. media_members reports a port's media methods for both kits.
None of these import pytest.
Source: https://github.com/Alberto-Codes/judgevet/issues/241#issuecomment-5902293909.
Examples:
from contextlib import nullcontext
from judgevet.media import MediaCapabilities
from judgevet.providers import provider_scope
from judgevet.testing import FakeSystemOnePort
from judgevet.testing._conformance_probes import (
ScopeRecorder,
declared_evidence,
undeclared_evidence,
)
recorder = ScopeRecorder(lambda: nullcontext(FakeSystemOnePort()))
with provider_scope(factory=recorder) as port:
assert callable(port.system_one)
assert recorder.contexts[0].exits == [None]
evidence = undeclared_evidence(MediaCapabilities({"image/png"}))
assert evidence.images[0].media_type == "image/jpeg"
declared = declared_evidence(MediaCapabilities({"image/webp"}))
assert declared is not None
assert declared.images[0].media_type == "image/webp"
from judgevet.testing._conformance_probes import (
provider_error_problem,
signature_problem,
)
assert signature_problem(FakeSystemOnePort().system_one, "m") is None
assert provider_error_problem(RuntimeError("boom")) is not None
See Also
- judgevet.testing.conformance: The base class that uses these observers
- judgevet.providers:
provider_scopeandProviderFactory - judgevet.media:
judge_with_imagesand its capability checks
IMAGE_ID = 'conformance-image'
module-attribute
The attachment identity of every kit image.
IMAGE_QUESTION = 'conformance_noul'
module-attribute
The kit question every kit image is bound to.
MEDIA_METHODS = ('capabilities', 'system_one_media')
module-attribute
The methods the media checks require as callables on a media port.
BodyError
Bases: Exception
The exception the kit raises inside a provider scope body.
Attributes:
| Name | Type | Description |
|---|---|---|
args |
tuple
|
Standard exception arguments containing a message. |
Examples:
error = BodyError("raised inside the provider scope")
assert str(error) == "raised inside the provider scope"
MediaProbe
Bases: TextOnlyView
Delegate every media method and count media dispatches.
Attributes:
| Name | Type | Description |
|---|---|---|
port |
MediaSystemOnePort
|
The wrapped media port. |
calls |
int
|
How many text judgments reached the port. |
media_calls |
int
|
How many media judgments reached the port. |
Examples:
from judgevet.media import MediaCapabilities
from judgevet.testing import FakeSystemOnePort
class Media(FakeSystemOnePort):
def capabilities(self, model):
return MediaCapabilities({"image/png"})
def system_one_media(self, state, questions, model, *, evidence):
return self.system_one(state, questions, model)
probe = MediaProbe(Media())
assert probe.media_calls == 0
__init__(port)
Wrap a media port.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
port
|
MediaSystemOnePort
|
The media port to observe. |
required |
capabilities(model)
Return the wrapped port's declaration unchanged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
str
|
The selected model. |
required |
Returns:
| Type | Description |
|---|---|
MediaCapabilities
|
The wrapped port's capabilities. |
system_one_media(state, questions, model, *, evidence)
Count the media dispatch and delegate it to the wrapped port.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
str | dict[str, Any] | list[Any]
|
The content to judge. |
required |
questions
|
Mapping[str, Question | Mapping[str, Any]]
|
Question names mapped to question definitions. |
required |
model
|
str
|
The selected model. |
required |
evidence
|
ImageEvidence
|
The ordered image evidence. |
required |
Returns:
| Type | Description |
|---|---|
SystemOneResponse
|
The wrapped port's response. |
RecordedContext
Bases: AbstractContextManager[SystemOnePort]
Delegate to one provider context and record its entry and exit.
Attributes:
| Name | Type | Description |
|---|---|---|
inner |
AbstractContextManager[SystemOnePort]
|
The provider's context. |
enters |
int
|
How many times entry ran. |
exits |
list[BaseException | None]
|
The exception each exit received, or None for a clean exit. |
Examples:
from contextlib import nullcontext
from judgevet.testing import FakeSystemOnePort
recorded = RecordedContext(nullcontext(FakeSystemOnePort()))
with recorded:
pass
assert (recorded.enters, recorded.exits) == (1, [None])
__enter__()
Count the entry and enter the provider context.
Returns:
| Type | Description |
|---|---|
SystemOnePort
|
The port the provider context yields. |
__exit__(exc_type, exc, traceback)
Record the exit and pass the provider's suppression answer through.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exc_type
|
type[BaseException] | None
|
The escaping exception type, or None. |
required |
exc
|
BaseException | None
|
The escaping exception, or None. |
required |
traceback
|
TracebackType | None
|
The escaping traceback, or None. |
required |
Returns:
| Type | Description |
|---|---|
bool | None
|
The provider context's own return value. |
__init__(inner)
Wrap one provider context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inner
|
AbstractContextManager[SystemOnePort]
|
The context the provider factory returned. |
required |
ScopeRecorder
Wrap a provider factory and record every context it constructs.
Attributes:
| Name | Type | Description |
|---|---|---|
factory |
ProviderFactory
|
The provider's own factory. |
contexts |
list[RecordedContext]
|
One recorder per factory call. |
Examples:
from contextlib import nullcontext
from judgevet.testing import FakeSystemOnePort
recorder = ScopeRecorder(lambda: nullcontext(FakeSystemOnePort()))
with recorder():
pass
assert len(recorder.contexts) == 1
__call__()
Construct one provider context and start recording it.
Returns:
| Type | Description |
|---|---|
RecordedContext
|
A recorder that delegates to the new provider context. |
__init__(factory)
Store the provider factory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
factory
|
ProviderFactory
|
The provider's own factory. |
required |
TextOnlyView
Expose only the text judgment method of a port.
Attributes:
| Name | Type | Description |
|---|---|---|
port |
SystemOnePort
|
The wrapped port. |
calls |
int
|
How many judgments reached the port. |
Examples:
from judgevet.ports.media import MediaSystemOnePort
from judgevet.testing import FakeSystemOnePort
view = TextOnlyView(FakeSystemOnePort())
assert not isinstance(view, MediaSystemOnePort)
__init__(port)
Wrap a port.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
port
|
SystemOnePort
|
The port whose text method stays visible. |
required |
system_one(state, questions, model)
Count the call and delegate it to the wrapped port.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
str | dict[str, Any] | list[Any]
|
The content to judge. |
required |
questions
|
Mapping[str, Question | Mapping[str, Any]]
|
Question names mapped to question definitions. |
required |
model
|
str
|
The selected model. |
required |
Returns:
| Type | Description |
|---|---|
SystemOneResponse
|
The wrapped port's response. |
declared_evidence(capabilities)
Build one kit image that the declared capabilities admit.
The first image type judgevet supports that the declaration names, and whose kit image fits the declared byte ceilings, is used.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
capabilities
|
MediaCapabilities
|
The provider's declaration for the selected model. |
required |
Returns:
| Type | Description |
|---|---|
ImageEvidence | None
|
Evidence that |
ImageEvidence | None
|
None when the declaration admits no kit image. |
image_evidence(media_type)
Build one kit image of a MIME type, bound to the kit Noul question.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
media_type
|
str
|
The MIME type the attachment declares. |
required |
Returns:
| Type | Description |
|---|---|
ImageEvidence
|
Evidence with one small synthetic attachment. |
media_members(port)
Report which media methods a port exposes and whether all are callable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
port
|
object
|
The provider port. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
The exposed media attribute names, and whether every media method is |
bool
|
present and callable. |
provider_error_problem(error)
Describe a failing port's exception that is not a ProviderError.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
error
|
BaseException
|
The exception the failing port raised. |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
A failure message naming the |
str | None
|
the exception is a |
signature_problem(method, model)
Describe why a system_one method does not take the port's arguments.
SystemOnePort and AsyncSystemOnePort are not runtime-checkable
protocols, so the kit checks the method and its signature directly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
object
|
The port's |
required |
model
|
str
|
The selected model. |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
A failure message when the method is not callable or does not accept |
str | None
|
(state, questions, model) positionally and by keyword, or None. |
undeclared_evidence(capabilities)
Build one kit image whose MIME type the capabilities do not admit.
The first image type judgevet supports that the declaration omits is used. When the declaration names all of them, an unsupported type is used.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
capabilities
|
MediaCapabilities
|
The provider's declaration for the selected model. |
required |
Returns:
| Type | Description |
|---|---|
ImageEvidence
|
Evidence that |