Skip to content

Python reserved keyword 'class' blocking Weave installation#1

Merged
zbirenbaum merged 1 commit intozbirenbaum:masterfrom
andrewmonostate:fix-class-keyword-bug
Nov 20, 2025
Merged

Python reserved keyword 'class' blocking Weave installation#1
zbirenbaum merged 1 commit intozbirenbaum:masterfrom
andrewmonostate:fix-class-keyword-bug

Conversation

@andrewmonostate
Copy link

This bug prevents the installation and usage of Weights & Biases Weave in python 3.9 where compiling happens at install because polyfile-weave is a dependency of Weave. When Weave tries to import polyfile, it fails with a SyntaxError, making Weave completely unusable.

How This Was Discovered

While trying to use Weave for ML experiment tracking, we encountered:

>>> import weave
...
File "polyfile/kaitai/parsers/openpgp_message.py", line 370
    self.class = self._io.read_u1()
         ^^^^^
SyntaxError: invalid syntax

This import error cascades up and prevents Weave from functioning.

The Problem

The Kaitai Struct compiler generates invalid Python code in polyfile/kaitai/parsers/openpgp_message.py. It uses self.class = ... which causes a SyntaxError because class is a reserved keyword in Python (since Python 1.0).

Impact

  • Blocks Weave installation/usage - Critical for ML practitioners using W&B
  • Affects any Python project depending on polyfile-weave
  • Prevents parsing of OpenPGP messages

Solution

This PR adds automatic patching that fixes the issue transparently on import, ensuring both polyfile-weave and its dependents (like Weave) work out-of-the-box.

Changes Made

  1. polyfile/__init__.py: Added _fix_class_keyword_if_needed() function that:

    • Detects if openpgp_message.py uses the reserved keyword class
    • Automatically replaces self.class with self.class_ before module imports
    • Runs silently without breaking imports if any issues occur
    • Ensures Weave can successfully import polyfile
  2. polyfile/kaitai/parsers/openpgp_message.py: Auto-fixed by the import hook:

    • Line 370: self.classself.class_
    • Line 361: SEQ_FIELDS = ["class"...]SEQ_FIELDS = ["class_"...]
    • Lines 369, 371: ['class']['class_']
  3. fix_class_keyword.py: Standalone script for manual fixing if needed

  4. README.md: Updated with Known Issues section

  5. .gitignore: Added *.egg-info/ to ignore build artifacts

Testing

The fix has been tested and successfully:

  • Allows Weave to be imported and used normally
  • Allows polyfile-weave to be imported without errors
  • Preserves all functionality
  • Works with all Python versions (3.9 through 3.13 tested)

Verification

# Before fix:
>>> import weave  # SyntaxError!

# After fix:
>>> import weave  # ✅ Works!
>>> weave.init("my-project")  # ✅ Works!

Related Fixes

Note

This is a workaround for a bug in the Kaitai Struct compiler. Once the compiler is fixed upstream and new parsers are generated, this auto-fix will no longer be necessary but will remain harmless if left in place.

Problem: Kaitai Struct compiler generates code using 'class' as a variable name,
causing SyntaxError on import. This affects openpgp_message.py lines 361, 369-371.

Solution: Added automatic patching in polyfile/__init__.py that:
- Detects if openpgp_message.py uses the reserved keyword
- Automatically replaces self.class with self.class_ before module imports
- Runs silently without breaking imports if any issues occur
- Ensures the package works out-of-the-box without manual intervention

Changes made:
- polyfile/__init__.py: Added _fix_class_keyword_if_needed() auto-fix function
- polyfile/kaitai/parsers/openpgp_message.py: Auto-fixed by the import hook
- fix_class_keyword.py: Standalone script for manual fixing if needed
- .gitignore: Added *.egg-info/ to ignore build artifacts

Tested and verified: The auto-fix successfully patches the file on first import.

Fixes import errors in polyfile-weave v0.1.6 and earlier.
@zbirenbaum
Copy link
Owner

Thanks for this! sorry I seem to have missed the notification

@zbirenbaum zbirenbaum merged commit a32fdf3 into zbirenbaum:master Nov 20, 2025
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.

2 participants