Skip to content

Conversation

@mblsha
Copy link

@mblsha mblsha commented Jul 13, 2025

This commit implements comprehensive Python type annotation support for the Kaitai Struct compiler, making it the default behavior for modern Python development while maintaining backward compatibility.

  • Type Annotations by Default: Python code now generates with full type annotations enabled by default

  • Comprehensive Coverage: Type annotations for constructors, methods, properties, and all data types

  • Modern Python Support: Imports typing module and uses modern annotation syntax

  • Backward Compatibility: New --no-python-type-annotations flag to disable when needed

  • Added pythonTypeAnnotations: Boolean = true to RuntimeConfig (default: true)

  • Created comprehensive kaitaiTypeToPythonType() mapping function

  • Enhanced constructor with full parameter and instance variable annotations

  • Added return type annotations for all methods (_read, properties, repr)

  • Conditional typing imports only when annotations enabled

  • Removed "# type: ignore" comment when annotations are present

With type annotations (default):

from typing import Any, List, Optional, Union

def __init__(self, _io: 'KaitaiStream', _parent: 'KaitaiStruct' = None,
             _root: Optional['KaitaiStruct'] = None) -> None:
    self._io: 'KaitaiStream' = _io
    self._parent: 'KaitaiStruct' = _parent

@property
def calculated_value(self) -> int:
    return self.magic * 2

Without annotations (--no-python-type-annotations):

def __init__(self, _io, _parent=None, _root=None):
    self._io = _io
    self._parent = _parent
  • --python-type-annotations: Explicitly enable (now redundant but supported)
  • --no-python-type-annotations: Disable type annotations
  • Default behavior: Type annotations enabled

This commit implements comprehensive Python type annotation support for the
Kaitai Struct compiler, making it the default behavior for modern Python
development while maintaining backward compatibility.

- **Type Annotations by Default**: Python code now generates with full type
  annotations enabled by default
- **Comprehensive Coverage**: Type annotations for constructors, methods,
  properties, and all data types
- **Modern Python Support**: Imports typing module and uses modern annotation
  syntax
- **Backward Compatibility**: New --no-python-type-annotations flag to disable
  when needed

- Added pythonTypeAnnotations: Boolean = true to RuntimeConfig (default: true)
- Created comprehensive kaitaiTypeToPythonType() mapping function
- Enhanced constructor with full parameter and instance variable annotations
- Added return type annotations for all methods (_read, properties, __repr__)
- Conditional typing imports only when annotations enabled
- Removed "# type: ignore" comment when annotations are present

**With type annotations (default):**
```python
from typing import Any, List, Optional, Union

def __init__(self, _io: 'KaitaiStream', _parent: 'KaitaiStruct' = None,
             _root: Optional['KaitaiStruct'] = None) -> None:
    self._io: 'KaitaiStream' = _io
    self._parent: 'KaitaiStruct' = _parent

@Property
def calculated_value(self) -> int:
    return self.magic * 2
```

**Without annotations (--no-python-type-annotations):**
```python

def __init__(self, _io, _parent=None, _root=None):
    self._io = _io
    self._parent = _parent
```

- `--python-type-annotations`: Explicitly enable (now redundant but supported)
- `--no-python-type-annotations`: Disable type annotations
- Default behavior: Type annotations enabled
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