Skip to content

Add Python code generator using pydantic (python subcommand)#5

Draft
Copilot wants to merge 3 commits into
mainfrom
copilot/add-python-code-generator
Draft

Add Python code generator using pydantic (python subcommand)#5
Copilot wants to merge 3 commits into
mainfrom
copilot/add-python-code-generator

Conversation

Copilot AI commented Mar 4, 2026

Copy link
Copy Markdown
Contributor

Adds a python subcommand to dcrpcgen that generates a typed Python JSON-RPC client from an OpenRPC schema, using pydantic for type validation and deserialization.

Generated output (3 files)

types.py

  • Object schemas → BaseModel with ConfigDict(populate_by_name=True) and explicit Field(alias="camelCase") per field (avoids pydantic's alias_generator which misroundtrips names like e2eeAvail)
  • String enums → StrEnum with UPPER_CASE = "original_value"
  • Discriminated unions → Annotated[Union[VariantA, VariantB], Field(discriminator="kind")]; each variant carries kind: Literal["value"]
  • All field names in snake_case
class AccountConfigured(BaseModel):
    model_config = ConfigDict(populate_by_name=True)
    kind: Literal["Configured"]
    color: str
    id: int
    display_name: Optional[str] = Field(default=None, alias="displayName")

Account = Annotated[Union[AccountConfigured, AccountUnconfigured], Field(discriminator="kind")]

rpc.py

  • Synchronous Rpc class; method params in snake_case
  • Non-None returns use TypeAdapter(T).validate_python(result)
  • Pydantic model params serialized via _wrap()model.model_dump(by_alias=True)
def get_contact(self, account_id: int, contact_id: int) -> Contact:
    return TypeAdapter(Contact).validate_python(
        self.transport.call("get_contact", account_id, contact_id)
    )

transport.py

  • IOTransport over a deltachat-rpc-server subprocess (adapted from draft PR add python generator #2); returns raw Python data for pydantic to process upstream
  • Abstract RpcTransport base class for custom transports

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: adbenitez <24558636+adbenitez@users.noreply.github.com>
Copilot AI changed the title [WIP] Add code generator for Python language in dcrpcgen CLI Add Python code generator (python subcommand) Mar 4, 2026
…Transport

Co-authored-by: adbenitez <24558636+adbenitez@users.noreply.github.com>
Copilot AI changed the title Add Python code generator (python subcommand) Add Python code generator using pydantic (python subcommand) Mar 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants