# Pre-Submit Self-Review — Python

Python-specific checks for the pre-submit review. Read `${CLAUDE_PLUGIN_ROOT}/references/shared/pre-submit-review.md` first for the language-agnostic checklist.

## Resource Ownership — Python Patterns

```bash
# Find all callers of a function you modified
git diff <base>..<branch> --name-only | xargs grep -n "function_name("
# For each caller: does it use the object after the call?
```

Watch for:
- `del` on objects passed as parameters
- `.close()` on file handles, images, or connections that the caller may still reference
- Context manager changes (`with` blocks) that change resource lifetime
- Early returns that skip cleanup of caller-owned resources

## Concurrency — Python Patterns

- **`asyncio.run()` from existing loop:** Never call `asyncio.run()` in code that may already be in an async context. Use `asyncio.get_event_loop().run_in_executor()` or check for a running loop first.
- **Module-level caches and globals:** `dict`, `list`, and other mutable objects at module scope are shared across threads in WSGI/ASGI servers. Use `threading.Lock` or `asyncio.Lock` as appropriate.
- **`.pyc` contamination:** After reverting changes, stale `.pyc` files can mask regressions. Delete `__pycache__` directories if you suspect stale bytecode.

## Feature Flags & Alternate Paths

Check for Python-specific conditional paths that your tests might miss:

- `if os.environ.get(...)` — environment-driven feature flags
- `try: import fast_lib except: import slow_lib` — conditional imports
- Monkey-patching consumers (common in test frameworks)
- `__init__.py` re-exports that may expose different implementations

```bash
# Find conditional paths in modified files
grep -n "os.environ\|importlib\|__import__\|monkey" <modified-files>
```

## Type Safety

If the codebase uses type hints:
- Did a container type change (e.g., `list` → `set`) break iteration order guarantees?
- Did a return type change (e.g., generator vs list)?
- Run `mypy` or `pyright` if the project uses them — check `pyproject.toml` for type checker config.

## Caller Discovery

```bash
# Find all callers of modified functions
grep -rn "function_name(" --include="*.py" .
```
