Skip to content

Feature: Python renderer for Codama #557

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 20 commits into
base: main
Choose a base branch
from

Conversation

kamalbuilds
Copy link

Fixes #527

This package provides Python client renderers for Codama IDLs.

Installation

pnpm install @codama/renderers-python

Note

This package is not included in the main codama package.

Usage

// node ./codama.mjs
import { renderPythonVisitor } from '@codama/renderers-python';

codama.accept(renderPythonVisitor('clients/python/src/generated'));

Options

The following options can be provided when using the Python renderer:

type RenderOptions = {
  // The folder to delete before rendering.
  deleteFolderBeforeRendering?: boolean; // default: true
  
  // Whether to format the code with black.
  formatCode?: boolean; // default: true
  
  // Options to pass to the black formatter.
  blackOptions?: string[]; // default: ['--line-length', '88', '--target-version', 'py311']
  
  // Path to additional templates.
  extraTemplatesPath?: string;
  
  // Override import paths for different types of links.
  importOverrides?: LinkOverrides;
  
  // Map of dependencies.
  dependencyMap?: Record<string, string>;
  
  // Whether to render the root init file.
  renderRootInit?: boolean; // default: true
};

Generated client structure

The generated Python client has the following structure:

generated/
│
├── __init__.py              # Main entry point
├── programs.py              # Program IDs
│
├── accounts/                # Account definitions
│   ├── __init__.py
│   └── ...
│
├── instructions/            # Instruction definitions
│   ├── __init__.py
│   └── ...
│
├── errors/                  # Error definitions
│   ├── __init__.py
│   └── ...
│
├── types/                   # Type definitions
│   ├── __init__.py
│   └── ...
│
└── pdas/                    # PDA utilities
    ├── __init__.py
    └── ...

Dependencies

The generated Python client requires the following dependencies:

solana-py>=0.29.0

Examples

Basic usage

from solana.rpc.api import Client
from solana.publickey import Pubkey
from solana.keypair import Keypair
from solana.transaction import Transaction

from generated import PROGRAM_ID
from generated.instructions import SomeInstruction

# Create a connection to the Solana cluster
connection = Client("https://api.mainnet-beta.solana.com")

# Create an instruction
instruction = SomeInstruction(
    amount=1000,
    recipient=Pubkey("..."),
)

# Build the transaction
tx = Transaction()
tx.add(instruction.get_instruction())

# Sign and send the transaction
result = connection.send_transaction(tx, Keypair.from_secret_key([...]))
print(f"Transaction signature: {result.value}")

Copy link

changeset-bot bot commented Apr 20, 2025

⚠️ No Changeset found

Latest commit: 58c6f74

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@kamalbuilds
Copy link
Author

Hey @lorisleiva , Can you please review these changes ?

Copy link
Member

@lorisleiva lorisleiva left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi there, I'm afraid I had a 5 minute look at this PR and I don't think it's ready to review yet.

The serialising and deserialising part of a renderer is one of the most important part and it is completely missing from the renderer — the TypeManifest should build that information in a composable way. If a Codec-like system doesn't exist in Python, it should be built first so Codama types can be generated properly.

I also don't think the ImportMap is used correctly (if at all).

@lorisleiva lorisleiva marked this pull request as draft April 25, 2025 08:42
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.

Python renderer
3 participants