Skip to content

Stop tracking compiled Python files and forbid committing them - #32

Merged
ludgerpaehler merged 2 commits into
mainfrom
chore/untrack-pyc
Oct 11, 2026
Merged

ludgerpaehler merged 2 commits into
mainfrom
chore/untrack-pyc

Conversation

@ludgerpaehler

Copy link
Copy Markdown
Collaborator

Summary

Nine compiled Python files were tracked under koopmanrl/environments/__pycache__/ and koopmanrl/koopman_tensor/__pycache__/. Two of them were the bytecode of the cartpole environments, which have since been removed. They showed up as modified after every run and gave every clone stale bytecode. This PR stops tracking them and ignores compiled files in every directory. It also adds a pre-commit hook, so a compiled file that someone force-adds fails both locally and in CI.

Changes

  • Remove the tracked .pyc files (git rm): 7 under koopmanrl/environments/__pycache__/ (including cartpole_control_env and continuous_cartpole) and 2 under koopmanrl/koopman_tensor/__pycache__/. Python regenerates them on import.
  • .gitignore: the six per-directory <dir>/__pycache__/ entries are replaced by one general rule under the existing # Ignore PyCaches header. The old entries missed koopmanrl/koopman_tensor/ and observables/, which is how these files got in. The new rule is:
    __pycache__/
    *.py[cod]
    
    No other entries change.
  • .pre-commit-config.yaml: a new repo: local hook, forbid-compiled-python (language: fail, files: '(\.py[cod]$|(^|/)__pycache__/)'). It rejects any .pyc, .pyo or .pyd file, and any file under a __pycache__/ directory, including one at the top level. .gitignore cannot stop git add -f, but this hook does. .github/workflows/lint.yml runs uv run pre-commit run --all-files, which checks the tracked files, so a compiled file that is committed anyway also fails the Lint workflow.
  • Guides:
    • koopmanrl/environments/AGENTS.md: the bullet that warned about tracked .pyc files now says they are gitignored and rejected by the hook.
    • Root AGENTS.md: the hook is added to the list of pre-commit hooks, and compiled files are added to the list of ignored files.
    • docs/docs/developers/contributing.md: the hook is added to the list of hooks.

Test plan

  • uv run pre-commit run --all-files passes on the branch. All hooks pass; forbid-compiled-python reports "no files to check".
  • The hook works: I force-added a dummy koopmanrl/__pycache__/dummy.cpython-310.pyc. Both uv run pre-commit run --files <it> and --all-files then fail with "Compiled Python files (.pyc, .pyo, .pyd, pycache/) must not be committed". I removed the dummy afterwards.
  • After uv run python -c "import koopmanrl.environments, koopmanrl.koopman_tensor.torch_tensor", git status is clean. git status --ignored lists the new __pycache__/ directories as ignored.
  • Fresh-machine check on a git clone of the branch:
    • The clone contains no .pyc files and no __pycache__/ directories.
    • After uv sync, uv run python -c "import koopmanrl.environments" creates __pycache__/, and git status --ignored shows it as ignored (!!).
    • uv run pytest -q tests/test_koopman_tensor_regressors.py gives 17 passed. Its __pycache__/ directories under koopman_tensor/, observables/ and tests/ are also ignored.

🤖 Generated with Claude Code

ludgerpaehler and others added 2 commits October 10, 2026 21:19
Nine .pyc files under koopmanrl/environments/__pycache__/ and
koopmanrl/koopman_tensor/__pycache__/ were tracked, among them the
bytecode of the removed cartpole environments. They showed up as
modified after every run and shipped stale bytecode to every clone.
They are build artefacts that Python regenerates on import, so they are
removed from the repository.

.gitignore listed __pycache__/ per directory and missed
koopmanrl/koopman_tensor/ and its observables/ package, which is how
those files got in. The per-directory entries are replaced by a general
rule, __pycache__/ and *.py[cod], that covers every directory.

Ignoring does not stop a force-add, so a local pre-commit hook,
forbid-compiled-python, fails on any .pyc, .pyo, .pyd or file under a
__pycache__/ directory. CI runs `pre-commit run --all-files` on the
tracked files, so a compiled file that is committed anyway fails the
Lint workflow.

The guides describe the new state: koopmanrl/environments/AGENTS.md no
longer warns about tracked .pyc files, and the root AGENTS.md and the
contributing page of the docs list the new hook and the ignored
compiled files.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ludgerpaehler
ludgerpaehler merged commit ee8ecf4 into main Oct 11, 2026
2 checks passed
@ludgerpaehler
ludgerpaehler deleted the chore/untrack-pyc branch October 11, 2026 05:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant