CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-josepy

JOSE protocol implementation in Python with support for JSON Web Algorithms, Keys, and Signatures

73

1.15x
Overview
Eval results
Files

task.mdevals/scenario-10/

Typed Message Dispatch

Define JSON-serializable message models that use a type field to select the correct subclass when deserializing, while safely handling unknown or missing types. The type value error maps to ErrorMessage and info maps to InfoMessage, and that field must always be present in serialized output.

Capabilities

Type-based deserialization

  • Given payload {"type": "error", "content": "Failed", "code": 500, "detail": "DB down"}, Message.from_json returns an ErrorMessage instance with matching attributes. @test
  • If payload uses an unregistered type value, Message.from_json raises a descriptive exception mentioning that type value. @test
  • If payload omits the type field, Message.from_json raises a descriptive exception about the missing type field. @test

Serialization roundtrip

  • Serializing an InfoMessage with to_json and deserializing it with Message.from_json preserves the type of info and subclass-specific fields such as level and content. @test

Implementation

@generates

API

from typing import Any, Dict

class Message:
    message_type: str

    @classmethod
    def from_json(cls, payload: Dict[str, Any]) -> "Message":
        ...

    def to_json(self) -> Dict[str, Any]:
        ...

class ErrorMessage(Message):
    message_type: str = "error"
    content: str
    code: int
    detail: str

class InfoMessage(Message):
    message_type: str = "info"
    content: str
    level: str

Dependencies { .dependencies }

josepy { .dependency }

Provides declarative JSON field definitions and type-based object dispatching utilities.

Install with Tessl CLI

npx tessl i tessl/pypi-josepy

tile.json