Skip to main content

A Python static analysis tool that detects heavy initialization inside AWS Lambda handlers that should be moved to module scope for faster warm starts.

Project description

pythaw

日本語ドキュメント

A Python static analysis tool that detects heavy initialization inside AWS Lambda handlers that should be moved to module scope for faster warm starts.

Recursively follows function calls—including across imported files—to catch indirect violations.

Requirements

Python 3.10 - 3.14 — matching the actively supported AWS Lambda Python runtimes.

Install

# pip
pip install pythaw

# uv
uv add pythaw

Quick Start

# handler.py

def lambda_handler(event, context):
    # BAD: Creating a boto3 client inside the handler
    #      runs initialization on every invocation,
    #      losing the benefit of warm starts.
    client = boto3.client("s3")
    return client.get_object(Bucket="my-bucket", Key=event["key"])
$ pythaw check handler.py
infra/aws.py:7:15: PW001 boto3.client() should be called at module scope
  → handler.py:5:11 process()
    → service.py:5:13 S3Provider.get_client()

Found 1 violation in 1 file.

Move the client to module scope so Lambda container reuse skips the initialization:

# handler.py (fixed)

client = boto3.client("s3")

def lambda_handler(event, context):
    return client.get_object(Bucket="my-bucket", Key=event["key"])
$ pythaw check handler.py
All checks passed!

Usage

pythaw check <path>                    # Check a file or directory
pythaw check . --format json           # JSON output
pythaw check . --format github         # GitHub Actions annotation format
pythaw check . --format sarif          # SARIF format (Code Scanning integration)
pythaw check . --select PW001,PW002    # Enable only specific rules
pythaw check . --ignore PW003          # Disable specific rules
pythaw check . --exit-zero             # Always exit with code 0
pythaw check . --statistics            # Show per-rule violation counts
pythaw rules                           # List built-in rules
pythaw rule PW001                      # Show rule details

Exit Codes

Code Meaning
0 No violations found
1 Violations found
2 Tool error (invalid config, etc.)

Rules

ID Detects
PW001 boto3.client()
PW002 boto3.resource()
PW003 boto3.Session()
PW004 pymysql.connect()
PW005 psycopg2.connect()
PW006 redis.Redis()
PW007 redis.StrictRedis()
PW008 httpx.Client()
PW009 requests.Session()

Call Graph Traversal

pythaw recursively follows local function calls and imported modules from the handler, detecting indirect violations across files.

Supported patterns

Pattern Example
Same-file function call helper()
Module-qualified function call infra.get_client()
Class method call AwsProvider.get_client()
Class instantiation (__init__) S3Client()
Cross-file import tracking from infra import get_client

Note: Instance method calls via variables (e.g. obj = Cls(); obj.method()) are not tracked — this would require data-flow analysis beyond the current scope.

infra/aws.py:4:15: PW001 boto3.client() should be called at module scope
  → handler.py:2:10 get_client()

Found 1 violation in 1 file.

Suppression

Inline suppression

Append # nopw: <code> to a line to suppress that violation. Multiple codes can be comma-separated.

client = boto3.client("s3")  # nopw: PW001

File-level suppression

Add # pythaw: nocheck in the leading comment block to skip the entire file.

# pythaw: nocheck
import boto3

def handler(event, context):
    boto3.client("s3")  # not checked

Configuration

Configure via the [tool.pythaw] section in pyproject.toml.

[tool.pythaw]
# Function name patterns recognized as handlers (fnmatch syntax)
handler_patterns = ["handler", "lambda_handler", "*_handler"]

# Patterns to exclude from scanning
exclude = [".venv", "tests"]

# Disable specific rules per file pattern
[tool.pythaw.per-file-ignores]
"tests/*" = ["PW001", "PW002"]
"scripts/*" = ["PW001"]

License

MIT

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

pythaw-0.1.0.tar.gz (16.0 kB view details)

Uploaded Source

Built Distribution

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

pythaw-0.1.0-py3-none-any.whl (27.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pythaw-0.1.0.tar.gz
  • Upload date:
  • Size: 16.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.8

File hashes

Hashes for pythaw-0.1.0.tar.gz
Algorithm Hash digest
SHA256 711c856e4164cb6bd8d9ec6a0b9397d228da227cbe600eeed4d5e7ca43612f8c
MD5 b8c5de76c21688cb65c18ae8d0f90113
BLAKE2b-256 1326e0a0210e41345a1fbcac127a95070abd887509dbecd8357a2af25e08e93a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pythaw-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 27.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.8

File hashes

Hashes for pythaw-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c678917223e5cfe7e88d5ec57e69c8f60e35c33a289c2d4c36e9e2e3f9950983
MD5 40d8d5cae2b4737e221bc44d8952a75e
BLAKE2b-256 18f1a5f41984dcd8e7364f4eb39c238723d1e26ee3a465db924f6a5331c7690e

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