Skip to content

[DEV] (2.5.0 rc) Python type hints#30

Merged
MatrixEditor merged 19 commits into
dev/2.5.0-rcfrom
dev/2.5.0-rc_stubs
Jun 25, 2025
Merged

[DEV] (2.5.0 rc) Python type hints#30
MatrixEditor merged 19 commits into
dev/2.5.0-rcfrom
dev/2.5.0-rc_stubs

Conversation

@MatrixEditor

@MatrixEditor MatrixEditor commented Jun 24, 2025

Copy link
Copy Markdown
Owner

See #31 for changes

---

abc.py:
+ removed unused '_ContextPathStr'
+ Removed unnecessary 'abstractmethod' definitions
+ '_Action' was renamed to '_ActionLike'
+ removed '_EnumLike'
+ '_Switch' was renamed to '_SwitchLike'
+ Added two new protocols: '_SupportsBits' and '_ContainsBits'
+ The following constants and methods were moved into shared.py: STRUCT_FIELD -> ATTR_STRUCT, hasstruct, getstruct and typeof
---

_common.py:
+ Removed any existing typing information
+ unpack_seq and pack_seq now return or use a collection instead of a list
---

byteorder.py:
+ moved 'BYTEORDER_FIELD' to shared.py as ATTR_BYTEORDER
---

+ shortcuts now include typeof, to_struct, hasstruct, getstruct and sizeof
---

+ Removed unnecessary type hints in python code
+ Fixed an issue with unions declared using the Sequence class
---

_struct.py:
+ Removed type hints in original python file
+ Sizeof() now raises a TypeError when the target object does not implement the __size__ protocol
---

_bitfield.py:
+ Moved attribute constants to caterpillar.shared.
+ Removed getformat().
+ Removed type hints from the original file.
+ Fixed issue where field options defined in BitField were not populated when creating fields.

_base.py:
+ Fixed issue where field options defined in Sequence were not populated when creating fields.
+ Added private methods to stubs.
---

_template.py:
+ Moved template attribute constant to shared.py
+ removed all type hints

+ Updated imports in _bitfield, _struct and _base
---

_base.py:
+ Removed all type hints
---

fields.common.py:
+ Transformer: removed __fmt__ method

fields.compression.py:
+ Updated public compression methods to use lazy imports
@MatrixEditor MatrixEditor marked this pull request as ready for review June 25, 2025 13:48
---

abc.py:
+ Removed __type__() requirement from '_StructLIke' as described in the docs
+ Added new protocol: _SupportsType
@MatrixEditor MatrixEditor merged commit e12f13b into dev/2.5.0-rc Jun 25, 2025
12 checks passed
@MatrixEditor MatrixEditor deleted the dev/2.5.0-rc_stubs branch June 25, 2025 18:50
MatrixEditor added a commit that referenced this pull request Jun 29, 2025
---

## Changes made

The code of all modules were refactored to include as little type hints as possible now. All typing related information should be taken from `.pyi` files.

##### `caterpillar.abc`

- Removed `_Action` and split into two separate Protocols `_ActionLike` := `_SupportsActionUnpack` | `_SupportsActionPack`
- Renamed `_Switch` to `_SwitchLike`
- Removed `_EnumLike`
- Added two new protocols: '_SupportsBits' and '_ContainsBits'
- The following attributes and methods were moved into [caterpillar.shared](#caterpillarshared): `STRUCT_FIELD` -> `ATTR_STRUCT`, `hasstruct`, `getstruct` and `typeof`
-  Removed unused '_ContextPathStr'
- Removed `__type__()` requirement from '_StructLike'
- Added new protocol: `_SupportsType`

##### `caterpillar.byteorder`

- Moved 'BYTEORDER_FIELD' to [caterpillar.shared](#caterpillarshared) as `ATTR_BYTEORDER`

##### `caterpillar.shortcuts`

- Shortcuts now include `typeof`, `to_struct`, `hasstruct`, `getstruct` and `sizeof`

##### `caterpillar.shared`

- New constants moved from other modules: `ATTR_BYTEORDER`, `ATTR_TYPE`, `ATTR_BITS`, `ATTR_SIGNED`, `ATTR_TEMPLATE`

##### `caterpillar.context`

- New context attribute: '_root' can be set to point to the root context instance. Internally, instead of a for-loop that iterates through parent context instances, a simple self.get(...) call is made. (see #35)

##### `caterpillar.model._base`

- Fixed an issue when parsing union objects with an unbound stream object
- Fixed an issue where field options defined in Sequences were not populated when creating fields.

##### `caterpillar.model._struct`

- `sizeof` now checks if the provided object implements the `_SupportsSize` protocol

##### `caterpillar.model._bitfield`

- **Completely reworked the bitfield mechanism to make it even more powerful. For details refer to #34.**
- Fixed an issue where field options defined in BitFields were not populated when creating fields.
- Moved `BITS_ATTR` and `SIGNED_ATTR` into [caterpillar.shared](#caterpillarshared)
- Removed unused `getformat` function

##### `caterpillar.fields.common`

- `Transformer`: removed `__fmt__` method

##### `caterpillar.fields.compression`

- Updated public compression methods to use lazy imports

#### Documentation

- Updated library documentation to reflect changes made to function signatures
- Updated the reference to explicitly state the Protocols defined in `caterpillar.abc`
- Created a change log to reflect changes made up to v2.5.0

## Associated Issues and Pull Requests

- #30: Stub files for Caterpillar allowing to use generics and more detailed return types
- #27: Python3.14 does NOT break this project. However, some features (with-statements) can't be used
- #34: Introducing a new Bitfield concept to make it even more flexible
- #35: Optimized resolving the root context instance
- #37: Various small fixes related to the documentation
- #24: Bitfield docs
- #33: General documentation updates
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant