Skip to main content

Prescrypt

A Python to JavaScript transpiler for a well-defined subset of Python.

Status: Alpha. Use at your own risks.

Overview

Prescrypt converts Python 3.9+ code to ES6+ JavaScript. It prioritizes correctness over completeness, targeting common use cases rather than full Python compatibility.

Features

  • Modern Python support - Python 3.10+ with pattern matching
  • Modern JavaScript output - Targets ES6+ with const/let, arrow functions, classes
  • Comprehensive test suite - 2400+ tests covering expressions, statements, and full programs
  • Optimized output - Tree-shaking, constant folding, function inlining
  • Source locations in errors - Clear error messages with file:line:column

Installation

pip install prescrypt
# or
uv add prescrypt

Quick Start

As a library

from prescrypt import py2js

code = """
def greet(name: str) -> str:
    return f"Hello, {name}!"

print(greet("World"))
"""

js = py2js(code)
print(js)

Output (stdlib helpers omitted):

function greet(name) {
    return ('Hello, ' + name + '!');
}
console.log(greet('World'));

Note: Type annotations (name: str) enable optimized output. Without them, the compiler uses runtime helpers for Python-compatible behavior.

From the command line

py2js input.py           # Creates input.js
py2js input.py output.js # Explicit output path

Supported Python Features

Fully Supported

  • Data types: int, float, bool, str, None, list, dict, tuple
  • Control flow: if/elif/else, for, while, break, continue
  • Exception handling: try/except/finally, raise
  • Functions: def, lambda, *args, default arguments, closures
  • Classes: class, __init__, inheritance, super(), @property
  • Decorators: @staticmethod, @classmethod, @property
  • Comprehensions: list, dict, set, generator expressions
  • Generators: yield, yield from, send(), throw(), close()
  • Operators: arithmetic, comparison, logical, membership (in)
  • Builtins: print, len, range, enumerate, zip, min, max, sorted, etc.

Partially Supported

  • **kwargs - in function calls only (not in function definitions)
  • with statement - single context manager only
  • async def - generates async functions (await not yet implemented)

Not Supported

  • Metaclasses
  • Multiple inheritance
  • exec(), eval()
  • Most of the standard library

Architecture

Python Source → Parse → Desugar → Bind → Optimize → CodeGen → JavaScript
  • Parse: Python's ast module with custom extensions
  • Desugar: Simplifies syntax (e.g., a += b → a = a + b)
  • Bind: Scope analysis, determines const vs let
  • Optimize: Constant folding, dead code elimination
  • CodeGen: Produces JavaScript output

Development

# Install dependencies
uv sync

# Run tests
make test

# Run linter
make lint

# Format code
make format

License

BSD 2-Clause License. See LICENSE for details.

Acknowledgments

Prescrypt was inspired by PScript, originally developed as part of Flexx. While the codebase has been largely rewritten, we acknowledge PScript's pioneering work in Python-to-JavaScript transpilation.

Metadata

Release files for prescrypt 0.9.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for prescrypt 0.9.6
File Size Uploaded
prescrypt-0.9.6.tar.gz 152.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for prescrypt 0.9.6
File Interpreter ABI Platform
prescrypt-0.9.6-py3-none-any.whl Python 3 none any Details

Total release size: 333.4 kB

Release files / prescrypt-0.9.6.tar.gz

Download URL prescrypt-0.9.6.tar.gz
Size 152.5 kB
Tags Source
SHA-256 checksum
How to use checksums
52573445da8b50d4205df462017f8152e01bfdc62bcf372794834fb2af8ae9ca
BLAKE2b-256 checksum
How to use checksums
050fddc1dcd829523cd65a6f27ddffbeaf816722d6c9f06f47fa5a344d8cda2d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.2

Release files / prescrypt-0.9.6-py3-none-any.whl

Download URL prescrypt-0.9.6-py3-none-any.whl
Size 180.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5a8101f31bc55763e837456446fc80db58887f6128e3ae83b0b372185f311d8f
BLAKE2b-256 checksum
How to use checksums
d26bc35c4b9f791581fcad9e27b241861f314c01c581a2ccebaaf14513068215
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.2

Release history Release notifications | RSS feed

This release

0.9.6 This release

2 release files

0.9.5

2 release files

0.9.3

2 release files

0.9.2

2 release files

0.9.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page