Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/caterpillar/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import warnings

from typing import Annotated, Callable, Any, Generic, Protocol, get_origin
from typing_extensions import Final, Literal, Self, Sized, overload, override, TypeVar
from typing_extensions import Buffer, Final, Literal, Self, Sized, overload, override, TypeVar
from types import FrameType, TracebackType
from dataclasses import dataclass

Expand Down Expand Up @@ -105,7 +105,7 @@ def __getattribute__(self, key: Literal["_parent"]) -> _ContextLike: ...
@overload
def __getattribute__(self, key: Literal["_obj"]) -> _ContextLike: ...
@overload
def __getattribute__(self, key: Literal["_offsets"]) -> dict[int, int]: ...
def __getattribute__(self, key: Literal["_offsets"]) -> dict[int, Buffer]: ...
@overload
def __getattribute__(self, key: Literal["_io"]) -> _StreamType: ...
@overload
Expand Down Expand Up @@ -181,7 +181,7 @@ def __getitem__(self, key: Literal["_root"], /) -> _ContextLike: ...
@overload
def __getitem__(self, key: Literal["_io"], /) -> _StreamType: ...
@overload
def __getitem__(self, key: Literal["_offsets"], /) -> dict[int, int]: ...
def __getitem__(self, key: Literal["_offsets"], /) -> dict[int, Buffer]: ...
@overload
def __getitem__(self, key: Literal["_index"], /) -> int: ...
@overload
Expand Down Expand Up @@ -621,7 +621,7 @@ def __getattribute__(self, key: Literal["_obj"]) -> ContextPath[_ContextLike]: .
@overload
def __getattribute__(
self, key: Literal["_offsets"]
) -> ContextPath[dict[int, int]]: ...
) -> ContextPath[dict[int, Buffer]]: ...
@overload
def __getattribute__(self, key: Literal["_io"]) -> ContextPath[_StreamType]: ...
@overload
Expand Down
8 changes: 6 additions & 2 deletions src/caterpillar/fields/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,13 @@ def offset(self) -> _ContextLambda[int] | int:
@offset.setter
def offset(self, value: _ContextLambda[int] | int):
self.__offset = value
self._has_offset = value not in (-1, None)
# _ContextLambda is ExprMixin so `__eq__` is overridden
# and `Tuple.__contains__` isn't correct. If value is an
# instance of _ContextLambda we may assume it is not
# -1 or None
self._has_offset = value not in (-1, None) or isinstance(value, _ContextLambda)
self._offset_is_lambda = callable(value)
self._keep_pos = value in (-1, None)
self._keep_pos = value in (-1, None) and not isinstance(value, _ContextLambda)

@property
def amount(self) -> _LengthT | None:
Expand Down
14 changes: 8 additions & 6 deletions src/caterpillar/model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@
struct,
UnionHook,
union,
unpack,
unpack_file,
pack,
pack_into,
pack_file,
sizeof,
Invisible,
StructDefMixin,
struct_factory,
Expand All @@ -46,6 +40,14 @@
BitfieldDefMixin,
)
from ._template import istemplate, template, TemplateTypeVar, derive
from .provider import (
unpack,
unpack_file,
pack,
pack_into,
pack_file,
sizeof,
)

__all__ = [
"Sequence",
Expand Down
Loading