Skip to content

Commit

Permalink
Python 3.9: use Optional instead of <type> | None
Browse files Browse the repository at this point in the history
Not 100% sure why these didn't throw errors before, or why only some of
them do now, I'm sure it's something to do with
`from __future__ import annotations` interactions on when it stringizes
annotations.
  • Loading branch information
lojack5 committed Oct 5, 2024
1 parent 7e72c85 commit c023d8d
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 15 deletions.
4 changes: 2 additions & 2 deletions structured/hint_types/arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
StaticStructArraySerializer,
StructSerializer,
)
from ..type_checking import Annotated, Generic, S, Self, T, TypeVar, annotated
from ..type_checking import Annotated, Generic, S, Self, T, TypeVar, annotated, Optional
from ..utils import StructuredAlias
from .basic_types import _SizeTypes

Expand All @@ -31,7 +31,7 @@ class Header:
def __init__(
self,
count: int | StructSerializer[int],
data_size: StructSerializer[int] | None,
data_size: Optional[StructSerializer[int]],
) -> None:
self.count = count
self.data_size = data_size
Expand Down
5 changes: 3 additions & 2 deletions structured/serializers/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
ClassVar,
Generic,
Iterable,
Optional,
ReadableBuffer,
Self,
Ss,
Expand Down Expand Up @@ -163,7 +164,7 @@ def is_final(self) -> bool:
"""
return self.get_final() is not None

def get_final(self) -> Serializer | None:
def get_final(self) -> Optional[Serializer]:
"""Get the serializer (if any) that makes this serializer the final
serializer.
"""
Expand Down Expand Up @@ -248,7 +249,7 @@ def __init__(self, serializers: tuple[Serializer, ...]) -> None:
for serializer in serializers
)

def get_final(self) -> Serializer | None:
def get_final(self) -> Optional[Serializer]:
return self.serializers[-1].get_final()

def prepack(self, partial_object: Any) -> Serializer:
Expand Down
4 changes: 3 additions & 1 deletion structured/serializers/conditional.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
to a Truthy value, the original serializer operates as normal. Otherwise,
it acts as if the serializer was not there.
"""
from __future__ import annotations

__all__ = [
'ConditionalSerializer',
Expand All @@ -14,6 +15,7 @@
BinaryIO,
Callable,
Generic,
Optional,
ReadableBuffer,
Self,
Ts,
Expand Down Expand Up @@ -52,7 +54,7 @@ def __init__(
f'{self.num_values}, got {expected}'
)

def get_final(self) -> Serializer | None:
def get_final(self) -> Optional[Serializer]:
return self.serializers[True].get_final()

def with_byte_order(self, byte_order: ByteOrder) -> Self:
Expand Down
3 changes: 2 additions & 1 deletion structured/serializers/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
BinaryIO,
Callable,
ClassVar,
Optional,
ReadableBuffer,
Self,
Union,
Expand Down Expand Up @@ -368,7 +369,7 @@ def __init__(
self.encoder = encoder
self.decoder = decoder

def get_final(self) -> Serializer | None:
def get_final(self) -> Optional[Serializer]:
return self.serializer.get_final()

@property
Expand Down
3 changes: 2 additions & 1 deletion structured/serializers/structured.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
BinaryIO,
ClassVar,
Generic,
Optional,
ReadableBuffer,
TypeVar,
WritableBuffer,
Expand Down Expand Up @@ -44,7 +45,7 @@ def __init__(self, obj_type: type[TStructured]) -> None:
self.obj_type = obj_type
self.size = 0

def get_final(self) -> Serializer | None:
def get_final(self) -> Optional[Serializer]:
return self.obj_type.serializer.get_final()

def pack(self, values: TStructured) -> bytes:
Expand Down
3 changes: 2 additions & 1 deletion structured/serializers/tuples.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
ClassVar,
Generic,
Iterable,
Optional,
ReadableBuffer,
Ts,
Unpack,
Expand All @@ -28,7 +29,7 @@ class TupleSerializer(Generic[Unpack[Ts]], Serializer[Unpack[Ts]]):
def __init__(self, serializers: Iterable[Serializer]) -> None:
self.serializer = sum(serializers, NullSerializer())

def get_final(self) -> Serializer | None:
def get_final(self) -> Optional[Serializer]:
return self.serializer.get_final()

@property
Expand Down
7 changes: 4 additions & 3 deletions structured/serializers/unions.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
Callable,
ClassVar,
Iterable,
Optional,
ReadableBuffer,
WritableBuffer,
annotated,
Expand All @@ -35,8 +36,8 @@ class AUnion(Serializer):

num_values: ClassVar[int] = 1
result_map: dict[Any, Serializer]
default: Serializer | None
_last_serializer: Serializer | None
default: Optional[Serializer]
_last_serializer: Optional[Serializer]

def __init__(self, result_map: dict[Any, Any], default: Any = None) -> None:
"""result_map should be a mapping of possible return values from `decider`
Expand All @@ -52,7 +53,7 @@ def __init__(self, result_map: dict[Any, Any], default: Any = None) -> None:
}
self.size = 0

def get_final(self) -> Serializer | None:
def get_final(self) -> Optional[Serializer]:
for serializer in self.result_map.values():
if serializer.is_final():
return serializer.get_final()
Expand Down
6 changes: 3 additions & 3 deletions structured/structured.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def ispad(annotation: Any) -> bool:
)


def final_transform(base_type: Any, hint: Any) -> Serializer | None:
def final_transform(base_type: Any, hint: Any) -> Optional[Serializer]:
if isinstance(hint, Serializer):
return hint

Expand Down Expand Up @@ -120,8 +120,8 @@ def get_structured_base(cls: type[Structured]) -> Optional[type[Structured]]:
def gen_init(
args: dict[str, Any],
*,
globalsns: dict[str, Any] | None = None,
localsns: dict[str, Any] | None = None,
globalsns: Optional[dict[str, Any]] = None,
localsns: Optional[dict[str, Any]] = None,
) -> Callable:
"""Generates an __init__ method for a class. `args` should be a mapping of
arguments to type annotations to be used in the method definition.
Expand Down
2 changes: 1 addition & 1 deletion structured/type_checking.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def istuple(annotation: Any) -> TypeGuard[tuple]:
return get_origin(annotation) in (tuple, Tuple)


def get_tuple_args(annotation: Any, fixed_size: bool = True) -> tuple[Any, ...] | None:
def get_tuple_args(annotation: Any, fixed_size: bool = True) -> Optional[tuple[Any, ...]]:
"""Get the arguments to a tuple type hint, or None if the annotation is not
a tuple hint. If `fixed_size` is True (default), then the tuple must be
a fixed length tuple hint.
Expand Down

0 comments on commit c023d8d

Please sign in to comment.