Skip to content

Modernize codebase for Python 3.13+#13

Merged
emmapowers merged 3 commits into
mainfrom
modernize/2-python-modernization
Apr 14, 2026
Merged

Modernize codebase for Python 3.13+#13
emmapowers merged 3 commits into
mainfrom
modernize/2-python-modernization

Conversation

@emmapowers

@emmapowers emmapowers commented Apr 14, 2026

Copy link
Copy Markdown
Owner

Summary

  • Add runtime types module (bakelite/proto/types.py) for use in generated code without heavy dependencies
  • Update type hints to modern syntax (X | None, list[X])
  • Apply ruff formatting and linting fixes
  • Rename arraySize to array_size for snake_case consistency
  • Fix parser bug with bytes[] (variable-length) parsing
  • Add comprehensive test coverage (CLI, parser, CRC, framing, serialization)
  • Use Protocol class for type checking instead of type: ignore

Test plan

  • pixi run pytest passes (116 tests)
  • pixi run ruff check clean
  • pixi run mypy clean

Summary by CodeRabbit

Release Notes

  • Bug Fixes

    • Corrected error messages: "Unkown" → "Unknown" for invalid language and type errors.
  • Tests

    • Added comprehensive test coverage for CLI code generation, protocol parsing, CRC functions, and serialization error handling.
  • Refactor

    • Modernized type annotations across all modules to use Python 3.10+ syntax (e.g., list[...] instead of typing.List[...]).
    • Introduced runtime-checkable protocols (PackableProtocol, EnumProtocol) for improved type safety.
    • Improved internal code organization with consistent naming conventions and docstrings throughout.

@coderabbitai

coderabbitai Bot commented Apr 14, 2026

Copy link
Copy Markdown

Caution

Review failed

Pull request was closed or merged during review

📝 Walkthrough

Walkthrough

This PR modernizes the Bakelite codebase by migrating to Python 3.10+ syntax, updating type annotations to use builtin generics and union operators, introducing new runtime descriptor types, refactoring serialization decorators to use protocols instead of JSON strings, and adding comprehensive test coverage.

Changes

Cohort / File(s) Summary
Package initialization
bakelite/__init__.py, bakelite/generator/__init__.py, bakelite/proto/__init__.py
Added module docstrings and replaced pkg_resources.get_distribution() with importlib.metadata.version() for version lookup. Extended proto exports to include types module.
Type system modernization
bakelite/generator/types.py, bakelite/generator/parser.py
Migrated all type annotations from typing.* to Python 3.10+ builtin generics (list[...], tuple[...]) and union syntax (X | None). Renamed arraySize to array_size in ProtoStructMember. Introduced PRIMITIVE_TYPES constant for type checks. Updated function signatures for parse() and validate() to handle nullable Protocol.
Code generators
bakelite/generator/cli.py, bakelite/generator/cpptiny.py, bakelite/generator/python.py
Updated type annotations throughout generators. Modified CLI to parse proto before language dispatch, eliminating duplicate parsing. Updated member references from arraySize to array_size. Added runtime_import parameter to Python and C++ Tiny render functions. Fixed typo "Unkown" → "Unknown".
Utility
bakelite/generator/util.py
Simplified conditional logic in to_camel_case to single-line expression.
Runtime protocols & descriptors
bakelite/proto/crc.py, bakelite/proto/framing.py, bakelite/proto/types.py
Added module docstrings, typed annotations, and helper functions. Introduced new ProtoType, ProtoStruct, ProtoEnumDescriptor, and ProtocolDescriptor descriptor classes for type-safe protocol representation. Updated framing to use bytes | None return type.
Serialization refactoring
bakelite/proto/serialization.py
Introduced PackableProtocol and EnumProtocol for structural typing. Refactored @struct and @enum decorators to accept ProtoStruct | str and ProtoEnumDescriptor | str instead of JSON strings. Renamed arraySize to array_size. Updated enum packing/unpacking to use registered enum classes. Simplified control flow and fixed typo "Unkown" → "Unknown".
Test suite additions
bakelite/tests/generator/test_cli.py, bakelite/tests/generator/test_parser.py, bakelite/tests/proto/test_crc.py, bakelite/tests/proto/test_serialization.py, bakelite/tests/proto/test_validation.py, bakelite/tests/proto/test_protocol.py
Added comprehensive test coverage for CLI commands, parser behavior, CRC implementations, serialization, and validation. Updated existing tests to pass runtime_import="bakelite.proto" to render functions and use ProtoStruct descriptors instead of JSON.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related PRs

  • Migrate from Poetry to Pixi + Hatchling #12: Shares related protocol parsing and serialization refactoring, including changes to how array sizes are handled and represented (arraySize to array_size migration), affecting both parser and downstream type handling.

Poem

🐰 With types so modern and names so clean,
From JSON strings to protocols keen,
Docstrings blooming, tests taking flight,
Bakelite hops to Python's light! ✨
Array sizes snake-case-bound,
A codebase polished and sound.

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 21.90% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title 'Modernize codebase for Python 3.13+' accurately reflects the main objective described across the changeset: systematic updates to type hints, syntax modernization, and linting fixes for Python 3.13+ compatibility.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Comment @coderabbitai help to get the list of available commands and usage tips.

@emmapowers
emmapowers force-pushed the modernize/2-python-modernization branch 2 times, most recently from 51ddf34 to fee9b06 Compare April 14, 2026 14:53
Base automatically changed from modernize/1-build-system to main April 14, 2026 15:26
Separate type descriptors (ProtoType, ProtoStruct, etc.) into
bakelite/proto/types.py for use in generated code without
pulling in external dependencies like dataclasses_json.
- Update type hints to modern syntax (X | None, list[X])
- Apply Ruff formatting and linting fixes
- Rename arraySize to array_size for snake_case consistency
- Fix parser bug with bytes[] (variable-length) parsing
- Add comprehensive test coverage (CLI, parser, CRC)
- Use Protocol class for type checking instead of type: ignore
- Carry forward review fixes (size=None, conftest plugin name, typos)
- Modernize python.py types (List → list, Optional → |, arraySize → array_size)
- Add runtime_import parameter to render() for generated code compatibility
- Fix struct/enum decorators to accept JSON strings from generated code
- Skip runtime CLI tests (feature added in later PR)
- Refactor cli.py gen command to avoid function pointer type mismatch
@emmapowers
emmapowers force-pushed the modernize/2-python-modernization branch from fee9b06 to 34a256e Compare April 14, 2026 15:33
@emmapowers
emmapowers merged commit 8c6ce20 into main Apr 14, 2026
2 of 3 checks passed
@emmapowers
emmapowers deleted the modernize/2-python-modernization branch April 14, 2026 15:44
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