Skip to content

Commit

Permalink
updgate github actions to full Python 3.12 release (#47)
Browse files Browse the repository at this point in the history
- update all dependencies
- rerun formatters
- remove Python 3.12 `__new__`/`__init__` hack for `struct.Struct`, at some
  point between the RCs and 3.12.2 this was updated to behave as in
  previous python versions.
  • Loading branch information
lojack5 authored Mar 14, 2024
1 parent d74ccfc commit 5c651ef
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 47 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ["3.9", "3.10", "3.11", "3.12-dev"]
python-version: ["3.9", "3.10", "3.11", "3.12"]
os: [ubuntu-latest, windows-latest, macos-latest]

runs-on: ${{ matrix.os }}
Expand Down
12 changes: 6 additions & 6 deletions requirements-tests.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
-r requirements.txt

# Tests / linting / formatters
pytest~=7.2.1
pytest-cov~=4.0.0
black~=22.12.0
isort~=5.11.4
flake8~=6.0.0
Flake8-pyproject~=1.2.2
pytest~=8.1.1
pytest-cov~=4.1.0
black~=24.2.0
isort~=5.13.2
flake8~=7.0.0
Flake8-pyproject~=1.2.3
1 change: 1 addition & 0 deletions structured/hint_types/arrays.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Array typhinting classes to return serializers.
"""

from __future__ import annotations

__all__ = [
Expand Down
1 change: 1 addition & 0 deletions structured/hint_types/serialize_as.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Class to use with Annotated to specify a custom type for serialization.
"""

from __future__ import annotations

__all__ = [
Expand Down
1 change: 1 addition & 0 deletions structured/hint_types/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Char and Unicode typhint classes, which dispatch to the appropriate serializer
based on provided specialization args.
"""

__all__ = [
'EncoderDecoder',
'unicode',
Expand Down
1 change: 1 addition & 0 deletions structured/serializers/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- All unpacking methods may return an iterable of values instead of a tuple.
For more details, check the docstrings on each method or attribute.
"""

from __future__ import annotations

__all__ = [
Expand Down
28 changes: 7 additions & 21 deletions structured/serializers/structs.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

import re
import struct
import sys
from functools import cached_property, partial, reduce
from itertools import chain, repeat
from typing import overload
Expand All @@ -34,8 +33,6 @@
)
from .api import Serializer

_PY_3_12 = sys.version_info >= (3, 12)


def noop_action(x: T) -> T:
"""A noop for StructActionSerializers where no additional wrapping is
Expand All @@ -46,6 +43,7 @@ def noop_action(x: T) -> T:

def compute_num_values(st: struct.Struct, *, __cache: dict[str, int] = {}) -> int:
"""Determine how many values are used in packing/unpacking a struct format."""
print(f'Compute num values: {st!r} {st.format} {st.size}')
try:
return __cache[st.format]
except KeyError:
Expand Down Expand Up @@ -109,19 +107,12 @@ def base_format(self) -> str:
def _split_format(self) -> tuple[ByteOrder, str]:
return split_byte_order(self.format)

def __new__(cls, format: str, byte_order: ByteOrder = ByteOrder.DEFAULT) -> Self:
if _PY_3_12:
return super().__new__(cls, byte_order.value + format) # type: ignore
else:
return super().__new__(cls)

def __init__(
self,
format: str,
byte_order: ByteOrder = ByteOrder.DEFAULT,
) -> None:
if not _PY_3_12:
super().__init__(byte_order.value + format)
super().__init__(byte_order.value + format)
self.num_values = compute_num_values(self)

def __str__(self) -> str:
Expand All @@ -141,8 +132,7 @@ def unpack(self, buffer: ReadableBuffer) -> tuple[Unpack[Ts]]:

def unpack_from(
self, buffer: ReadableBuffer, offset: int = 0
) -> tuple[Unpack[Ts]]:
...
) -> tuple[Unpack[Ts]]: ...

def unpack_read(self, readable: BinaryIO) -> tuple[Unpack[Ts]]:
# NOTE: use super-class's unpack to not interfere with custom
Expand All @@ -157,14 +147,12 @@ def pack_write(self, writable: BinaryIO, *values: Unpack[Ts]) -> None:
@overload
def __add__(
self, other: StructSerializer[Unpack[Ss]]
) -> StructSerializer[Unpack[Ts], Unpack[Ss]]:
...
) -> StructSerializer[Unpack[Ts], Unpack[Ss]]: ...

@overload
def __add__(
self, other: Serializer[Unpack[Ss]]
) -> Serializer[Unpack[Ts], Unpack[Ss]]:
...
) -> Serializer[Unpack[Ts], Unpack[Ss]]: ...

def __add__(self, other: Serializer) -> Serializer:
if isinstance(other, StructSerializer):
Expand Down Expand Up @@ -276,14 +264,12 @@ def with_byte_order(self, byte_order: ByteOrder) -> Self:
@overload
def __add__(
self, other: StructSerializer[Unpack[Ss]]
) -> StructActionSerializer[Unpack[Ts], Unpack[Ss]]:
...
) -> StructActionSerializer[Unpack[Ts], Unpack[Ss]]: ...

@overload
def __add__(
self, other: Serializer[Unpack[Ss]]
) -> Serializer[Unpack[Ts], Unpack[Ss]]:
...
) -> Serializer[Unpack[Ts], Unpack[Ss]]: ...

def __add__(self, other: Serializer) -> Serializer:
if isinstance(other, StructActionSerializer):
Expand Down
2 changes: 1 addition & 1 deletion structured/structured.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ def __repr__(self) -> str:
return f'<{self}>'

def __eq__(self, other) -> bool:
if type(other) == type(self):
if type(other) is type(self):
return all(
(getattr(self, attr) == getattr(other, attr) for attr in self.attrs)
)
Expand Down
22 changes: 10 additions & 12 deletions structured/type_checking.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,20 +204,18 @@ def with_check(
return inst # type: ignore

@overload
def __call__(self, *want_types: type[T]) -> _annotated[T]:
...
def __call__(self, *want_types: type[T]) -> _annotated[T]: ...

@overload
def __call__(self, *want_types: type[Union[T, U]]) -> _annotated[T, U]:
...
def __call__(self, *want_types: type[Union[T, U]]) -> _annotated[T, U]: ...

@overload
def __call__(self, *want_types: type[Union[T, U, V]]) -> _annotated[T, U, V]:
...
def __call__(self, *want_types: type[Union[T, U, V]]) -> _annotated[T, U, V]: ...

@overload
def __call__(self, *want_types: type[Union[T, U, V, W]]) -> _annotated[T, U, V, W]:
...
def __call__(
self, *want_types: type[Union[T, U, V, W]]
) -> _annotated[T, U, V, W]: ...

def __call__(self, *want_types):
return _annotated(*want_types)
Expand All @@ -227,13 +225,13 @@ def __call__(self, *want_types):


@overload
def safe_issubclass(a, cls: type[T]) -> TypeGuard[type[T]]:
...
def safe_issubclass(a, cls: type[T]) -> TypeGuard[type[T]]: ...


@overload
def safe_issubclass(a, cls: tuple[Unpack[Ts]]) -> TypeGuard[type[Union[Unpack[Ts]]]]:
...
def safe_issubclass(
a, cls: tuple[Unpack[Ts]]
) -> TypeGuard[type[Union[Unpack[Ts]]]]: ...


def safe_issubclass(a, cls): # type: ignore
Expand Down
10 changes: 4 additions & 6 deletions structured/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Various utility methods.
"""

import operator
import sys
import warnings
Expand All @@ -14,23 +15,20 @@
from .type_checking import Iterable, S

@overload
def zips(iterable1: Iterable[S], *, strict: bool = ...) -> Iterable[tuple[S]]:
...
def zips(iterable1: Iterable[S], *, strict: bool = ...) -> Iterable[tuple[S]]: ...

@overload
def zips( # noqa: F811
iterable1: Iterable[S], iterable2: Iterable[T], *, strict: bool = ...
) -> Iterable[tuple[S, T]]:
...
) -> Iterable[tuple[S, T]]: ...

@overload
def zips( # noqa: F811
iterable1: Iterable[Any],
iterable2: Iterable[Any],
*iterables: Iterable[Any],
strict: bool = ...,
) -> Iterable[tuple[Any, ...]]:
...
) -> Iterable[tuple[Any, ...]]: ...

def zips(*iterables: Iterable, strict: bool = False): # noqa: F811
"""Python 3.9 compatible way of emulating zip(..., strict=True)"""
Expand Down

0 comments on commit 5c651ef

Please sign in to comment.