Skip to content

Commit 5916b42

Browse files
committed
Documentation fixes, update changelog
1 parent 564a2be commit 5916b42

File tree

3 files changed

+122
-1
lines changed

3 files changed

+122
-1
lines changed

binobj/pep526.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ def dataclass(class_object: type[TStruct]) -> type[TStruct]:
183183
184184
Here are a few examples of how you can declare your fields::
185185
186+
@binobj.dataclass
186187
class MyStruct(binobj.Struct):
187188
# Preferred: use a class object
188189
foo: UInt16
@@ -198,6 +199,20 @@ class MyStruct(binobj.Struct):
198199
# the field instance!
199200
baz: Timestamp64(signed=False)
200201
202+
For compatibility with type checkers, use :class:`typing.Annotated`. The struct
203+
above would be declared like this::
204+
205+
@binobj.dataclass
206+
class MyStruct(binobj.Struct):
207+
foo: Annotated[int, UInt16]
208+
bar: Annotated[str, StringZ] = ""
209+
210+
# We don't need Annotated[...] here because type checkers will expect the
211+
# same annotation that BinObj uses.
212+
sub_struct: MyOtherStruct
213+
214+
baz: Annotated[datetime, Timestamp64(signed=False)]
215+
201216
.. versionadded:: 0.9.0
202217
203218
.. _PEP 526: https://www.python.org/dev/peps/pep-0526/

binobj/structures.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ def collect_assigned_fields(
229229
230230
.. versionchanged:: 0.12.0
231231
Removed redundant first argument. The class name is now taken from
232-
`class_metadata` instead of as an explicit argument.
232+
`class_metadata` instead of needing to be passed in as an explicit argument.
233233
"""
234234
field_index = len(class_metadata.components)
235235
n_fields_found = 0

docs/source/CHANGELOG.rst

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,107 @@
11
Changelog
22
=========
33

4+
0.12.0
5+
------
6+
7+
Released 2024-09-09
8+
9+
New Features
10+
~~~~~~~~~~~~
11+
12+
Type Checking Compatibility
13+
^^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
15+
Type checker-compatible annotations are finally here! The old dataclass annotation way
16+
would cause type checkers to report errors when you try assigning an ``int`` to a field
17+
marked as ``UInt8``. You can now get around this by using ``typing.Annotated``.
18+
19+
The old way:
20+
21+
.. code-block:: python
22+
23+
@binobj.dataclass
24+
class MyStruct(binobj.Struct):
25+
foo: UInt16
26+
bar: StringZ = ""
27+
sub_struct: MyOtherStruct
28+
baz: Timestamp64(signed=False)
29+
30+
To save you headaches with MyPy, the same struct can be declared like so:
31+
32+
.. code-block:: python
33+
34+
@binobj.dataclass
35+
class MyStruct(binobj.Struct):
36+
foo: Annotated[int, UInt16]
37+
bar: Annotated[str, StringZ] = ""
38+
sub_struct: MyOtherStruct # Note no change necessary here
39+
baz: Annotated[datetime, Timestamp64(signed=False)]
40+
41+
Full Mapping-like Behavior
42+
^^^^^^^^^^^^^^^^^^^^^^^^^^
43+
44+
Two new struct wrappers, ``StructMappingProxy`` and ``MutableStructMappingProxy``, allow
45+
you to use a ``Struct`` exactly as you would a ``Mapping`` or ``MutableMapping``:
46+
47+
.. code-block:: python
48+
49+
ro_proxy = binobj.StructMappingProxy(struct_instance)
50+
assert ro_proxy.struct is struct_instance # Original struct still available
51+
52+
for key, value in ro_proxy.items():
53+
print(f"{k!r} = {v!r}")
54+
55+
cm = collections.ChainMap({}, binobj.MutableStructMappingProxy(struct_instance))
56+
57+
These are typed as ``Mapping[str, Any]`` and ``MutableMapping[str, Any]``, respectively.
58+
59+
Other New Features
60+
^^^^^^^^^^^^^^^^^^
61+
62+
Now testing on Python 3.13-rc1.
63+
64+
.. _PEP 593: https://peps.python.org/pep-0593/
65+
66+
Deprecations
67+
~~~~~~~~~~~~
68+
69+
* Support for Python 3.9 will be removed in the next backwards-incompatible release.
70+
* Using ``Field`` instances as bare annotations is deprecated; use ``typing.Annotated``
71+
instead.
72+
73+
Bugfixes
74+
~~~~~~~~
75+
76+
* On Python ≤ 3.10, using ``Field`` instances as type annotations completely broke if
77+
deferred annotation evaluation was enabled with ``from __future__ import annotations``.
78+
This can now be worked around by using ``Annotated``, or with normal field assignment.
79+
* When reading fixed data, if ``exact`` was true the error message would be one byte off
80+
when saying how much it expected to read.
81+
* Better type annotations for containers.
82+
* Error messages now use ``__qualname__`` for classes, instead of ``__name__``. This
83+
will only change the output of nested classes.
84+
85+
Breaking Changes
86+
~~~~~~~~~~~~~~~~
87+
88+
Dropped support for EOL Python 3.7 and 3.8.
89+
90+
Other Changes
91+
~~~~~~~~~~~~~
92+
93+
* Refactored ``Struct`` class initialization and pushed it into a factory method on
94+
``StructMetadata``. The eventual goal is to completely eliminate the need for
95+
inheriting ``Struct``.
96+
* Switched from Black to Ruff.
97+
* Minimum version of ``typing_extensions`` is now 4.4.
98+
* Upgraded test dependencies.
99+
4100
0.11.4
5101
------
6102

103+
Released 2024-03-13
104+
7105
Bugfixes
8106
~~~~~~~~
9107

@@ -28,6 +126,8 @@ Other Changes
28126
0.11.3
29127
------
30128

129+
Released 2023-11-10
130+
31131
Bugfixes
32132
~~~~~~~~
33133

@@ -37,6 +137,8 @@ in 3.10. We now handle that case in the annotation detection.
37137
0.11.2
38138
------
39139

140+
(YANKED)
141+
40142
Bugfixes
41143
~~~~~~~~
42144

@@ -57,6 +159,8 @@ Other Changes
57159
0.11.1
58160
------
59161

162+
Released 2023-09-16
163+
60164
Bugfixes
61165
~~~~~~~~
62166

@@ -91,6 +195,8 @@ behaves the same.
91195
0.11.0
92196
------
93197

198+
Released 2023-02-14
199+
94200
New Features
95201
~~~~~~~~~~~~
96202

0 commit comments

Comments
 (0)