Skip to main content

AST-based Python compatibility checker for the Monty interpreter

Project description

monty-compat

AST-based Python compatibility checker for the Monty sandbox.

Parses the Monty Rust source to extract every implemented builtin function, type constructor, exception type, and stdlib module, then checks arbitrary Python code for unsupported features — without executing it.

Installation

pip install monty-compat

Or for development (from checkout):

pip install -e .

Quick start

from monty_compat import monty_compat

# One-shot check — loads capabilities from cache (or builds on first run)
ok, reasons = monty_compat("x = [i * 2 for i in range(10)]")
# ok=True, reasons=[]

ok, reasons = monty_compat("import json; json.loads('{}')")
# ok=False, reasons=["module 'json' is not supported by Monty"]

# Force rebuild (ignores cache)
ok, reasons = monty_compat(code, cache='regenerate')

# Skip cache entirely, read from local monty checkout
ok, reasons = monty_compat(code, cache='off', monty_root='/path/to/monty')

Cache

Capabilities are cached at ~/.monty_compat/monty_{version}_compat.json.

  • Default TTL: 12 hours
  • Cache is version-keyed — each installed pydantic-monty version gets its own file
  • cache='regenerate' forces a rebuild and overwrites the cache
  • cache='off' skips all cache I/O

Lower-level API

from monty_compat import MontyCapabilities

# Build from GitHub (downloads ZIP in memory)
caps = MontyCapabilities.from_github()

# Build from a local monty repo checkout
caps = MontyCapabilities.from_local('/path/to/monty')

# Inspect capabilities
caps.builtin_functions    # frozenset: abs, all, any, bin, chr, …
caps.type_constructors    # frozenset: int, str, list, dict, …
caps.exception_types      # frozenset: ValueError, TypeError, …
caps.modules              # frozenset: sys, typing, asyncio, pathlib, os, re, …
caps.module_attributes    # dict:      {'asyncio': {'gather', 'run'}, …}

# Check code
ok, reasons = caps.check_code(some_code)

# Pretty-print
print(caps.summary())

What is checked

Pattern Example Check
import X import json Is json a supported module?
from X import Y from asyncio import subprocess Is Y in X's known attributes?
Builtin calls eval(...) Is the builtin implemented in Monty?

Supported modules (as of 2026-03)

Module Available attributes
re search, match, sub, findall, …
asyncio gather, run
os getenv, environ
pathlib Path
sys platform, version, version_info, stdout, stderr
typing Any, Optional, Union, List, Dict, Callable, …

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

monty_compat-0.1.0.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

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

monty_compat-0.1.0-py3-none-any.whl (11.4 kB view details)

Uploaded Python 3

File details

Details for the file monty_compat-0.1.0.tar.gz.

File metadata

  • Download URL: monty_compat-0.1.0.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.7

File hashes

Hashes for monty_compat-0.1.0.tar.gz
Algorithm Hash digest
SHA256 979bf92ff35451f4d325062b2c259e0b55e4c6f2e492d725b738af773a6dbbd3
MD5 b5bcc00d4a54573b6cf1810ac85b13b1
BLAKE2b-256 1103c770319f7e27fd83ae0582f47f12942a34253fa3c87b81ed71d9f3de4293

See more details on using hashes here.

File details

Details for the file monty_compat-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: monty_compat-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 11.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.7

File hashes

Hashes for monty_compat-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4f3bfe3b5d54d5a301faf6b2e1614247fa89f46f1e8aadc6a6bf565c660df60c
MD5 bed3cba101c5357381faaf9af8514acd
BLAKE2b-256 a3f62c4b3066e98ebfa6ce192fe1e813390c81f49a5edf37fdc0a0d80d3c3d64

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