feat(python): add full type annotations support with default enabled#319
Open
mblsha wants to merge 1 commit intokaitai-io:masterfrom
Open
feat(python): add full type annotations support with default enabled#319mblsha wants to merge 1 commit intokaitai-io:masterfrom
mblsha wants to merge 1 commit intokaitai-io:masterfrom
Conversation
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
|
@mblsha I've rebased your work on top of master here: https://github.com/EmperorArthur/kaitai_struct_compiler/tree/python_type_annotations |
|
I have updated my branch to work with |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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):
Without annotations (--no-python-type-annotations):
--python-type-annotations: Explicitly enable (now redundant but supported)--no-python-type-annotations: Disable type annotations