Skip to main content

No project description provided

Project description

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 - basic support
  • with statement - single context manager
  • async/await - basic support

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 += ba = 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

prescrypt-0.9.1.tar.gz (129.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

prescrypt-0.9.1-py3-none-any.whl (156.8 kB view details)

Uploaded Python 3

File details

Details for the file prescrypt-0.9.1.tar.gz.

File metadata

  • Download URL: prescrypt-0.9.1.tar.gz
  • Upload date:
  • Size: 129.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for prescrypt-0.9.1.tar.gz
Algorithm Hash digest
SHA256 b69f208441ce93b007257fd93ac0758b4fadf0188f2a413a18c9e6d6a1c6274f
MD5 e726f2f80e41137ce3432cbd904e23fc
BLAKE2b-256 dc2368ee604c76893b033396709fad99b690304e79387ce971d04a3c88d74b28

See more details on using hashes here.

File details

Details for the file prescrypt-0.9.1-py3-none-any.whl.

File metadata

  • Download URL: prescrypt-0.9.1-py3-none-any.whl
  • Upload date:
  • Size: 156.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for prescrypt-0.9.1-py3-none-any.whl
Algorithm Hash digest
SHA256 bea5caebaf502abd4c0bc1a3e870f9d730dc3be92a93830935a747ba21e2616c
MD5 36aece559c93b1488f7c640b40778035
BLAKE2b-256 c8751b4f5a76dce2fb832a19801fd9b26ade83bec9475dedbdac0faacd20ead2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page